#!/usr/bin/python
#======================================================================
#
# Project	 : hpp_IOStressTest
# File 		 : IOST.py
# Date		 : Sep 21, 2016
# Author	 : HuuHoang Nguyen
# Contact	 : hhnguyen@apm.com
#            : hoangnh.hpp@gmail.com
# License    : MIT License
# Copyright  : 2016
# Description: The hpp_IOStressTest is under the MIT License, a copy of license which may be found in LICENSE
#
#======================================================================

import io
import os
import operator
import sys
import base64
import time
# import timpfile

from IOST_Prepare import IOST_Prepare
from IOST_Config import *
from IOST_Main import *


def hello():
    print "Hello World!!!"

class IOST:
    "This is main class "
    # def  __init__(self, glade_filename="IOST_UI.glade", window_name="IOST_WMain"):
    def  __init__(self, glade_filename="IOST_UI_02.glade", window_name="IOST_WMain"):
        "The function is a main function to start program IOST"
        # global IOST_WMain_Builder

        if os.path.isfile(glade_filename):
            self.IOST_GladeFile = glade_filename
        else:
            self.IOST_GladeDir = os.path.dirname(sys.argv[0])
            self.IOST_GladeFile = os.path.join(self.IOST_GladeDir, glade_filename)

        #Get WMain from glade file
        self.IOST_GetWMain(self.IOST_GladeFile, window_name)
        self.IOST_GetDialogAbout(self.IOST_GladeFile, "aboutdialog_HelpAbout")
        self.IOST_GetLoadConfigFile(self.IOST_GladeFile, "filechooserdialog_LoadConfigFile")

        self.IOST_ShowWMain()


    #===========================================================================================
    def IOST_GetWMain(self, glade_filename, window_name):
        self.IOST_WMain_Builder = gtk.Builder()
        self.IOST_WMain_Builder.add_from_file(glade_filename)

        self.IOST_WMain_Builder.connect_signals(self)
        self.IOST_WMain_Obj = self.IOST_WMain_Builder.get_object(window_name)        
        
    def IOST_ShowWMain(self):
        self.IOST_WMain_Obj.show()    

    def on_IOST_WMain_destroy(self, object, data=None):
        "Exit with detroy"
        gtk.main_quit()
    
    def on_imagemenuitem_FileQuit_activate(self, object, data=None):
        "Exit with from File -> Exit"
        gtk.main_quit()

    def delete_event(self, widget, data=None):
        "Exit by delete even"
        gtk.main_quit()

    
    #===========================================================================================
    def on_button_Run_clicked(self, object, data=None):
        "Control to Run button"

    # --------------------
    #    Run Button
    # --------------------
    def on_button_SaveConfig_clicked(self, object, data=None):
        "Control to Save Condfig button"
    
    # --------------------
    #    Cancel Button
    # --------------------
    def on_button_Cancel_clicked(self, object, data=None):
        "Control to Cancel button"

    # --------------------
    #    I2C
    # --------------------
    def on_button_ConfigI2C0_clicked(self, object, data=None):
        "Control to ConfigI2C-0 button "

    def on_button_ConfigI2C1_clicked(self, object, data=None):
        "Control to ConfigI2C-1 button "

    def on_button_ConfigI2C2_clicked(self, object, data=None):
        "Control to ConfigI2C-2 button "

    def on_button_ConfigI2C3_clicked(self, object, data=None):
        "Control to ConfigI2C-3 button "

    def on_button_ConfigI2C4_clicked(self, object, data=None):
        "Control to ConfigI2C-4 button "

    def on_button_ConfigI2C5_clicked(self, object, data=None):
        "Control to ConfigI2C-5 button "

    # --------------------
    #    USB
    # --------------------
    def on_button_ConfigUSB0_clicked(self, object, data=None):
        "Control to ConfigUSB-0 button "

    def on_button_ConfigUSB1_clicked(self, object, data=None):
        "Control to ConfigUSB-1 button "

    def on_button_ConfigUSB2_clicked(self, object, data=None):
        "Control to ConfigUSB-2 button "    

    # --------------------
    #    SATA
    # --------------------
    def on_button_ConfigSATA0_clicked(self, object, data=None):
        "Control to ConfigSATA-0 button "

    def on_button_ConfigSATA1_clicked(self, object, data=None):
        "Control to ConfigSATA-1 button "

    def on_button_ConfigSATA2_clicked(self, object, data=None):
        "Control to ConfigSATA-2 button "

    def on_button_ConfigSATA3_clicked(self, object, data=None):
        "Control to ConfigSATA-3 button "

    # --------------------
    #    UART
    # --------------------  
    def on_button_ConfigUART0_clicked(self, object, data=None):
        "Control to ConfigUART-0 button "

    def on_button_ConfigUART1_clicked(self, object, data=None):
        "Control to ConfigUART-1 button "

    def on_button_ConfigUART2_clicked(self, object, data=None):
        "Control to ConfigUART-2 button "

    def on_button_ConfigUART3_clicked(self, object, data=None):
        "Control to ConfigUART-3 button "

    def on_button_ConfigUART4_clicked(self, object, data=None):
        "Control to ConfigUART-4 button "

    # --------------------
    #    SPI
    # --------------------
    def on_button_ConfigSPI0_clicked(self, object, data=None):
        "Control to ConfigSPI-0 button "

    def on_button_ConfigSPI1_clicked(self, object, data=None):
        "Control to ConfigSPI-1 button "

    def on_button_ConfigSPI2_clicked(self, object, data=None):
        "Control to ConfigSPI-2 button "

    # --------------------
    #    PCIE
    # --------------------
    def on_button_ConfigPCIE0_clicked(self, object, data=None):
        "Control to ConfigPCIE-0 button "

    def on_button_ConfigPCIE1_clicked(self, object, data=None):
        "Control to ConfigPCIE-2 button "

    def on_button_ConfigPCIE2_clicked(self, object, data=None):
        "Control to ConfigPCIE-2 button "

    def on_button_ConfigPCIE3_clicked(self, object, data=None):
        "Control to ConfigPCIE-3 button "

    def on_button_ConfigRGMII_clicked(self, object, data=None):
        "Control to Config Rgmii button "
    
    def on_button_SetupEmail_clicked(self, object, data=None):
        "Control to Config SetupEmail button "


    
    #===========================================================================================
    def IOST_GetLoadConfigFile(self, glade_filename, window_name):
        if not self.IOST_WMain_Builder:
            self.IOST_WMain_Builder = gtk.Builder()
            self.IOST_WMain_Builder.add_from_file(glade_filename)
            self.IOST_WMain_Builder.connect_signals(self)
        
        self.IOST_LoadConfigFile_Obj = self.IOST_WMain_Builder.get_object(window_name)
        self.IOST_LoadConfigFile_Obj.entry_PathConfigFile = self.IOST_WMain_Builder.get_object("entry_PathConfigFile")
        
    def on_button_SelectConfigFile_clicked(self, object, data=None):
        if not self.IOST_LoadConfigFile_Obj:
            IOST_GetLoadConfigFile(self.IOST_GladeFile, "filechooserdialog_LoadConfigFile")
        
        self.IOST_LoadConfigFile_Obj.run()

    def on_button_LoadConfigFile_OK_clicked(self, object, data=None):
        "Get file name and exit dialog window"
        self.IOST_LoadConfigFile_Obj.ConfigFile = self.IOST_LoadConfigFile_Obj.get_filename()
        self.IOST_LoadConfigFile_Obj.entry_PathConfigFile.set_text(self.IOST_LoadConfigFile_Obj.ConfigFile)
        self.IOST_LoadConfigFile_Obj.hide()
        
        print "The file name choise is: %s " %self.IOST_LoadConfigFile_Obj.ConfigFile
    def on_button_LoadConfigFile_Cancel_clicked(self, object, data=None):
        self.IOST_LoadConfigFile_Obj.hide()
        

    #===========================================================================================
    def IOST_GetDialogAbout(self, glade_filename, window_name):
        "This is a function get object of Dialog Help -> About window"
        if not self.IOST_WMain_Builder:
            self.IOST_WMain_Builder = gtk.Builder()
            self.IOST_WMain_Builder.add_from_file(glade_filename)
            self.IOST_WMain_Builder.connect_signals(self)
        else:
            self.IOST_DialogAbout_Obj = self.IOST_WMain_Builder.get_object(window_name)

    #This is a some function to Dialog About from menu Help
    def on_imagemenuitem_HelpAbout_activate(self, object, data=None):
        self.IOST_DialogAbout_Obj.run()
        self.IOST_DialogAbout_Obj.hide()
    
    def on_aboutdialog_HelpAbout_activate_link(self, object, data=None):
        self.IOST_DialogAbout_Obj.hide()
    
   
#===========================================================================================
if __name__=="__main__":
    main = IOST()
    gtk.main()

