"""
------------------------------------------------------------------------------
--                                                                          --
--                                 LAB 07                                   --
--                                                                          --
--                        S A V E V I E W E R . P Y                         -- 
--                                                                          --
------------------------------------------------------------------------------
-- <Your Name Here>                                                         --
--                                                                          --
-- <Class Name Here>                                                        --
--                                                                          --
------------------------------------------------------------------------------
-- This file contains a small app that displays Final Fantasy VII save data --
-- and writes saves out to CSV files.                                       --
--                                                                          --
------------------------------------------------------------------------------
"""

from Tkinter import *
import tkFileDialog
import tkSimpleDialog
import tkMessageBox
import portraits
from lab07 import *

class SaveViewMainWindow(Frame):
    """
    The main window of the app.  Has buttons for each function it performs
    """
    def __init__(self, parent = None):
        Frame.__init__(self, parent)
        self.pack()

        self.saves = []

        self.read_list = Button(self, height=1, width=17)
        self.read_list['text'] = 'Read Saves'
        self.read_list['command'] = self.read_from_file
        self.read_list.grid(row = 0, column = 0)

        self.write_list = Button(self, height=1, width=17)
        self.write_list['text'] = 'Write CSV'
        self.write_list['command'] = self.write_to_file
        self.write_list.grid(row = 1, column = 0)
        
        self.view_saves = Button(self, height=1, width=17)
        self.view_saves['text'] = 'View Saves'
        self.view_saves['command'] = self.show_saves
        self.view_saves.grid(row = 2, column = 0)

        self.quit = Button(self, height=1, width=17)
        self.quit['text'] = 'Quit'
        self.quit['command'] = exit
        self.quit.grid(row = 3, column = 0)

    def read_from_file(self):
        """
        Asks the user for an FF7 save file to read data from.  Appends each
        save it reads to self.saves
        """
        fname = tkFileDialog.askopenfilename()

        if fname == '' or type(fname) not in (str, unicode):
            return

        print fname

    def write_to_file(self):
        """
        Asks the user for a file name to write the current list of saves to in
        CSV format.
        """
        fname = tkFileDialog.asksaveasfilename()

        if fname == '' or type(fname) not in (str, unicode):
            return

        print fname

    def show_saves(self):
        """
        Opens a new window which displays save file information and allows the 
        user to page through all the saves currently loaded.
        """
        if len(self.saves) > 0:
            SaveDisplayWindow(self.saves, self)
        else:
            tkMessageBox.showerror('No Saves', 'There are no saves loaded for viewing!')

class SaveDisplayWindow(Toplevel):
    """
    A window which displays FFVII save file information to the user
    """
    def __init__(self, saves, parent = None):
        Toplevel.__init__(self, parent)
        self.transient(parent)
        self.parent = parent
        self.saves = saves
        self.current = 0

        self.make_portraits()

        self.body = Frame(self)
        self.body.pack()
        self.set_save()
        self.initial_focus = self

        prev = Button(self.body, text='<-', command=self.previous_save)
        prev.grid(row = 1, column = 0)

        ok = Button(self.body, text='OK', command=self.cancel)
        ok.grid(row = 1, column = 1)

        nxt = Button(self.body, text='->', command=self.next_save)
        nxt.grid(row = 1, column = 2)

        self.bind("<Return>", self.cancel)
        self.bind("<Escape>", self.cancel)

    def set_save(self):
        """
        Updates the currently displayed save.
        """
        self.save_display = SaveDisplayWidget(self.saves[self.current], self.portraits, self.body)
        self.save_display.grid(row = 0, column = 0, columnspan = 3)

    def cancel(self, event=None):
        self.parent.focus_set()
        self.destroy()

    def next_save(self):
        """
        Pages to the next available save in the list, if there is one.  If not,
        nothing happens.
        """
        if self.current < len(self.saves) - 1:
            self.current += 1
            self.save_display.destroy()
            self.set_save()

    def previous_save(self):
        """
        Pages to the preveious save in the list, if there is one.  If not,
        nothing happens.
        """
        if self.current > 0:
            self.current -= 1
            self.save_display.destroy()
            self.set_save()

    def make_portraits(self):
        self.portraits = {}
        self.portraits['Cloud'] = PhotoImage(data = portraits.cloud)
        self.portraits['Barret'] = PhotoImage(data = portraits.barret)
        self.portraits['Tifa'] = PhotoImage(data = portraits.tifa)
        self.portraits['Aerith'] = PhotoImage(data = portraits.aerith)
        self.portraits['Red XII'] = PhotoImage(data = portraits.red_xii)
        self.portraits['Yuffie'] = PhotoImage(data = portraits.yuffie)
        self.portraits['Cait Sith'] = PhotoImage(data = portraits.cait_sith)
        self.portraits['Vincent'] = PhotoImage(data = portraits.vincent)
        self.portraits['Cid'] = PhotoImage(data = portraits.cid)
        self.portraits['No one'] = PhotoImage(data = portraits.empty)
        

class SaveDisplayWidget(Frame):
    def __init__(self, ff7save, portraits, parent = None):
        """
        Constrcuts a Save Display widget using the passed in FF7Save object.
        Pulls character picuters out of portraits.
        """
        Frame.__init__(self, parent)
        self.pack()

        Label(self, text = 'Cloud').grid(row = 0, column = 0, sticky=W)
        Label(self, text = 'Level: 99').grid(row = 1, column = 0, sticky=W)

        self.canvas = Canvas(self, width=84*3, height=96)
        self.canvas.create_image(42, 48, image = portraits['Cloud'])
        self.canvas.create_image(84 * 1 + 42, 48, image = portraits['Cloud'])
        self.canvas.create_image(84 * 2 + 42, 48, image = portraits['Cloud'])
        self.canvas.grid(row = 2, column = 0, columnspan = 3)

        Label(self, text = 'HP: 9999/9999').grid(row = 3, column = 0, sticky=W)
        Label(self, text = 'MP: 999/999').grid(row = 4, column = 0, sticky=W)

        Label(self, text = 'Location: "Debug Room"').grid(row = 5, column = 0, sticky=W, columnspan=2)
        Label(self, text = 'Gil: 1000000000').grid(row = 5, column = 2, sticky=E)

if __name__ == '__main__':
    root = Tk()
    root.title('FFVII Save Viewer')
    app = SaveViewMainWindow(root)
    app.mainloop()
