########################################################################
#
#  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 sys
import pygtk
pygtk.require("2.0")
import gtk, gobject

import lang
from page import Page
import images, labeltext
import compat
import dnstests


class PageReport(Page):
    
    title = 'report_title'
    number = 6
    focus_label = 'gtk-apply'

    def __init__(self, parent):
        Page.__init__(self, parent)
        self.widget = self.create_vbox()

    def create_vbox(self):
        vbox = gtk.VBox()
        vbox.set_border_width(20)
        vbox.set_spacing(5)

        hbox = gtk.HBox()
        compat = gtk.Label()
        lang.bind(compat, 'compatibility')
        hbox.pack_start(compat, True, True, 5)

        self.result_icon = gtk.Image()
        self.result_icon.show()
        hbox.pack_start(self.result_icon, False, False, 5)

        self.result_text = gtk.Label()
        self.result_text.show()
        hbox.pack_start(self.result_text, False, False, 0)
        self.summary = gtk.Label()
        hbox.pack_start(self.summary, True, True, 5)

        vbox.pack_start(hbox, False, False)

        # PS: result warning has special markup
        self.result_warning = labeltext.LabelText(padding=0)
        self.result_warning.set_justify(gtk.JUSTIFY_FILL)
        self.result_warning.set_use_markup(True)
        vbox.pack_start(self.result_warning, False, False)

        title = gtk.Label()
        lang.bind(title, 'report')
        title.set_alignment(0, 0)
        vbox.pack_start(title, False, False)
        
        self.scrolled_window = gtk.ScrolledWindow(hadjustment=None, vadjustment=None)
        self.scrolled_window.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)

        self.report_container = gtk.VBox()
        self.scrolled_window.add_with_viewport(self.report_container)
        vbox.pack_start(self.scrolled_window, True, True)
        self.starinfo = labeltext.LabelText('report_known_device')
        vbox.pack_start(self.starinfo, False, False, 0)
        warning = labeltext.LabelText('report_warning', padding=0)
        vbox.pack_start(warning, False, False, 0)
        
        vbox.show_all()
        return vbox


    def on_before_show(self):
        Page.on_before_show(self)

        self.fill_report()
        self.setup_summary()
        self.parent.deactivate_testsprogress()
        self.parent.prevent_last_button()


    def device_report(self, dev, prefix='  '):
        report = []
        list = dev.get_upload_list('')

        if list['combo_manufacturer'] == 'othermanufacturer':
            manufacturer = list['entry_manufacturer']
        else:
            manufacturer = lang.get_string(list['combo_manufacturer'])

        if list['combo_model'] == 'othermodel':
            model = list['entry_model']
        else:
            model = lang.get_string(list['combo_model'])

        firmware = list['entry_firmware']

        report.append(prefix + lang.get_string('manufacturer') + ' ' + manufacturer)
        report.append(prefix + lang.get_string('model') + ' ' + model)
        report.append(prefix + lang.get_string('firmware') + ' ' + firmware)
        return report


    def netinfo_report(self, prefix = '  '):
        report = []

        data = self.parent.netinfo.get_data()

        report.append(prefix + lang.get_string('report_ipaddresses') + ' ' + data['addresses'])
        report.append(prefix + lang.get_string('report_dnsservers') + ' ' + data['nameservers'])
        report.append(prefix + lang.get_string('report_gwip') + ' ' + data['gateway'])
        report.append(prefix + lang.get_string('report_gwmac') + ' ' + data['mac'])
        return report


    def strings2label(self, content):
        label = gtk.Label()
        label.set_line_wrap(True)
        label.set_alignment(0, 0)
        label.set_text('\n'.join(content))
        return label


    def flush_report_container(self):
        children = self.scrolled_window.get_children()
        for c in children:
            self.scrolled_window.remove(c)
            c.destroy()


    def setup_report_container(self):
        self.scrolled_window.add_with_viewport(self.report_container)
        self.scrolled_window.show_all()


    def fill_report(self):
        report = []

        report.append(lang.get_string('report_devparams'))
        page_dev = self.parent.pages[self.parent.PAGE_SELECTDEVICE]
        if not page_dev.dev2:
            report.extend(self.device_report(page_dev.dev1))
            self.known_tests = page_dev.dev1.get_known_tests()
        else:
            report.append('  ' + lang.get_string('report_dev1params'))
            report.extend(self.device_report(page_dev.dev1, '    '))
            report.append('  ' + lang.get_string('report_dev2params'))
            report.extend(self.device_report(page_dev.dev2, '    '))
            self.known_tests = None

        report.append('')

        report.append(lang.get_string('report_netparams'))
        report.extend(self.netinfo_report())

        report.append('')

        report.append(lang.get_string('report_osinfo'))
        report.append('  ' + lang.get_string('report_platform') + ' ' + sys.platform)

        report.append('')

        report.append(lang.get_string('report_testsresults'))

        self.flush_report_container()
        self.report_container.pack_start(self.strings2label(report), False, False, 0)

        show_starinfo = False

        for t in dnstests.tests: 
            known_better = t.is_known_better(self.known_tests)
            if known_better: show_starinfo = True
            result_widget = t.result_widget(known_better)
            self.report_container.pack_start(result_widget, False, False, 0)

        if show_starinfo:
            self.starinfo.show()
        else:
            self.starinfo.hide()

        self.setup_report_container()


    def setup_summary(self):
        # count all tests and successfull ones
        tests_total = len(dnstests.tests)
        tests_ok = 0
        score = 0.0
        for t in dnstests.tests:
            if t.result:
                tests_ok += 1

            if t.result or t.is_known_better(self.known_tests):
                w = t.get_weight()
                if w != None: score += w

        self.summary.set_text(str(tests_ok) + '/' + str(tests_total) + lang.get_string('summary'))

        self.result_icon.set_from_file(images.fullname(compat.get_image(score)))
        self.result_text.set_text(lang.get_string(compat.get_description(score)))
        # PS: result warning has special markup => set_label()
        self.result_warning.set_label('<i>'+lang.get_string(compat.get_warning(score))+'</i>')


