# -*- coding:utf-8 -*-

import wx
from log_utils import LogUtils


class TabKey(wx.Panel):
    def __init__(self, parent):
        super(TabKey, self).__init__(parent)

        gen_btn = wx.Button(self, label='Generate', size=(100, 35))
        dis_btn = wx.Button(self, label='Display', size=(100, 35))
        clear_btn = wx.Button(self, label='Reset', size=(100, 35))

        vbox = wx.BoxSizer(wx.VERTICAL)
        vbox.Add(gen_btn, proportion=1, flag=wx.EXPAND | wx.ALL, border=10)
        vbox.Add(dis_btn, proportion=1, flag=wx.EXPAND | wx.ALL, border=10)
        vbox.Add(clear_btn, proportion=1, flag=wx.EXPAND | wx.ALL, border=10)

        self.SetSizerAndFit(vbox)


class TabBlSetting(wx.Panel):
    def __init__(self, parent):
        super(TabBlSetting, self).__init__(parent)

        #------------------------------------
        # 子部件
        family_title = wx.StaticText(self, label='--family')
        self.family_cb = wx.ComboBox(self,
                                     choices=['NRF52'],
                                     value='NRF52',
                                     style=wx.CB_READONLY)

        boot_version_title = wx.StaticText(self, label='--bootloader-version')
        self.boot_version_text = wx.TextCtrl(self, value='1')

        bl_setting_version_title = wx.StaticText(self,
                                                 label='--bl-setting-version')
        self.bl_setting_version_cb = wx.ComboBox(self,
                                                 choices=['2'],
                                                 value='2',
                                                 style=wx.CB_READONLY)

        app_version_title = wx.StaticText(self, label='--application-version')
        self.app_version_text = wx.TextCtrl(self, value='10')

        app_title = wx.StaticText(self, label='--application')
        self.app_path = wx.TextCtrl(self, style=wx.TE_READONLY)
        self.app_load_btn = wx.Button(self, label='...')

        self.reset_btn = wx.Button(self, label='Reset')
        self.generator_btn = wx.Button(self, label='Generate')

        #------------------------------------
        # 布局
        self.gbs = wx.GridBagSizer(0, 0)
        self.gbs.Add(family_title,
                     pos=(0, 0),
                     flag=wx.EXPAND | wx.ALL,
                     border=5)
        self.gbs.Add(self.family_cb,
                     pos=(0, 1),
                     flag=wx.EXPAND | wx.ALL,
                     border=5)
        self.gbs.Add(boot_version_title,
                     pos=(1, 0),
                     flag=wx.EXPAND | wx.ALL,
                     border=5)
        self.gbs.Add(self.boot_version_text,
                     pos=(1, 1),
                     flag=wx.EXPAND | wx.ALL,
                     border=5)
        self.gbs.Add(bl_setting_version_title,
                     pos=(2, 0),
                     flag=wx.EXPAND | wx.ALL,
                     border=5)
        self.gbs.Add(self.bl_setting_version_cb,
                     pos=(2, 1),
                     flag=wx.EXPAND | wx.ALL,
                     border=5)
        self.gbs.Add(app_version_title,
                     pos=(3, 0),
                     flag=wx.EXPAND | wx.ALL,
                     border=5)
        self.gbs.Add(self.app_version_text,
                     pos=(3, 1),
                     flag=wx.EXPAND | wx.ALL,
                     border=5)
        self.gbs.Add(app_title, pos=(4, 0), flag=wx.EXPAND | wx.ALL, border=5)
        self.gbs.Add(self.app_path,
                     pos=(4, 1),
                     flag=wx.EXPAND | wx.ALL,
                     border=5)
        self.gbs.Add(self.app_load_btn,
                     pos=(4, 2),
                     flag=wx.EXPAND | wx.ALL,
                     border=5)

        self.gbs.Add(self.reset_btn,
                     pos=(5, 1),
                     flag=wx.EXPAND | wx.ALL,
                     border=5)
        self.gbs.Add(self.generator_btn,
                     pos=(5, 2),
                     flag=wx.EXPAND | wx.ALL,
                     border=5)

        #------------------------------------
        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(self.gbs)
        self.SetSizerAndFit(sizer)


class TabDfuPackage(wx.Panel):
    def __init__(self, parent):
        super(TabDfuPackage, self).__init__(parent)

        #------------------------------------
        # 子部件
        hw_version_title = wx.StaticText(self, label='--hw-version')
        self.hw_version_cb = wx.ComboBox(self,
                                         choices=['52'],
                                         value='52',
                                         style=wx.CB_READONLY)

        sd_seq_title = wx.StaticText(self, label='--sd-req')
        self.sd_seq_cb = wx.ComboBox(self,
                                     choices=['0x101'],
                                     value='0x101',
                                     style=wx.CB_READONLY)

        app_version_title = wx.StaticText(self, label='--application-version')
        self.app_version_text = wx.TextCtrl(self, value='1')

        app_title = wx.StaticText(self, label='--application')
        self.app_path = wx.TextCtrl(self, style=wx.TE_READONLY)
        self.app_load_btn = wx.Button(self, label='...')

        key_file_title = wx.StaticText(self, label='--key-file')
        self.key_file_path = wx.TextCtrl(self, style=wx.TE_READONLY)
        self.key_file_load_btn = wx.Button(self, label='...')

        self.reset_btn = wx.Button(self, label='Reset')
        self.generator_btn = wx.Button(self, label='Generate')

        #------------------------------------
        # 布局
        self.gbs = wx.GridBagSizer(0, 0)
        self.gbs.Add(hw_version_title,
                     pos=(0, 0),
                     flag=wx.EXPAND | wx.ALL,
                     border=5)
        self.gbs.Add(self.hw_version_cb,
                     pos=(0, 1),
                     flag=wx.EXPAND | wx.ALL,
                     border=5)
        self.gbs.Add(sd_seq_title,
                     pos=(1, 0),
                     flag=wx.EXPAND | wx.ALL,
                     border=5)
        self.gbs.Add(self.sd_seq_cb,
                     pos=(1, 1),
                     flag=wx.EXPAND | wx.ALL,
                     border=5)
        self.gbs.Add(app_version_title,
                     pos=(2, 0),
                     flag=wx.EXPAND | wx.ALL,
                     border=5)
        self.gbs.Add(self.app_version_text,
                     pos=(2, 1),
                     flag=wx.EXPAND | wx.ALL,
                     border=5)
        self.gbs.Add(app_title, pos=(3, 0), flag=wx.EXPAND | wx.ALL, border=5)
        self.gbs.Add(self.app_path,
                     pos=(3, 1),
                     flag=wx.EXPAND | wx.ALL,
                     border=5)
        self.gbs.Add(self.app_load_btn,
                     pos=(3, 2),
                     flag=wx.EXPAND | wx.ALL,
                     border=5)
        self.gbs.Add(key_file_title,
                     pos=(4, 0),
                     flag=wx.EXPAND | wx.ALL,
                     border=5)
        self.gbs.Add(self.key_file_path,
                     pos=(4, 1),
                     flag=wx.EXPAND | wx.ALL,
                     border=5)
        self.gbs.Add(self.key_file_load_btn,
                     pos=(4, 2),
                     flag=wx.EXPAND | wx.ALL,
                     border=5)
        self.gbs.Add(self.reset_btn,
                     pos=(5, 1),
                     flag=wx.EXPAND | wx.ALL,
                     border=5)
        self.gbs.Add(self.generator_btn,
                     pos=(5, 2),
                     flag=wx.EXPAND | wx.ALL,
                     border=5)

        #------------------------------------
        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(self.gbs)
        self.SetSizerAndFit(sizer)


class TabMergingFile(wx.Panel):
    def __init__(self, parent):
        super(TabMergingFile, self).__init__(parent)

        #------------------------------------
        # 子部件
        sd_title = wx.StaticText(self, label='softdevice')
        self.sd_path = wx.TextCtrl(self, style=wx.TE_READONLY)
        self.sd_load_btn = wx.Button(self, label='...')

        boot_title = wx.StaticText(self, label='bootloader')
        self.boot_path = wx.TextCtrl(self, style=wx.TE_READONLY)
        self.boot_load_btn = wx.Button(self, label='...')

        app_title = wx.StaticText(self, label='application')
        self.app_path = wx.TextCtrl(self, style=wx.TE_READONLY)
        self.app_load_btn = wx.Button(self, label='...')

        settings_title = wx.StaticText(self, label='settings')
        self.settings_path = wx.TextCtrl(self, style=wx.TE_READONLY)
        self.settings_load_btn = wx.Button(self, label='...')

        self.reset_btn = wx.Button(self, label='Reset')
        self.generate_btn = wx.Button(self, label='Generate')

        #------------------------------------
        # 布局
        self.gbs = wx.GridBagSizer(0, 0)
        self.gbs.Add(sd_title, pos=(0, 0), flag=wx.EXPAND | wx.ALL, border=5)
        self.gbs.Add(self.sd_path,
                     pos=(0, 1),
                     flag=wx.EXPAND | wx.ALL,
                     border=5)
        self.gbs.Add(self.sd_load_btn,
                     pos=(0, 2),
                     flag=wx.EXPAND | wx.ALL,
                     border=5)

        self.gbs.Add(boot_title, pos=(1, 0), flag=wx.EXPAND | wx.ALL, border=5)
        self.gbs.Add(self.boot_path,
                     pos=(1, 1),
                     flag=wx.EXPAND | wx.ALL,
                     border=5)
        self.gbs.Add(self.boot_load_btn,
                     pos=(1, 2),
                     flag=wx.EXPAND | wx.ALL,
                     border=5)

        self.gbs.Add(app_title, pos=(2, 0), flag=wx.EXPAND | wx.ALL, border=5)
        self.gbs.Add(self.app_path,
                     pos=(2, 1),
                     flag=wx.EXPAND | wx.ALL,
                     border=5)
        self.gbs.Add(self.app_load_btn,
                     pos=(2, 2),
                     flag=wx.EXPAND | wx.ALL,
                     border=5)

        self.gbs.Add(settings_title,
                     pos=(3, 0),
                     flag=wx.EXPAND | wx.ALL,
                     border=5)
        self.gbs.Add(self.settings_path,
                     pos=(3, 1),
                     flag=wx.EXPAND | wx.ALL,
                     border=5)
        self.gbs.Add(self.settings_load_btn,
                     pos=(3, 2),
                     flag=wx.EXPAND | wx.ALL,
                     border=5)

        self.gbs.Add(self.reset_btn,
                     pos=(4, 1),
                     flag=wx.EXPAND | wx.ALL,
                     border=5)
        self.gbs.Add(self.generate_btn,
                     pos=(4, 2),
                     flag=wx.EXPAND | wx.ALL,
                     border=5)

        #------------------------------------
        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(self.gbs)
        self.SetSizerAndFit(sizer)


class DfuImageGenerator(wx.Panel):
    '''DFU升级文件制作'''
    def __init__(self, parent):
        super(DfuImageGenerator, self).__init__(parent)

        # 实例化log模块
        self.logger = LogUtils.sharedInstance('3')

        self._init_ui()

    def _init_ui(self):
        ''''''
        # 定义选项卡
        nb = wx.Notebook(self)
        tab1 = TabKey(nb)
        tab2 = TabBlSetting(nb)
        tab3 = TabDfuPackage(nb)
        tab4 = TabMergingFile(nb)
        nb.AddPage(tab1, 'Cryptographic Key')
        nb.AddPage(tab2, 'BL setting')
        nb.AddPage(tab3, 'DFU packages')
        nb.AddPage(tab4, 'Merging file')

        vbox = wx.BoxSizer(wx.VERTICAL)
        vbox.Add(nb, flag=wx.EXPAND | wx.ALL)

        self.SetSizerAndFit(vbox)
        self.Centre()
