#Boa:Frame:Frame1

import os

import wx
import agw.aui

def create(parent):
    return Frame1(parent)

[wxID_FRAME1] = [wx.NewId() for _init_ctrls in range(1)]

class Frame1(wx.Frame):
    def _init_ctrls(self, prnt):
        # generated method, don't edit
        wx.Frame.__init__(self, id=wxID_FRAME1, name='', parent=prnt,
              pos=wx.Point(23, 23), size=wx.Size(1024, 768),
              style=wx.DEFAULT_FRAME_STYLE,
              title='The Wine Cellar Book - prototype')
        self.SetClientSize(wx.Size(1008, 732))

    def __init__(self, parent):
        self._init_ctrls(parent)
        
        self.CreateConfig()

        # tell FrameManager to manage this frame        
        self._mgr = aui.AuiManager()
        self._mgr.SetManagedWindow(self)
        self._mgr.SetDockSizeConstraint(0.5, 0.5)        
        
        self._perspectives = []
        
        
        bookStyle = aui.AUI_NB_DEFAULT_STYLE
        bookStyle &= ~(aui.AUI_NB_CLOSE_ON_ACTIVE_TAB)
        self.nb = aui.AuiNotebook(self, style=bookStyle)

        text = ['Search result', 'Search options']
        for num in range(0, len(text)):
            tc = wx.TextCtrl(self.nb, -1, text[num]+' would be shown here',
                               style=wx.TE_MULTILINE)
            self.nb.AddPage(tc, text[num])

        bookStyle = aui.AUI_NB_DEFAULT_STYLE
        bookStyle &= ~(aui.AUI_NB_CLOSE_ON_ACTIVE_TAB)
        self.nb2 = aui.AuiNotebook(self, style=bookStyle)

        text = ['Wine details', 'Vintage/Bottle', 'Purchase', 'Consumption', 'Rating', 'Tasting', 'Image', 'Tags']
 
        for num in range(0, len(text)):
            if num == 1:
                panel = wx.Panel(self.nb2)
                vbText = ['Vintage', 'Add. Vintage', 'Bottle size']
                bookStyle = aui.AUI_NB_DEFAULT_STYLE
                bookStyle &= ~(aui.AUI_NB_CLOSE_ON_ACTIVE_TAB)
                self.nb3 = aui.AuiNotebook(panel, style=bookStyle)
                for vbNum in range(0, len(vbText)):
                    aTc = wx.TextCtrl(self.nb3, -1, vbText[vbNum]+' would be shown here',
                                       style=wx.TE_MULTILINE)
                    self.nb3.AddPage(aTc, vbText[vbNum])
                vbSizer = wx.BoxSizer(orient=wx.VERTICAL)
                vbSizer.Add(self.nb3, 1, wx.EXPAND)
                panel.SetSizer(vbSizer)
                
                self.nb2.AddPage(panel, text[num])
            else:
                tc = wx.TextCtrl(self.nb2, -1, text[num]+' would be shown here',
                                   style=wx.TE_MULTILINE)
                self.nb2.AddPage(tc, text[num])

        size = self.GetSize()

        self._mgr.AddPane(self.nb, aui.AuiPaneInfo().
                          Name("Search").Caption("Search").
                          CenterPane().MinimizeButton(True).MaximizeButton(True).
                          CloseButton(False).BestSize(wx.Size(size[0], size[1]/2)))

        self._mgr.AddPane(self.nb2, aui.AuiPaneInfo().
                          Name("DrinkDetails").Caption("Wine Details of: some info on the selected wine would be shown here, e.g. name, vintage").
                          Bottom().MinimizeButton(True).MaximizeButton(True).
                          CloseButton(False).BestSize(wx.Size(size[0], size[1]/2)))

        # create some toolbars
        tb1 = wx.ToolBar(self, -1, wx.DefaultPosition, wx.DefaultSize,
                         wx.TB_FLAT | wx.TB_NODIVIDER)
        tb1.SetToolBitmapSize(wx.Size(32,32))
        tb1.AddLabelTool(101, "Test", wx.ArtProvider_GetBitmap(wx.ART_ERROR))
        tb1.AddSeparator()
        tb1.AddLabelTool(102, "Test", wx.ArtProvider_GetBitmap(wx.ART_QUESTION))
        tb1.AddLabelTool(103, "Test", wx.ArtProvider_GetBitmap(wx.ART_INFORMATION))
        tb1.AddLabelTool(103, "Test", wx.ArtProvider_GetBitmap(wx.ART_WARNING))
        tb1.AddLabelTool(103, "Test", wx.ArtProvider_GetBitmap(wx.ART_MISSING_IMAGE))
        tb1.Realize()
        
        self._mgr.AddPane(tb1, aui.AuiPaneInfo().
                          Name("wineTB").Caption("Wine toolbar").
                          ToolbarPane().Top().
                          LeftDockable(False).RightDockable(False))

        self.CreateMenuBar()
        
        self._mgr.Update()
        
        self.GetViewsFromConfig()

    def CreateMenuBar(self):
        
        # create menu
        mb = wx.MenuBar()

        self._perspectives_menu = wx.Menu()
        menuItem = self._perspectives_menu.Append(-1, "Create Perspective")
        self.Bind(wx.EVT_MENU, self.OnCreatePerspective, menuItem)

        mb.Append(self._perspectives_menu, "&Perspectives")

        self.SetMenuBar(mb)

    def CreateConfig(self):
        sp = wx.StandardPaths.Get()
        userData = sp.GetUserDataDir()

        self.config = wx.FileConfig(appName='AuiTesting', vendorName='Whoever', localFilename=os.path.join(userData, "AuiViews"))

    def GetViewsFromConfig(self):
        if len(self._perspectives) == 0:
            self._perspectives_menu.AppendSeparator()

        # Creating menu items
        contFlag, itemVal, idxNext = self.config.GetFirstGroup()
        if itemVal:
            self.CreateViewMenuItem(itemVal)
        while contFlag:
            contFlag, itemVal, idxNext = self.config.GetNextGroup(idxNext)
            if itemVal:
                self.CreateViewMenuItem(itemVal)
        
    def CreateViewMenuItem(self, key):
        menuItem = self._perspectives_menu.Append(wx.NewId(), key)
        self.Bind(wx.EVT_MENU, self.OnRestorePerspective, menuItem)
        self._perspectives.append(key)

    def OnCreatePerspective(self, event):
        dlg = wx.TextEntryDialog(self, "Enter a name for the new perspective:", "AUI Test")

        dlg.SetValue("Perspective %u"%(len(self._perspectives) + 1))
        if dlg.ShowModal() != wx.ID_OK:
            return

        perspName = dlg.GetValue()
        if len(self._perspectives) == 0:
            self._perspectives_menu.AppendSeparator()
        
        self.config.Write(perspName+'/auiPers', self._mgr.SavePerspective())
        
        auibook = self.nb
        self.config.Write(perspName+'/Search', auibook.SavePerspective())

        auibook = self.nb2
        self.config.Write(perspName+'/DrinkDetails', auibook.SavePerspective())
        
        auibook = self.nb3
        self.config.Write(perspName+'/VintageDetails', auibook.SavePerspective())

        self.config.Flush()

        menuItem = self._perspectives_menu.Append(wx.ID_NEW, perspName)
        self.Bind(wx.EVT_MENU, self.OnRestorePerspective, menuItem)
        self._perspectives.append(perspName)

    def OnRestorePerspective(self, event):
        item = self.GetMenuBar().FindItemById(event.GetId())
        text = item.GetText()

        perspInfo = self.config.Read(text+'/auiPers')
        self._mgr.LoadPerspective(perspInfo)
        
        perspInfo = self.config.Read(text+'/Search')
        auibook = self.nb
        auibook.LoadPerspective(perspInfo)

        perspInfo = self.config.Read(text+'/VintageDetails')
        auibook = self.nb3
        auibook.LoadPerspective(perspInfo)

        perspInfo = self.config.Read(text+'/DrinkDetails')
        auibook = self.nb2
        auibook.LoadPerspective(perspInfo)
                
        self._mgr.Update()

if __name__ == '__main__':
    app = wx.PySimpleApp()
    frame = create(None)
    frame.Show()

    app.MainLoop()
