########################################################################
# Pi-HFIQ widgets module for Quisk.
#
# Original 'funcube_widget.py' for RS-HFIQ hardware by _____.
#
# Adapted into the "Pi-HFIQ" configuration by Rob French, KC4UPR.
#
# This file provides custom widgets for display in the Quisk window when
# used with an RS-HFIQ.  This was specifically defined for the "Pi-HFIQ"
# in which a Raspberry Pi is used with the RS-HFIQ, and Quisk has been
# modified with the GPIO Keyer patch.
#
# TODO as of 2020-04-21:
# (1) Make keyer parameter(s) (mode, speed) saved to a config file auto-
#     matically on exit.
# (2) Convert the horizontal sliders to popup sliders.  This is mainly 
#     to fix the goofy cell sizing I get with the GridBagSizer.
#
########################################################################

from __future__ import print_function
import math
import wx

from quisk_widgets import *

import _quisk as QS

class BottomWidgets:    # Add extra widgets to the bottom of the screen
    def __init__(self, app, hardware, conf, frame, gbs, vertBox):
        self.config = conf
        self.hardware = hardware
        self.application = app
        self.start_row = app.widget_row             # The first available row
        self.start_col = app.button_start_col       # The start of the button columns

        self.num_rows_added = 1
        start_row = self.start_row    
    
        bt = app.QuiskCycleCheckbutton(frame, self.OnKeyerMode, hardware.keyer_mode_list)
        b = WrapSlider(bt, self.OnKeyerWPM, slider_value=self.hardware.keyer_wpm, slider_min=1, slider_max=60, display=True)
        bw, bh = b.GetMinSize()
        bt.SetIndex(self.hardware.keyer_mode)
        gbs.Add(b, (start_row, self.start_col), (1, 2), flag=wx.EXPAND)
        
        #b = app.SliderBoxHH(frame, '%d wpm', 15, 1, 60, self.OnKeyerWPM, True)
        #b.SetValue(self.hardware.keyer_wpm)
        #gbs.Add(b, (start_row, self.start_col+2), (1, 3), flag=wx.EXPAND)
        
        b = app.SliderBoxHH(frame, 'Output: %d%%', conf.mic_out_volume, 0, 100, self.OnOutputLevel, True)
        b.SetValue(70)
        gbs.Add(b, (start_row, self.start_col+2), (1, 3), flag=wx.EXPAND)

        self.status_text = app.QuiskText(frame, 'Rig: 100C, CPU: 100C, Proc: 100%, Mem: 100%', bh, wx.ALIGN_CENTER, True)
        gbs.Add(self.status_text, (start_row, self.start_col+5), (1, 4), flag=wx.EXPAND)

    def OnKeyerMode(self, event):
        index = event.GetEventObject().index
        self.hardware.SetKeyerMode(index)

    def OnKeyerWPM(self, event):
        btn = event.GetEventObject()
        value = btn.slider_value
        print('Keyer WPM: ', value)
        self.hardware.SetKeyerWPM(value)

    def OnOutputLevel(self, event):
        btn = event.GetEventObject()
        value = btn.GetValue()
        print('Output Level: ', value)
        QS.set_mic_out_volume(value)

    def UpdateText(self, text):
        self.status_text.SetLabel(text)
    
    # lna = app.QuiskCheckbutton(frame, self.OnLNA, 'LNA')
    # lna.SetValue( self.hardware.lna )
    # gbs.Add(lna, (start_row, self.start_col), (1, 2), flag=wx.EXPAND)

    # mixer = app.QuiskCheckbutton(frame, self.OnMixer, 'Mixer gain')
    # mixer.SetValue( self.hardware.mixer )
    # gbs.Add(mixer, (start_row, self.start_col+2), (1, 2), flag=wx.EXPAND)
  
    # gain = app.SliderBoxHH(frame, 'If gain %d dB', 0, 0, 59, self.OnIF, True)
    # gain.SetValue(self.hardware.ifgain)
    # gbs.Add(gain,(start_row, self.start_col+4),(1,4), flag=wx.EXPAND)

  # def OnLNA(self, event):
    # btn = event.GetEventObject()
    # if btn.GetValue() :
	# value = 1
    # else :
	# value = 0
    # print('LNA: ',value)
    # self.hardware.SetLNA(value)

  # def OnMixer(self, event):
    # btn = event.GetEventObject()
    # if btn.GetValue() :
	# value = 1
    # else :
	# value = 0
    # print('Mixer: ',value)
    # self.hardware.SetMixer(value)

  # def OnIF(self,event) :
    # btn = event.GetEventObject()
    # value = btn.GetValue()
    # print('IF: ', value )
    # self.hardware.SetIfGain(value)

