# GNU Solfege - eartraining for GNOME
# Copyright (C) 2000, 2001, 2002  Tom Cato Amundsen
#
# This program 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 2 of the License, or
# (at your option) any later version.
#
# This program 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, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

import gtk, gnome
import gu, widgets, inputwidgets
import soundcard, mpd
import abstract, const, utils
from i18n import _
import statistics, statisticsviewer

class Teacher(abstract.Teacher):
    OK = 0
    ERR_PICKY = 1
    ERR_NO_INTERVALLS = 2
    ERR_NOTERANGE = 3
    def __init__(self, exname, app):
        abstract.Teacher.__init__(self, exname, app)
        self.m_statistics = statistics.HarmonicIntervallStatistics(self)
        self.m_tonika = None
        self.m_intervall = None
    def new_question(self):
        """
        Return values:
        OK: new question created, all ok
        ERR_NO_INTERVALLS: no new question because no intervalls are selected
        ERR_PICKY: you have to solve this question before you are allowed to create new
        """
        if self.m_timeout_handle:
            gtk.timeout_remove(self.m_timeout_handle)
            self.m_timeout_handle = None

        if self.get_bool('config/picky_on_new_question') \
                 and self.q_status in [const.QSTATUS_NEW, const.QSTATUS_WRONG]:
             return Teacher.ERR_PICKY

        self.q_status = const.QSTATUS_NO

        if self.get_list('intervalls') == []:
            return Teacher.ERR_NO_INTERVALLS
        last_question = self.m_intervall
        last_tonika = self.m_tonika
        L, H = utils.adjust_low_high_to_irange(self.get_string('lowest_tone'),
           self.get_string('highest_tone'), self.get_list('intervalls'))
        while 1:
            # in this loop we will try to make a question that is not the
            # same that the last one.
            self.m_tonika, self.m_intervall = \
                  utils.random_tonika_and_intervall(L, H,
                    self.get_list('intervalls'))
            if last_question is None:
                break
            if (self.m_intervall == last_question
                and self.m_tonika == last_tonika) \
                and (len(self.get_list('intervalls')) > 1 or (H - L) > 1):
                continue
            break
        assert self.m_tonika
        self.q_status = const.QSTATUS_NEW
        return Teacher.OK
    def guess_answer(self, answer):
        """
        Return: 1 if correct, None if wrong
        """
        assert self.q_status not in (const.QSTATUS_NO, const.QSTATUS_GIVE_UP)
        if self.m_intervall == answer:
            if self.q_status == const.QSTATUS_NEW:
                self.m_statistics.add_correct(self.m_intervall)
            self.maybe_auto_new_question()
            self.q_status = const.QSTATUS_SOLVED
            return 1
        else:
            if self.q_status == const.QSTATUS_NEW:
                self.m_statistics.add_wrong(self.m_intervall, answer)
                self.q_status = const.QSTATUS_WRONG
    def give_up(self):
        """This function is only called *after* the user already has
        answered wrong once, so the statistics are already updated.
        """
        self.q_status = const.QSTATUS_GIVE_UP
    def play_question(self):
        if self.q_status == const.QSTATUS_NO:
            return
        instr_low, instr_low_vel, instr_high, instr_high_vel = self.get_instrument_config(2)
        low_tone = self.m_tonika.semitone_pitch()
        high_tone = (self.m_tonika+self.m_intervall).semitone_pitch()

        m = soundcard.Track()
        m.set_bpm(self.get_int('config/default_bpm'))
        m.set_patch(0, instr_low)
        m.set_patch(1, instr_high)
        m.start_note(0, low_tone, instr_low_vel)
        m.start_note(1, high_tone, instr_high_vel)
        m.notelen_time(4)
        m.stop_note(0, low_tone, instr_low_vel)
        m.stop_note(1, high_tone, instr_high_vel)
        soundcard.synth.play_track(m)
        return 1
    def play_melodic(self):
        if self.q_status == const.QSTATUS_NO:
            return
        if self.get_bool('override_default_instrument'):
            instr_low = self.get_int('lowest_instrument')
            instr_low_vel = self.get_int('lowest_instrument_velocity')
            instr_high = self.get_int('highest_instrument')
            instr_high_vel = self.get_int('highest_instrument_velocity')
        else:
            instr_low = instr_high = self.get_int('config/preferred_instrument')
            instr_low_vel = instr_high_vel \
                    = self.get_int('config/preferred_instrument_velocity')
        low_tone = self.m_tonika.semitone_pitch()
        high_tone = (self.m_tonika+self.m_intervall).semitone_pitch()
        m = soundcard.Track()
        m.set_bpm(self.get_int('config/default_bpm'))
        m.set_patch(0, instr_low)
        m.set_patch(1, instr_high)
        m.note(4, 0, low_tone, instr_low_vel)
        m.note(4, 1, high_tone, instr_high_vel)
        soundcard.synth.play_track(m)


class Gui(abstract.IntervallGui):
    def __init__(self, teacher, window):
        abstract.IntervallGui.__init__(self, teacher, window)
        ################
        # practice_box #
        ################
        self.g_repeat_melodic = gu.bButton(self.action_area,
            _("Repeat melodic"), lambda _o, self=self: self.m_t.play_melodic())

        self.g_repeat_melodic.set_sensitive(gtk.FALSE)

        self.g_give_up = gu.bButton(self.action_area, _("Give up"), self.give_up)
        self.g_give_up.set_sensitive(gtk.FALSE)
        ##############
        # config_box #        
        ##############
        frame = gtk.GtkFrame(_("Ask for these intervalls"))
        self.config_box.pack_start(frame, gtk.FALSE)

        self.g_intervall_selector = widgets.IntervallCheckBox(
                                          self.m_exname, 'intervalls')

        self.g_intervall_selector.set_border_width(gnome.uiconsts.PAD)
        frame.add(self.g_intervall_selector)
        #------we need some space
        self.config_box.pack_start(gtk.GtkHBox(), gtk.FALSE,
                                   padding=gnome.uiconsts.PAD_SMALL)
        # -------------------------------------------------
        hbox = gu.bHBox(self.config_box, gtk.FALSE)
        hbox.set_spacing(gnome.uiconsts.PAD)
        hbox.pack_start(gtk.GtkLabel(_("Try to keep question with range from")),
                        gtk.FALSE)
        lowspin = widgets.SimplestNotenameSpinButton(self.get_string('lowest_tone'))
        hbox.pack_start(lowspin, gtk.FALSE)
        gu.bLabel(hbox, _("to"), gtk.FALSE)
        highspin = widgets.SimplestNotenameSpinButton(self.get_string('highest_tone'))
        hbox.pack_start(highspin, gtk.FALSE)
        self.m_notenamerange = widgets.nNotenameRangeController(lowspin, highspin,
                           mpd.LOWEST_NOTENAME, mpd.HIGHEST_NOTENAME,
                           self.m_t.m_exname, 'lowest_tone', 'highest_tone')

        # ------------------------------------------
        self._add_auto_new_question_gui(self.config_box)
        # ----------------------------------------------
        hbox = gu.bHBox(self.config_box, gtk.FALSE)
        hbox.set_spacing(gnome.uiconsts.PAD_SMALL)
        gu.bLabel(hbox, _("Input interface:"), gtk.FALSE)
        combo = gu.nCombo(self.m_exname, 'inputwidget', _("Buttons"),
                   inputwidgets.inputwidget_names)
        hbox.pack_start(combo, gtk.FALSE)
        combo.entry.connect('changed',
               lambda entry, self=self, inputwidgets=inputwidgets:
               self.use_inputwidget(inputwidgets.name_to_inputwidget(
                           entry.get_text(), self.click_on_intervall)))
        # ------------ frame -------------------
        self.g_instrument_configurator = widgets.InstrumentConfigurator(
                                                             self.m_exname, 2)
        self.config_box.pack_start(self.g_instrument_configurator, gtk.FALSE)
        self.config_box.set_spacing(0)
        self.config_box.show_all()
        ##############
        # statistics #
        ##############
        self.setup_statisticsviewer(statisticsviewer.StatisticsViewer,
                                   _("Harmonic intervall"))
        self.m_key_bindings['repeat_melodic_ak'] = self.m_t.play_melodic
        self.select_inputwidget()
    def give_up(self, _o=None):
        if self.m_t.q_status == const.QSTATUS_WRONG:
            self.g_flashbar.push(_("The answer is: %s")
                 % const.int_intervall[self.m_t.m_intervall])
            self.m_t.give_up()
            self.g_new_intervall.set_sensitive(gtk.TRUE)
            self.g_give_up.set_sensitive(gtk.FALSE)
    def click_on_intervall(self, mouse_button, intervall, midi_int):
        if mouse_button == 1:
            if self.m_t.q_status == const.QSTATUS_NO:
                self.g_flashbar.flash(_("Click 'New intervall' to begin."))
                return
            if not -17 < intervall < 17:
                #FIXME  fix and remove
                self.g_flashbar.flash(_("Ignoring intervalls greater than decim."))
                self.g_input.forget_last_tone()
                return
            if self.m_t.q_status == const.QSTATUS_SOLVED:
                if self.m_t.guess_answer(intervall):
                    self.g_flashbar.flash(_("Correct, but you have already solved this question"))
                else:
                    self.g_flashbar.flash(_("Wrong, but you have already solved this question"))
            elif self.m_t.q_status in (const.QSTATUS_NEW, const.QSTATUS_WRONG):
                if self.m_t.guess_answer(intervall):
                    self.g_flashbar.flash(_("Correct"))
                    self.g_new_intervall.set_sensitive(gtk.TRUE)
                    self.g_give_up.set_sensitive(gtk.FALSE)
                else:
                    self.g_flashbar.flash(_("Wrong"))
                    self.g_give_up.set_sensitive(gtk.TRUE)
                    if self.get_bool("config/auto_repeat_question_if_wrong_answer"):
                        self.m_t.play_question()
            self.g_input.set_first_note(self.m_t.m_tonika)
        elif mouse_button == 2:
            # we only get here if you use one if the instrument widgets, not
            # with the buttons interface.
            soundcard.play_note(self.get_int('config/preferred_instrument'),
                          4, 0, midi_int,
                          self.get_int('config/preferred_instrument_velocity'))
        elif mouse_button == 3 and self.m_t.q_status != const.QSTATUS_NO:
            instr_low, instr_low_vel, instr_high, instr_high_vel = self.m_t.get_instrument_config(2)
            n2 = self.m_t.m_tonika + intervall
            m = soundcard.Track()
            m.set_patch(0, instr_low)
            m.set_patch(1, instr_high)
            m.start_note(0, self.m_t.m_tonika.semitone_pitch(), instr_low_vel)
            m.start_note(1, n2.semitone_pitch(), instr_low_vel)
            m.notelen_time(4)
            m.stop_note(0, self.m_t.m_tonika.semitone_pitch(), instr_high_vel)
            m.stop_note(1, n2.semitone_pitch(), instr_high_vel)
            soundcard.synth.play_track(m)
    def new_question(self, _o=None):
        """This function is called when you click on the 'New intervall'
        button, or when you use the key bindings. So it can be called even
        if the 'New intervall' button is insensitive.
        """
        g = self.m_t.new_question()
        if g == Teacher.OK: # new question, everything is OK
            self.g_repeat.set_sensitive(gtk.TRUE)
            self.g_repeat_melodic.set_sensitive(gtk.TRUE)
            self.g_input.set_first_note(self.m_t.m_tonika)
            self.g_new_intervall.set_sensitive(
              not self.get_bool('config/picky_on_new_question'))
            self.m_t.play_question()
            self.g_flashbar.clear()
        elif g == Teacher.ERR_PICKY:
                self.g_flashbar.flash(_("You have to solve this question first."))
        elif g == Teacher.ERR_NO_INTERVALLS:
            self.g_repeat.set_sensitive(gtk.FALSE)
            self.g_repeat_melodic.set_sensitive(gtk.FALSE)
            self.g_flashbar.clear()
            self.g_flashbar.flash(
                    _("You have to select some intervalls to practise."))
    def on_start_practise(self):
        self.m_t.m_statistics.reset_session()
        gtk.timeout_add(const.SHORT_WAIT, lambda self=self:
            self.g_flashbar.flash(_("Click 'New intervall' to begin.")))
    def on_end_practise(self):
        self.g_new_intervall.set_sensitive(gtk.TRUE)
        self.g_repeat.set_sensitive(gtk.FALSE)
        self.g_repeat_melodic.set_sensitive(gtk.FALSE)
        self.g_give_up.set_sensitive(gtk.FALSE)
        self.g_flashbar.clear()
        self.g_input.clear()
        self.m_t.end_practise()
