import wx
import mcnostalgialib
import thread

class Mc(wx.Frame):

    def __init__(self):
        wx.Frame.__init__(self,None, size=(300,300), title='MCNostalgia')
        pan = wx.Panel(self)
        self.Centre()
        self.mcn = mcnostalgialib.MCNostalgia(cli=False)
        self.versionlist = self.mcn.fetchAvailableVersions()
        self.txt = wx.TextCtrl(pan, -1, pos=(0,0), size=(300,20), style=wx.TE_READONLY)
        self.button1 = wx.Button(pan, label='Browse', pos=(200,30))
        self.button1.Bind(wx.EVT_BUTTON, self.onbutton)
        self.button2 = wx.Button(pan, label = 'OK', pos=(wx.CENTRE,240))
        self.button2.Bind(wx.EVT_BUTTON, self.startDowngrade)
        self.button3 = wx.Button(pan, label='OK', pos=(wx.CENTRE,30))
        self.button3.Bind(wx.EVT_BUTTON, self.onbutton2)

        windows = '*Attention Windows Users In The Version Selection List\nAfter Clicking Choice Be Sure To Press Enter*'
        self.button4 = wx.Button(pan, label='Default', pos = (100,30))
        self.txtdummy = wx.StaticText(pan, -1, windows, pos = (wx.CENTRE,100), size = (300,40), style = wx.TE_MULTILINE)
        self.button4.Bind(wx.EVT_BUTTON, self.onbutton3)
        self.version = wx.ListBox(pan, -1, pos=(wx.CENTRE,wx.CENTRE), size=(280,230), choices=self.versionlist, style=wx.LB_SINGLE)
        self.version.Bind(wx.EVT_LEFT_UP, self.printSelection)
        self.versioninput = wx.TextCtrl(pan, -1, pos=(90,241), size=(190,20), style=wx.DEFAULT)
        self.Bind(wx.EVT_CLOSE, self.OnClose)

        helpr = 'Enter Version # Above Here'
        self.button2.Show(False)
        self.version.Show(False)
        self.versioninput.Show(False)
        self.x = 0
        self.bindir = self.mcn.bindir
        self.txt.write(self.bindir)

    def OnClose(self, evt) :
        mcinstance2.Destroy()
        self.Destroy()
 
    def checkx(self):
        if self.x == 0:
            self.button1.Show(True)
            self.button2.Show(False)
            self.button3.Show(True)
            self.button4.Show(False)
            self.txtdummy.Show(False)
            self.txt.Show(True)
            self.version.Show(False)
            
        elif self.x == 1:
            self.button1.Show(False)
            self.button2.Show(True)
            self.button3.Show(False)
            self.button4.Show(False)
            self.txt.Show(False)
            self.version.Show(True)
        elif self.x == 2:
            self.button2.Show(False)
            self.version.Show(False)
            self.versioninput.Show(False)
            mcinstance1.Show(False)
            mcinstance2.Show(True)

            
    def onbutton(self, evt):
        dialog = wx.DirDialog(None, "Choose Minecraft Bin Folder:",style=wx.DD_DEFAULT_STYLE | wx.DD_NEW_DIR_BUTTON)
        if dialog.ShowModal() == wx.ID_OK:
            self.txt.Clear()
            self.bindir = dialog.GetPath()
            self.mcn.bindir = self.bindir
            self.mcn.outputdir = self.bindir
            self.mcn.origdir = self.bindir
            self.txt.write(self.bindir)
            dialog.Destroy()
    

    def onbutton2(self, evt):
        self.x = 1
        self.checkx()

    def onbutton3(self, evt):

        self.bindir = self.mcn.getMCBinPath()
        self.mcn.bindir = self.bindir
        self.mcn.outputdir = self.bindir
        self.mcn.origdir = self.bindir
        self.txt.Clear()
        self.txt.write(self.bindir)

    def printSelection(self, evt) :
        self.selection = self.versionlist[self.version.GetSelections()[0]]
        self.versioninput.Clear()
        self.versioninput.write(self.selection)
        self.versioninput.Show(True)
        

    def startDowngrade(self, evt) :
        self.x = 2
        self.checkx()
        mcinstance2.downgrade2()

class downgrade(wx.Frame):


    def __init__(self):
        wx.Frame.__init__(self,None, size=(500,500), title='Downgrade')
        self.Bind(wx.EVT_CLOSE, self.OnClose)
        self.SetSize((300, 200))
        self.SetTitle('Message box')
        self.Centre()
        pan2 = wx.Panel(self)
        self.downgradetxt = wx.TextCtrl(pan2, -1, pos=(wx.CENTRE,wx.CENTRE), size=(480,460), style=wx.TE_READONLY|wx.TE_MULTILINE)
        self.Show(True)

    def downgrade2(self) :
        self.Update()
        thread.start_new_thread(mcinstance1.mcn.patchTo, (mcinstance1.selection, ))
        while not mcinstance1.mcn.done[0] :
            if mcinstance1.mcn.checknow :
                for line in mcinstance1.mcn.status :
                    self.downgradetxt.write(line + "\n")
                    self.Update()
                mcinstance1.mcn.status = []
                mcinstance1.mcn.checknow = False
        if mcinstance1.mcn.status != [] :
            for line in mcinstance1.mcn.status :
                self.downgradetxt.write(line + "\n")
                self.Update()
            mcinstance1.mcn.status = []
            mcinstance1.mcn.checknow = False
        done = self.ShowMessage(mcinstance1.mcn.done[1])
    def ShowMessage(self, success):
        if success : wx.MessageBox('Downgrade completed', 'Info', wx.OK | wx.ICON_INFORMATION)
        else : wx.MessageBox('Downgrade failed', 'Info', wx.OK | wx.ICON_INFORMATION)
    def OnClose(self, evt):
        mcinstance1.Destroy()
        self.Destroy()



if __name__=='__main__':
    App=wx.App()
    mcinstance1 = Mc()
    mcinstance1.Show()
    mcinstance2 = downgrade()
    mcinstance2.Show(False)
    App.MainLoop()
