#!/usr/bin/python3
import gi, random
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, Pango

class App(Gtk.Window):
    def __init__(self):
        super().__init__(title='Math Quiz')
        self.set_default_size(380, 320)
        self.score = 0; self.total = 0
        self.answer = 0
        vbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=12)
        vbox.set_margin_top(20); vbox.set_margin_bottom(20)
        vbox.set_margin_start(20); vbox.set_margin_end(20)
        self.add(vbox)
        # difficulty
        diff_box = Gtk.Box(spacing=8)
        diff_box.add(Gtk.Label(label='Difficulty:'))
        self.diff = Gtk.ComboBoxText()
        for d in ['Easy (1-10)','Medium (1-50)','Hard (1-100)']:
            self.diff.append_text(d)
        self.diff.set_active(0)
        diff_box.pack_start(self.diff, False, False, 0)
        vbox.pack_start(diff_box, False, False, 0)
        # score
        self.score_lbl = Gtk.Label(label='Score: 0 / 0')
        vbox.pack_start(self.score_lbl, False, False, 0)
        # question
        self.question_lbl = Gtk.Label()
        self.question_lbl.override_font(Pango.FontDescription('Bold 28'))
        vbox.pack_start(self.question_lbl, True, True, 0)
        # answer
        hb = Gtk.Box(spacing=8)
        hb.set_halign(Gtk.Align.CENTER)
        self.answer_entry = Gtk.Entry()
        self.answer_entry.set_width_chars(8)
        self.answer_entry.override_font(Pango.FontDescription('20'))
        self.answer_entry.connect('activate', self.check_answer)
        hb.pack_start(self.answer_entry, False, False, 0)
        check_btn = Gtk.Button(label='Check')
        check_btn.connect('clicked', self.check_answer)
        hb.pack_start(check_btn, False, False, 0)
        vbox.pack_start(hb, False, False, 0)
        self.feedback = Gtk.Label()
        self.feedback.override_font(Pango.FontDescription('16'))
        vbox.pack_start(self.feedback, False, False, 0)
        next_btn = Gtk.Button(label='Next Question →')
        next_btn.connect('clicked', self.new_question)
        vbox.pack_start(next_btn, False, False, 0)
        self.new_question()
    def get_range(self):
        d = self.diff.get_active()
        return [10, 50, 100][d]
    def new_question(self, *_):
        r = self.get_range()
        op = random.choice(['+','-','×','÷'])
        a = random.randint(1, r)
        if op == '+': b = random.randint(1,r); self.answer = a+b
        elif op == '-': b = random.randint(1,a); self.answer = a-b
        elif op == '×': b = random.randint(1,min(r,12)); a = random.randint(1,min(r,12)); self.answer = a*b
        else:
            b = random.randint(1,10)
            self.answer = a; a = a*b
        self.question_lbl.set_text(f'{a} {op} {b} = ?')
        self.answer_entry.set_text('')
        self.feedback.set_text('')
        self.answer_entry.grab_focus()
    def check_answer(self, *_):
        try:
            val = int(self.answer_entry.get_text().strip())
            self.total += 1
            if val == self.answer:
                self.score += 1
                self.feedback.set_markup('<span foreground="green" weight="bold">✓ Correct!</span>')
            else:
                self.feedback.set_markup(f'<span foreground="red">✗ Wrong. Answer: {self.answer}</span>')
            self.score_lbl.set_text(f'Score: {self.score} / {self.total}')
        except ValueError:
            self.feedback.set_text('Enter a number')

win = App()
win.connect('destroy', Gtk.main_quit)
win.show_all()
Gtk.main()
