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

class UISensor(wx.Dialog):
    """ 
    @description    : Sensor 配置 
    """ 
    def __init__(self, parent, id, did):
        # 参数调入后初始化
        self.did = did
        super(UISensor, self).__init__( parent, id=id   )
        self.config = MyConfig()
        self.InitUI()
        self.SetSize((400, 400))
        self.SetTitle(" 设置页面  串口 采集间隔 ")

    def InitUI(self):

        self.config_sensor = self.config.read_yaml() 
        # self.sensor = self.config_sensor.get(self.did)
        # print(self.config_sensor)
        # print(self.sensor)
 
        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='Lsky: ',size=(60, -1), style=wx.ALIGN_CENTRE_VERTICAL )
        self.textCtrl1   = wx.TextCtrl(self.panel, value="",size=(250,25)  )
        tmp = self.getSnByDidAndFunc("Lsky",self.did)
        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='Esky: ' ,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.getSnByDidAndFunc("Esky",self.did)
        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.hbox3 = wx.BoxSizer(wx.HORIZONTAL)
        self.staticText3 = wx.StaticText(self.panel, label='Lwater: ' ,size=(60, -1))
        # self.staticText3_1 = wx.StaticText(self.panel, label='分钟 ' ,size=(60, -1))
        self.textCtrl3   = wx.TextCtrl(self.panel, value="",size=(250,25))
        tmp = self.getSnByDidAndFunc("Lwater",self.did)
        if tmp is not None:       
            self.textCtrl3.SetValue(tmp)
        self.hbox3.Add(self.staticText3, flag=wx.TOP|wx.LEFT,  border=5)
        self.hbox3.Add(self.textCtrl3,flag=wx.TOP|wx.LEFT, border=5)
        # self.hbox3.Add(self.staticText3_1, flag=wx.TOP|wx.LEFT,  border=5)
        self.sbs.Add(self.hbox3, 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 getSnByDidAndFunc(self,func,did):
        temp:dict = self.config_sensor.get(did)
        for v in temp.values():
            if v["FUNC"] == func:
                return v["SN"] 
        return None
        pass   

    def setSnByDidAndFunc(self,func,did,val):
        temp:dict = self.config_sensor.get(did)
        for k,v in temp.items():
            if v["FUNC"] == func:
                temp[k]['SN'] =val
                self.config_sensor.update({did:temp})
 
        pass 
    
    def saveData(self, e):
        pass   

    def OnClose(self, e):

        self.Destroy()

    def OnSave(self, e):
        success = True
        # 赋值字典，写入文件   
        self.setSnByDidAndFunc('Lsky',self.did,self.textCtrl1.GetValue( ))
        self.setSnByDidAndFunc('Esky',self.did,self.textCtrl2.GetValue( ))
        self.setSnByDidAndFunc('Lwater',self.did,self.textCtrl3.GetValue( )) 
        print(self.textCtrl3.GetValue( )) 

        print(self.config_sensor)
        self.config.write_yaml(self.config_sensor)
        del self.config

        if success:
            self.EndModal(wx.ID_OK)