# -*- coding: utf-8 -*-
#!/usr/bin/env python

import wx
from wx.lib.mixins.inspection import InspectionMixin

import wx.lib.sized_controls as sc

print(wx.VERSION_STRING)

class PwSplashScreen(wx.SplashScreen):
    def __init__(self, caller):
        #bmp =  wx.Image(opj(os.path.abspath(os.path.join(os.path.split(__file__)[0],"bitmaps","splash.png")))).ConvertToBitmap()
        bmp = wx.ArtProvider.GetBitmap(wx.ART_QUESTION, wx.ART_OTHER, wx.Size(64, 64))
        wx.SplashScreen.__init__(self, bmp,
                                 wx.SPLASH_CENTER_ON_SCREEN | wx.SPLASH_TIMEOUT, #  | wx.STAY_ON_TOP,
                                 3000, None, -1)

        self.caller = caller

        self.Bind(wx.EVT_CLOSE, self.OnClose)


    def OnClose(self, evt):
        # Make sure the default handler runs too so this window gets
        # destroyed
        evt.Skip()
        wx.CallAfter(self.caller.showLogin, self)
        print('close splash')


class LoginDialog(sc.SizedDialog):
    def __init__(self, *args, **kwds):

        super(LoginDialog, self).__init__(None)

        self.loginState = False

        contentPane = self.GetContentsPane()

        header = wx.StaticText(contentPane, -1, "PermitWatch Login")
        header.SetSizerProps(align='center')

        controlPane = sc.SizedPanel(contentPane)
        controlPane.SetSizerType('grid', options={'cols': 2, })

        lab = wx.StaticText(controlPane, -1, "Username: ")

        self.unameTxt = wx.TextCtrl(controlPane, -1, "", size=(150,-1),style=wx.TAB_TRAVERSAL|
                             wx.TE_PROCESS_ENTER|wx.RAISED_BORDER )
        self.unameTxt.SetHelpText("Username is case sensitive.")

        lab = wx.StaticText(controlPane, -1, "Password: ")

        self.pwTxt = wx.TextCtrl(controlPane, -1, "", size=(150,-1), style=wx.TE_PASSWORD|
                              wx.TAB_TRAVERSAL|wx.TE_PROCESS_ENTER|wx.RAISED_BORDER)
        self.pwTxt.SetHelpText("Password is case sensitive and must be longer than 6 characters.")

        btnPane = sc.SizedPanel(contentPane)
        btnPane.SetSizerType('horizontal')
        btnPane.SetSizerProps(align='center')

        OkBtn = wx.Button(btnPane, wx.ID_OK)
        OkBtn.SetHelpText("The OK button allows logging in.")
        OkBtn.SetDefault()
        self.Bind(wx.EVT_BUTTON, self.OnOK, OkBtn)

        CancelBtn = wx.Button(btnPane, wx.ID_CANCEL)
        CancelBtn.SetHelpText("The Cancel button quits the application.")
        self.Bind(wx.EVT_BUTTON, self.OnClose, CancelBtn)

        self.Fit()

    def OnOK(self, event):
        wx.MessageBox("You entered username %s and %s password." % (self.unameTxt.GetValue(), self.pwTxt.GetValue()))
        ## Add validity checking here.
        self.loginState = True # if success, or some object with login info like name etc or None
        self.Close()

    def OnClose(self, event):
        print('close dialog')
        event.Skip()


class BaseApp(wx.App, InspectionMixin):

    """The Application, using WIT to help debugging."""

    def OnInit(self):
        """
        Do application initialization work, e.g. define application globals.
        """
        self._loginShown = False
        splash = PwSplashScreen(caller=self)

        return True

    def showLogin(self, splash):
        if not self._loginShown:
            if splash.Timeout:
                wx.MicroSleep(splash.Timeout/2)
                self._loginShown = True
                self.doShowLogin()

    def doShowLogin(self):
        with LoginDialog() as dlg:
            print('show login')
            dlg.ShowModal()
            loginState = dlg.loginState

            if loginState:
                self.mainFrame = wx.Frame(None)
                wx.GetApp().SetTopWindow(self.mainFrame)
                self.mainFrame.Show()


if __name__ == '__main__':
    app = BaseApp()
    app.MainLoop()

