#!/usr/bin/env python

import wx
import os
from log_utils import LogUtils
from programmer import Programmer
from dfu_image_generator import DfuImageGenerator
from settings import Settings
from about import About
from wx.lib.embeddedimage import PyEmbeddedImage

title_icon = PyEmbeddedImage(
    b'iVBORw0KGgoAAAANSUhEUgAAAMgAAADICAYAAACtWK6eAAAAAXNSR0IArs4c6QAAC7VJREFU'
    b'eF7tnW2MVFcZx58z020x1nZnLVljTElMaMSQLtZYEmNaYHeJxGgaa2eAbUuUztT2Q1HbqAjf'
    b'pC8aS2PTGJmFGikLzDRVYmMkZXlr/EBVotgGiHyiJURCw6xoaIGde8ySvkXqMvfOnDPnPvfH'
    b'V+59Xn7/+5uZe3cWjIjkhD8QgMCHEjAIwpUBgf9PAEG4OiAwDQEE4fKAAIJwDUAgGQHeQZJx'
    b'46yMEECQjATNmskIIEgybpyVEQIIkpGgWTMZAQRJxo2zMkIAQTISNGsmI4AgybhxVkYIIEhG'
    b'gmbNZAQyK8gNG+7un7x+xrCY3M1GpN8a229Erk6GUddZVuSCseaUFTmVE/unC+ff3vefe597'
    b'U9eWrW2TOUGuHysvyl1lHhIjX20NEUddImDlxWjSPv2vkdE9WSKSKUF6t5erJmdWZingTu9q'
    b'I7tpYulopdN1Q62XGUEKtcrvxciXQw0iXXPZvzeKo59L18zJps2EIIV6pZkMD2dNR6BRrOa1'
    b'E1IvSKFe/quIuVl7kF3Zz8rORqn6la709tRUtSDcc7i/irTfk6gV5NLTqh6zy/0lQofooh3W'
    b'+nRLrSCFWmUHj3I9yWvlxUapeoenbl7bqBTk2s333NAz4yOnvJLMeLN849wn37x/izrmKgXp'
    b'q5W/YY2pZfya9bq+jaIVE0s3bvHa1EMzlYL01io/NUYe9sCPFu8QsFaenChVv68NiEpBCrXK'
    b'r8XI3drCCnofK1sapeqKoGdMMJxKQXrr5Z1GzHA8HnZ/vOO1H21uj7OhFbtrojiq7psKKgUp'
    b'1Mt7ROIFLGL3N4qji+JcFFqPhd/7ySLIeywQ5F0UCIIgH/LijyAI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og3549KI/NK6q6mH786g556ugfVO2EIF2Kc0H/HPnNbd/pUnc3'
    b'be98+eey99RhN8WDrMo7iNNYRuffJ3fe+AWnPXwVX/faDll/JEvvHlNkEcT59XXmrg3Oe/ho'
    b'0Pf8/T7aBNYDQbwE8r05S2Tt3Du89Op0kxde/7OUX9nY6bIpqYcg3oJa2P9ZmVeYJXN7PyUz'
    b'Z1znrW+SRifPNeRvjeNy9OxJ2XfqSJISSs5BECVBsoYbAgjihitVlRBAECVBsoYbAgjihitV'
    b'lRBAECVBsoYbAgjihitVlRBAECVBsoYbAgjihitVlRBAECVBsoYbAgjihitVlRBAECVBsoYb'
    b'AgjihitVlRBAECVBsoYbAgjihitVlRBAECVBsoYbAgjihitVlRBAECVBsoYbAgjihitVlRBA'
    b'ECVBsoYbAgjihitVlRBAECVBsoYbAgjihitVlRBAECVBsoYbAgjihitVlRBAECVBsoYbAgji'
    b'hitVlRBAECVBsoYbAgjihitVlRBAECVBsoYbAgjihitVlRBAECVBsoYbAgjihitVlRBAECVB'
    b'soYbAgjihitVlRBAECVBsoYbAgjihitVlRBAECVBsoYbAgjihitVlRBAECVBsoYbAgjihitV'
    b'lRBAECVBsoYbAgjihitVlRBAECVBsoYbAgjihitVlRBAECVBsoYbAgjihitVlRBAECVBsoYb'
    b'AgjihitVlRBAECVBsoYbAgjihitVlRBAECVBsoYbAgjihitVlRBAECVBsoYbAj4FqZU3izEj'
    b'bhahKgQcELC23iiNLotb2YhILu5Jvdsqq0xe1sc9j+Mh0DUCNlrTKG18Im7/RIL0jZWHbY/Z'
    b'GbcZx0OgWwTMZHPJmeWbXorbP5EgU00KtcqYGFkatyHHQ6ALBNY2itXHk/RNLMglSeqVZpKm'
    b'nAMBnwQaxWo+ab+2BHlHktUisi7pAJwHAWcErGxvlKptPUxqW5Cp5fq2rlxs8+YWETMgRvqd'
    b'LUxhCFyJgJUTNjIHc5E9fGZkdNeVDr/S33dEkCs14e8hkFYCCJLW5JjbCwEE8YKZJmklgCBp'
    b'TY65vRBAEC+YaZJWAgiS1uSY2wsBBPGCmSZpJYAgaU2Oub0QQBAvmGmSVgIIktbkmNsLAQTx'
    b'gpkmaSWAIGlNjrm9EEAQL5hpklYC/wW+p4KkFYXPKQAAAABJRU5ErkJggg==')


class TabTwo(wx.Panel):
    def __init__(self, parent):
        super(TabTwo, self).__init__(parent)
        text = wx.TextCtrl(self, style=wx.TE_MULTILINE, size=(250, 150))


class NrfxTool(wx.Frame):
    """hold all widgets"""
    def __init__(self, parent):
        super(NrfxTool, self).__init__(parent,
                                       style=wx.DEFAULT_FRAME_STYLE
                                       & ~(wx.RESIZE_BORDER | wx.MAXIMIZE_BOX))

        self.settings = Settings()

        self.SetIcon(title_icon.getIcon())
        self.SetTitle(self.settings.title)
        self.init_ui()
        self.welcome_message()

    def init_ui(self):
        vbox = wx.BoxSizer(wx.VERTICAL)

        #----------------------------------------
        # 设置区域
        self.nb = wx.Notebook(self)
        self.nb.AddPage(Programmer(self.nb), "Programmer")
        self.nb.AddPage(DfuImageGenerator(self.nb), "DFU Image Generater")
        self.nb.AddPage(About(self.nb), "About")

        #----------------------------------------
        # log显示区域
        btn_hbox = wx.BoxSizer(wx.HORIZONTAL)
        self.log_clear_btn = wx.Button(self, label='CLEAR LOG')
        self.log_open_btn = wx.Button(self, label='OPEN LOG FILE')
        self.log_clear_btn.Bind(wx.EVT_BUTTON, self.log_clear_btn_handler)
        self.log_open_btn.Bind(wx.EVT_BUTTON, self.log_open_btn_handler)
        btn_hbox.Add(self.log_clear_btn, flag=wx.EXPAND | wx.ALL)
        btn_hbox.Add(self.log_open_btn, flag=wx.EXPAND | wx.ALL)

        self.log_text = wx.TextCtrl(self,
                                    style=wx.TE_MULTILINE | wx.TE_READONLY
                                    | wx.VSCROLL)

        vbox.Add(self.nb)
        vbox.Add(btn_hbox)
        vbox.Add(self.log_text, proportion=1, flag=wx.EXPAND | wx.ALL)
        self.SetSizerAndFit(vbox)

        self.SetSize(self.settings.size)
        self.Centre()
        self.Show(True)

        # 实例化log模块，绑定输出到log显示区域
        self.logger = LogUtils.sharedInstance('1')
        LogUtils.sharedInstance('nrfx_tool').set_ctrl_to_logging(self.log_text)
        self.logger.set_logger('nrfx_tool', 'logs')

        #print(self.nb.GetSize())

    def log_clear_btn_handler(self, e):
        self.log_text.Clear()

    def log_open_btn_handler(self, e):
        log_file = self.logger.get_current_log_file()
        print('Open log file: %s' % (log_file))
        os.startfile(str(log_file))

    def welcome_message(self):
        self.logger.info('Check for updates: %s' % self.settings.url)


def main():
    app = wx.App()
    frame = NrfxTool(None)
    app.MainLoop()


if __name__ == "__main__":
    main()
