########################################################################
#
#  Copyright (C) 2009, CZ.NIC, z.s.p.o.
# 
#  This file is part of dnstester.
#
#  dnstester is free software: you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation, either version 3 of the License, or
#  (at your option) any later version.
#
#  dnstester is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
########################################################################

import pygtk
pygtk.require("2.0")
import gtk, gobject

import combo
import lang
import devices
import misc

class DeviceFormControls():

    cb_devcategory = None
    lcb_devcategory = None

    cb_manufacturer = None
    lcb_manufacturer = None
    entry_manufacturer = None

    cb_model = None
    lcb_model = None
    entry_model = None

    entry_firmware = None

    vbox = None

    def __init__(self, parent):

        self.parent = parent
        vbox = gtk.VBox()

        # create labels
        lb_manufacturer = gtk.Label()
        lang.bind(lb_manufacturer, 'manufacturer')
        lb_manufacturer.set_alignment(0, 0.5)
        lb_model = gtk.Label()
        lang.bind(lb_model, 'model')
        lb_model.set_alignment(0, 0.5)
        lb_firmware = gtk.Label()
        lang.bind(lb_firmware, 'firmware')
        lb_firmware.set_alignment(0, 0.5)

        #create entries

        # model combo & entry

        self.cb_model = gtk.combo_box_new_text()
        self.lcb_model = combo.LangCombo(self.cb_model, [])
        lang.bind(self.lcb_model, None)
        self.cb_model.connect("changed", self.on_model_change)
        self.entry_model = gtk.Entry()
        self.entry_model.connect("changed", self.on_entry_model_change)

        # manufacturer combo & entry
        manulist = []
        for m in devices.devs: manulist.append(m['manufacturer'])

        self.cb_manufacturer = gtk.combo_box_new_text()
        self.lcb_manufacturer = combo.LangCombo(self.cb_manufacturer, manulist)
        lang.bind(self.lcb_manufacturer, None)
        self.cb_manufacturer.connect("changed", self.on_manufacturer_change)
        self.entry_manufacturer = gtk.Entry()
        self.entry_manufacturer.connect("changed", self.on_entry_manufacturer_change)

        # firmware entry

        self.entry_firmware = gtk.Entry()

        self.lcb_manufacturer.refill(manulist)
        self.guess_manufacturer(devices.devs)

        # fill the form

        hw_form = gtk.Table(3, 3, True)
        hw_form.set_col_spacing(1, 20)

        hw_form.attach(lb_manufacturer, 0, 1, 0, 1)
        hw_form.attach(self.cb_manufacturer, 1, 2, 0, 1)
        hw_form.attach(self.entry_manufacturer, 2, 3, 0, 1)

        hw_form.attach(lb_model, 0, 1, 1, 2)
        hw_form.attach(self.cb_model, 1, 2, 1, 2)
        hw_form.attach(self.entry_model, 2, 3, 1, 2)

        hw_form.attach(lb_firmware, 0, 1, 2, 3)
        hw_form.attach(self.entry_firmware, 1, 2, 2, 3)

        vbox.pack_start(hw_form, False, False)
        vbox.show_all()
        self.vbox = vbox


    def guess_manufacturer(self, manufacturers):
        mac = self.parent.parent.netinfo.mac
        if not mac: return
        mac = ''.join(mac.split(':'))
        mac = mac.upper()

        for i in range(len(manufacturers)):
            m = manufacturers[i]
            maclist =  m['maclist']
            for prefix in maclist:
                if mac.startswith(prefix.upper()):
                    # found a match
                    self.lcb_manufacturer.set_active(i)
                    return


    def on_entry_manufacturer_change(self, caller):
        self.parent.on_form_change()


    def on_entry_model_change(self, caller):
        self.parent.on_form_change()


    def on_manufacturer_change(self, caller):
        manindex = caller.get_active()
        if manindex == -1:
            self.entry_manufacturer_hide()
            self.lcb_model.refill([])
            self.parent.on_form_change()
            return
        enabled = devices.devs[manindex].has_key('enable_entry')
        if enabled:
            self.entry_manufacturer_show()    
        else:
            self.entry_manufacturer_hide()    

        mlist = devices.devs[manindex]['models']
        modellist = []
        for m in mlist:
            modellist.append(m['name'])
        self.lcb_model.refill(modellist)
        self.parent.on_form_change()


    def on_model_change(self, caller):
        modelindex = caller.get_active()
        if modelindex == -1:
            self.entry_model_hide()
            self.entry_firmware.set_sensitive(False)
            self.parent.on_form_change()
            return

        manindex = self.cb_manufacturer.get_active()
        enabled = devices.devs[manindex]['models'][modelindex].has_key('enable_entry')
        if enabled:
            self.entry_model_show()    
        else:
            self.entry_model_hide()
        self.entry_firmware.set_sensitive(True)
        self.parent.on_form_change()


    def get_upload_list(self, postfix):

        return {
            'combo_manufacturer' + postfix: self.lcb_manufacturer.get_active_item_tag(),
            'entry_manufacturer' + postfix: self.entry_manufacturer.get_text(),
            'combo_model' + postfix: self.lcb_model.get_active_item_tag(),
            'entry_model' + postfix: self.entry_model.get_text(),
            'entry_firmware' + postfix: self.entry_firmware.get_text(),
            }


    def get_empty_upload_list(self, postfix):

        return {
            'combo_manufacturer' + postfix: '',
            'entry_manufacturer' + postfix: '',
            'combo_model' + postfix: '',
            'entry_model' + postfix: '',
            'entry_firmware' + postfix: '',
            }

    def hide_some(self):
        self.entry_manufacturer_hide()
        self.entry_model_hide()

    def grab_focus(self):
        self.cb_manufacturer.grab_focus()

    def get_known_tests(self):

        tag_manu = self.lcb_manufacturer.get_active_item_tag()
        tag_model = self.lcb_model.get_active_item_tag()
        fw = self.entry_firmware.get_text()


        manu = None
        for m in devices.devs:
            if m['manufacturer'] == tag_manu:
                manu = m
                break
        if not manu: return None

        model = None
        for m in manu['models']:
            if m['name'] == tag_model:
                model = m
                break
        if not model: return None

        tests = None
        for f in model['trusted']:
            if fw in f['aliases']:
                tests = f['tests']
                break

        return tests

    def get_form_complete(self):
        return ((not (self.entry_manufacturer_visible and
                     self.entry_manufacturer.get_text() == '')) and
                    (not (self.entry_model_visible and
                     self.entry_model.get_text() == '')) and
                    self.lcb_manufacturer.get_active_item_tag() != 'selectmanufacturer' and
                    self.lcb_model.get_active_item_tag() != 'selectmodel'
                   )

    
    def entry_manufacturer_show(self):
        self.entry_manufacturer.show()
        self.entry_manufacturer_visible = True


    def entry_manufacturer_hide(self):
        self.entry_manufacturer.hide()
        self.entry_manufacturer_visible = False


    def entry_model_show(self):
        self.entry_model.show()
        self.entry_model_visible = True


    def entry_model_hide(self):
        self.entry_model.hide()
        self.entry_model_visible = False

