########################################################################
#
#  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 threading, thread


import lang
import devices
import dnstests
import time
from page import Page
import images
from test import Test

class PageTestsProgress(Page):

    title = 'testtitle'
    number = 5


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


    def create_vbox(self):
        vbox = gtk.VBox()
        vbox.set_border_width(20)
        vbox.set_spacing(10)
        
        # add frame with label
        
        self.testtitle = gtk.Label()
        lang.bind(self.testtitle, 'testing')
        vbox.pack_start(self.testtitle, False, False)


        self.progressbar = gtk.ProgressBar()
        self.progressbar.set_fraction(0)

        pb_container = gtk.Alignment(0.5, 0.5, 1.0, 0)
        pb_container.add(self.progressbar)


        self.vrtule = gtk.Image()
        self.vrtule.set_from_file(images.fullname('loader.gif'))
        self.vrtule.show()

        hbox = gtk.HBox()
        hbox.set_spacing(10)
        hbox.pack_start(pb_container, True, True)
        hbox.pack_start(self.vrtule, False, False)

        vbox.pack_start(hbox, False, False)
        self.performing = gtk.Label(lang.get_string('performing_test'))
        vbox.pack_start(self.performing, False, False)

        expander = gtk.Expander(lang.get_string('show_details'))
        self.scrolled_window = gtk.ScrolledWindow(hadjustment=None, vadjustment=None)
        self.scrolled_window.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
        self.evbox = gtk.VBox()
        self.scrolled_window.add_with_viewport(self.evbox)
        expander.add(self.scrolled_window)
        expander.connect("activate", self.on_expander_activate)

        vbox.pack_start(expander, True, True)

        vbox.show_all()
        return vbox


    def on_expander_activate(self, expander):
        old_expanded = expander.get_expanded()
        if old_expanded:
            expander.set_label(lang.get_string('show_details'))
        else:
            expander.set_label(lang.get_string('hide_details'))


    def on_before_show(self):
        self.widget.show_all()
        self.start_tests()
        self.parent.enable_back_button(False)


    def run_tests_async(self, start_test_callback, finish_test_callback, finish_tests_callback):

        results = []
        i = 0
        for test in dnstests.tests:
            gobject.idle_add(start_test_callback, test)
            if '--slowly' in sys.argv: time.sleep(0.5)
            test.run()
            gobject.idle_add(finish_test_callback, test, i)
            i = i + 1

        gobject.idle_add(finish_tests_callback)


    def start_tests(self):
        self.setup_progressbar(0)
        self.parent.enable_back_button(False)
        self.parent.set_page_complete(self.widget, False)
        if self.running: return
        self.running = True
        for c in self.evbox.get_children(): self.evbox.remove(c)
        self.tests_thread = threading.Thread(target=self.run_tests_async, args=(self.start_test, self.finish_test, self.finish_tests))
        self.tests_thread.start()


    def start_test(self, test):
        self.performing.set_text(lang.get_string('performing_test') + test.name)
        test.update_phase(Test.PHASE_START)
        w = test.get_progress_widget()
        self.evbox.pack_start(w, False, False, 0)
        a = self.scrolled_window.get_vadjustment()
        a.set_value(a.upper)


    def setup_progressbar(self, n):
        cnt = len(dnstests.tests)
        self.progressbar.set_fraction(float(n + 1) / cnt)
        self.progressbar.set_text("%d/%d" % (n + 1, cnt))


    def finish_test(self, test, n):
        self.setup_progressbar(n)
        test.update_phase(Test.PHASE_DONE)


    def finish_tests(self):
        self.running = False
        self.parent.set_page_complete(self.widget, True)
        self.parent.next()
        self.deactivate()


    def activate(self):
        self.widget.show()


    def deactivate(self):
        self.widget.hide_all()

