#!/usr/bin/env python
# coding=utf-8

# Copyright (C) 2010 Jonathan Ciesla

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.


import pygtk
pygtk.require('2.0')
import gtk
import time
import os.path, getpass

class iapetal_launcher:

    def delete_event(self, widget, event, data=None):
        gtk.main_quit()
        return False

    def aboutdial(self, widget, event, data=None):
        self.abouts = gtk.Dialog(title="Iapetal © 2010 Jonathan Ciesla", parent=None, flags=0, buttons=None)

        abouttext = "Rescue the people from the habitat module by ferrying them to the rescue ship flying overhead.\n\n"
        abouttext += "Land next to the habitat module to load people from it, and land on the desk of the rescue ship to unload them.\n"
        abouttext += "If you run out of landers, all the people in a level are killed, the game ends.\n"
        abouttext += "If the rescue ship is destroyed and there are still people, you can attempt to escape into space with them,\n"
        abouttext += "which will also end the level.\n";
        abouttext += "Your deflector shield offers limited protection from asteroids, the surface and atmospheric fricton.\n\n"
        abouttext += "Rotate the lander with the right and left arrows.  Up arrow is the main thruster.  Down is the weaker downward thruster.\n"
        abouttext += "Space is the shield. P pauses. S toggles sound. Q quits."


        self.aboutlabel = gtk.Label(abouttext)
        self.abouts.vbox.pack_start(self.aboutlabel, True, True, 0)

        self.aboutbuttonok = gtk.Button("Ok")
        self.aboutbuttonok.connect("clicked", lambda w: self.abouts.destroy())
        self.abouts.action_area.pack_start(self.aboutbuttonok, True, True, 0)

        self.aboutlabel.show()
        self.aboutbuttonok.show()
        self.abouts.show()
        return False


    def runthing(self, widget, data):
        import os
        if os.path.lexists("/usr/bin/iapetal"):
            runobject = "/usr/bin/iapetal"
        else:
            runobject = "./iapetal.py"
        if self.windowedcheck.get_active():
            runobject = runobject + " -w"
        if not self.soundcheck.get_active():
            runobject = runobject + " -n"
        os.system(runobject)

        scorefilename = os.path.join(os.path.expanduser('~' +  getpass.getuser()), '.iapetal_score')
        if not os.path.isfile(scorefilename):
                open(scorefilename, 'a').close()
        scorefile = open(scorefilename, 'r')
        oldscore = scorefile.readline()
        scorefile.close()
        if not oldscore: oldscore = 0        
        scoretext = 'High Score: ' + str(oldscore)
        self.scorelabel.set_text(scoretext)

    def __init__(self):

        self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
        self.window.set_title("Iapetal")
        self.window.set_border_width(10)
        self.window.connect("delete_event", self.delete_event, None)

        self.mainbox = gtk.VBox(False, 0)
        self.window.add(self.mainbox)

        scorefilename = os.path.join(os.path.expanduser('~' +  getpass.getuser()), '.iapetal_score')
        if not os.path.isfile(scorefilename):
                open(scorefilename, 'a').close()
        scorefile = open(scorefilename, 'r')
        oldscore = scorefile.readline()
        scorefile.close()
        if not oldscore: oldscore = 0        
        scoretext = 'High Score: ' + str(oldscore)
        self.scorelabel = gtk.Label(scoretext)
        self.mainbox.pack_start(self.scorelabel, True, True, 0)

        if os.path.lexists("/usr/share/iapetal"):
            file_path = "/usr/share/iapetal/"
        else:
            file_path = "./"
                

        self.title = gtk.Image()
        self.title.set_from_file(file_path + 'title.png')
        self.mainbox.pack_start(self.title, True, True, 0)

        self.logo = gtk.Image()
        self.logo.set_from_file(file_path + 'habitat.png')
        self.mainbox.pack_start(self.logo, True, True, 0)

        self.runbutton = gtk.Button("Go!")
        self.runbutton.connect("clicked", self.runthing, None)
        self.runbutton.set_tooltip_text("Play")
        self.mainbox.pack_start(self.runbutton, True, True, 0)

        self.windowedcheck = gtk.CheckButton(label="Windowed")
        self.windowedcheck.set_tooltip_text("Run in windowed mode")
        self.mainbox.pack_start(self.windowedcheck, True, True, 0)

        self.soundcheck = gtk.CheckButton(label="Sound")
        self.soundcheck.set_tooltip_text("Enable/disable sound")
        self.soundcheck.set_active(True)
        self.mainbox.pack_start(self.soundcheck, True, True, 0)

        self.docbutton = gtk.Button("About")
        self.docbutton.connect("clicked", self.aboutdial, None)
        self.docbutton.set_tooltip_text("What's all this about?")
        self.mainbox.pack_start(self.docbutton, True, True, 0)

        self.quitbutton = gtk.Button("Quit")
        self.quitbutton.connect("clicked", self.delete_event, None)
        self.quitbutton.set_tooltip_text("Exit Iapetal")
        self.mainbox.pack_start(self.quitbutton, True, True, 0)

        self.quitbutton.show()
        self.title.show()
        self.logo.show()
        self.scorelabel.show()
        self.docbutton.show()
        self.windowedcheck.show()
        self.soundcheck.show()
        self.runbutton.show()
        self.mainbox.show()
        self.window.show()
        

def main():
    gtk.main()

if __name__ == "__main__":
    hello = iapetal_launcher()
    main()
