#!/usr/bin/python
#======================================================================
#
# Project	 : hpp_IOStressTest
# File 		 : IOST_Basic.py
# Date		 : Oct 17, 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 re
import operator
import sys
import base64
import time


from IOST_Prepare import IOST_Prepare
from IOST_Config  import *

import gtk
import gtk.glade
import gobject

class IOST_Basic(IOST_Prepare):
    def __init__(self):
        """
        """       

    #-----------------------------------------------------------------------
    def MsgBox(self, text, parent=None):
        ""
        global IOST_Config
        MsgBox = gtk.MessageDialog(parent, gtk.DIALOG_MODAL, gtk.MESSAGE_ERROR, gtk.BUTTONS_OK, text)
        MsgBox.set_icon_from_file(IOST_Config["IconPath"])
        MsgBox.run()
        MsgBox.destroy()

    #-----------------------------------------------------------------------
    def MsgConfirm(self, text=""):
        ""
        global IOST_Config
        MsgBox=gtk.MessageDialog(None, gtk.DIALOG_MODAL, gtk.MESSAGE_QUESTION, gtk.BUTTONS_OK_CANCEL, text)
        # MsgBox.set_icon(IOST_Config["IconPath"])
        Response = MsgBox.run()
        MsgBox.destroy()
        return Response

    #-----------------------------------------------------------------------
    def InputBox(self, title, text, default='', password=False):
        ""
        global IOST_Config
        MsgBox = EntryDialog(title, text, default, mask=password)
        # MsgBox.set_icon(IOST_Config["IconPath"])
        if MsgBox.run() == gtk.RESPONSE_OK:
            Response = MsgBox.value
        else:
            Response = None
        MsgBox.destroy()
        return Response

    #-----------------------------------------------------------------------
    def ShowFontDialog(self, parent, title, button):
        ""
        Dlg = gtk.FileChooserDialog(title=title, parent=parent, action=action)
        Dlg.add_button(gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL)

        Dlg.add_button(gtk.STOCK_SAVE if action==gtk.FILE_CHOOSER_ACTION_SAVE else gtk.STOCK_OPEN, gtk.RESPONSE_OK)
        Dlg.set_do_overwrite_confirmation(True)
        if not hasattr(parent,'lastPath'):
            parent.lastPath = os.path.expanduser("~")
        Dlg.set_current_folder( parent.lastPath )

        if Dlg.run() == gtk.RESPONSE_OK:
            filename = dlg.get_filename()
            parent.lastPath = os.path.dirname(filename)
        else:
            filename = None

        Dlg.destroy()
        return filename
    
    #-----------------------------------------------------------------------
    def GetKeyName(self, event):
        ""
        name = ""
        if event.state & 4:
            name = name + "CTRL+"
        if event.state & 1:
            name = name + "SHIFT+"
        if event.state & 8:
            name = name + "ALT+"
        if event.state & 67108864:
            name = name + "SUPER+"
        return name + gtk.gdk.keyval_name(event.keyval).upper()

    #-----------------------------------------------------------------------
    def GetUserName():
        return os.getenv('USER') or os.getenv('LOGNAME') or os.getenv('USERNAME')

    # def IOST_Basic_GetPassword():
    #     return get_username() + enc_passwd

    #-----------------------------------------------------------------------
    def ReadFileJSON(file_name=""):
        with open(file_name) as ReadFileName:
            ReadData = json.load(ReadFileName, object_pairs_hook=OrderedDict)
        # if IOST_Config_DebugEnable:
        #     pprint (IOST_Config_ReadData)
    
        return ReadData
    
    #-----------------------------------------------------------------------
    def WriteFileJSON(file_name="", data=None):
        with open(file_name, 'w') as WriteFileName:
            json.dump(data, WriteFileName,indent=4)

# IOST_Basic = IOST_Basic()