#!/usr/bin/env python
# -*- encoding: utf-8 -*-
import wx
# from configobj import ConfigObj
from myconfig import MyConfig
from myconfig import YAML_FILE_NAME

class UI_Plot_Setting(wx.Dialog):
    """ 
    @description    : 绘图 配置 
    """ 
    def __init__(self, parent, id):
        # 串口页面配置
        super(UI_Plot_Setting, self).__init__( parent ,id = id )
        self.config = MyConfig()
        self.InitUI()
        self.SetSize((400, 400))
        self.SetTitle(" 绘图 序列号 ")

    def InitUI(self):
        self.config_yml = self.config.read_rtv_yaml()
 
        self.panel = wx.Panel(self)
        self.vbox = wx.BoxSizer(wx.VERTICAL)

        self.sb = wx.StaticBox(self.panel, label='指定绘图设置 ')
        self.sbs = wx.StaticBoxSizer(self.sb, orient=wx.VERTICAL)

        self.hbox1 = wx.BoxSizer(wx.HORIZONTAL)
        self.staticText1 = wx.StaticText(self.panel, label='起始行号 ',size=(60, -1), style=wx.ALIGN_CENTRE_VERTICAL )
        self.textCtrl1   = wx.TextCtrl(self.panel, value="",size=(250,25)  )
        tmp = self.get_str_from_config(   "LineBegin" )
        if tmp is not None:       
            self.textCtrl1.SetValue(tmp)
        self.hbox1.Add(self.staticText1, flag=wx.TOP|wx.LEFT, border=5)
        self.hbox1.Add(self.textCtrl1, flag=wx.TOP|wx.LEFT, border=5)
        self.sbs.Add(self.hbox1, flag=wx.TOP|wx.LEFT, border= 5)

        self.hbox2 = wx.BoxSizer(wx.HORIZONTAL)
        self.staticText2 = wx.StaticText(self.panel, label='间隔行号: ' ,size=(60, -1))
        # self.staticText2_1 = wx.StaticText(self.panel, label='分钟 ' ,size=(60, -1))
        self.textCtrl2   = wx.TextCtrl(self.panel, value="",size=(250,25))
        tmp = self.get_str_from_config(   "LineInterval" )
        if tmp is not None:       
            self.textCtrl2.SetValue(tmp)
        self.hbox2.Add(self.staticText2, flag=wx.TOP|wx.LEFT,  border=5)
        self.hbox2.Add(self.textCtrl2,flag=wx.TOP|wx.LEFT, border=5)
        # self.hbox2.Add(self.staticText2_1, flag=wx.TOP|wx.LEFT,  border=5)
        self.sbs.Add(self.hbox2, flag=wx.TOP|wx.LEFT, border=5)

        self.panel.SetSizer(self.sbs)

        self.hbox_0 = wx.BoxSizer(wx.HORIZONTAL)
        self.okButton = wx.Button(self, label=u'保存配置')
        self.closeButton = wx.Button(self, label='Close')
        self.hbox_0.Add(self.okButton)
        self.hbox_0.Add(self.closeButton, flag = wx.LEFT, border=5)

        # 添加 vbox 到panel
        self.vbox.Add(self.panel, proportion=1,
                flag=wx.ALL | wx.EXPAND, border=5)
        
        self.vbox.Add(self.hbox_0, flag=wx.ALIGN_CENTER | wx.TOP | wx.BOTTOM, border=10)

        self.SetSizer(self.vbox)

        self.okButton.Bind(wx.EVT_BUTTON, self.OnSave)
        self.closeButton.Bind(wx.EVT_BUTTON, self.OnClose)
 
    def get_str_from_config( self, key ):
        return str(self.config_yml[key])
        pass   

    def set_config_by_key_val( self,  key, val ):
        # print(f" {key}  current type {type( self.config_yml[section][key] )}")
        if type( self.config_yml [key] ) == int:
            self.config_yml [key] = int(val)
        elif type( self.config_yml [key] ) == float:
            self.config_yml [key] = float(val)
        else:
            self.config_yml[key] = val
        pass 
    
    
    def saveData(self, e):
        pass   

    def OnClose(self, e):

        self.Destroy()

    def OnSave(self, e):
        success = True
        # 赋值字典，写入文件   
        self.set_config_by_key_val( "LineBegin",self.textCtrl1.GetValue( ))
        self.set_config_by_key_val( "LineInterval",self.textCtrl2.GetValue( ))
        self.config.write_rtv_yaml(self.config_yml)
        del self.config
        if success:
            self.EndModal(wx.ID_OK)