import tkinter as tk
from tkinter import ttk, messagebox
import random

# ===================== 新四军题库与知识点 =====================
knowledge_data = {
    "info": [
        {"title": "新四军成立时间", "content": "1937年10月，南方八省红军和游击队改编为国民革命军陆军新编第四军，简称新四军。"},
        {"title": "新四军首任领导", "content": "叶挺任军长，项英任副军长。"},
        {"title": "皖南事变", "content": "1941年1月发生皖南事变；事变后重建新四军军部，陈毅任代军长，刘少奇任政治委员。"},
        {"title": "新四军作战区域", "content": "主要在华中敌后：江苏、安徽、湖北、河南、浙江等地开展抗日游击战争。"},
        {"title": "铁军精神渊源", "content": "前身源自北伐战争时期国民革命军第四军，素有铁军称号，新四军继承铁军精神。"},
        {"title": "黄桥战役", "content": "1940年陈毅、粟裕指挥黄桥战役，重创国民党顽固派军队，奠定苏北抗日根据地基础。"},
        {"title": "车桥战役", "content": "1944年3月车桥战役，是新四军华中敌后一次大规模歼灭战。"}
    ],
    "questions": [
        {
            "question": "新四军正式改编于哪一年？",
            "options": ["A.1935", "B.1937", "C.1939", "D.1941"],
            "answer": "B",
            "explain": "1937年10月国共谈判达成协议，南方八省游击武装改编为新四军。"
        },
        {
            "question": "新四军首任军长是？",
            "options": ["A.陈毅", "B.项英", "C.叶挺", "D.刘少奇"],
            "answer": "C",
            "explain": "叶挺为新四军军长，皖南事变后重建军部，陈毅担任代军长。"
        },
        {
            "question": "皖南事变发生在哪一年？",
            "options": ["A.1938", "B.1940", "C.1941", "D.1942"],
            "answer": "C",
            "explain": "1941年1月爆发皖南事变。"
        },
        {
            "question": "新四军主要在哪里开展敌后抗日？",
            "options": ["A.华北", "B.华中", "C.华南", "D.东北"],
            "answer": "B",
            "explain": "新四军战场以华中敌后地区为主。"
        },
        {
            "question": "黄桥战役的主要指挥将领是？",
            "options": ["A.叶挺、项英", "B.陈毅、粟裕", "C.刘少奇、张云逸", "D.彭雪枫、邓子恢"],
            "answer": "B",
            "explain": "1940年陈毅、粟裕指挥黄桥战役。"
        }
    ]
}

wrong_questions = []  # 错题集合
current_question = None
question_list = []
q_index = 0
score = 0

# ===================== 主窗口 =====================
root = tk.Tk()
root.title("新四军历史学习系统")
root.geometry("720x520")

# 切换页面容器
main_frame = ttk.Frame(root)
main_frame.pack(fill="both", expand=True, padx=10, pady=10)

# 清空当前界面
def clear_frame():
    for widget in main_frame.winfo_children():
        widget.destroy()

# ===================== 首页菜单 =====================
def show_home():
    clear_frame()
    ttk.Label(main_frame, text="新四军历史学习系统", font=("黑体", 20)).pack(pady=30)
    ttk.Button(main_frame, text="📖 浏览历史知识点", command=show_knowledge_page, width=30).pack(pady=8)
    ttk.Button(main_frame, text="✍ 开始答题自测", command=start_exam, width=30).pack(pady=8)
    ttk.Button(main_frame, text="📝 查看错题本", command=show_wrong_page, width=30).pack(pady=8)
    ttk.Button(main_frame, text="❌ 退出程序", command=root.quit, width=30).pack(pady=8)

# ===================== 知识点浏览页面 =====================
def show_knowledge_page():
    clear_frame()
    ttk.Label(main_frame, text="新四军历史知识点", font=("黑体",16)).pack(anchor="w")
    txt = tk.Text(main_frame, wrap="word", font=("微软雅黑",11))
    txt.pack(fill="both", expand=True, pady=10)
    for item in knowledge_data["info"]:
        txt.insert(tk.END, f"【{item['title']}】\n")
        txt.insert(tk.END, f"{item['content']}\n\n")
    txt.config(state="disabled")
    ttk.Button(main_frame, text="返回首页", command=show_home).pack()

# ===================== 答题页面 =====================
def start_exam():
    global question_list, q_index, score
    clear_frame()
    score = 0
    q_index = 0
    question_list = random.sample(knowledge_data["questions"], len(knowledge_data["questions"]))
    show_one_question()

def show_one_question():
    global current_question
    if q_index >= len(question_list):
        messagebox.showinfo("答题结束", f"本次得分：{score}/{len(question_list)}")
        show_home()
        return

    current_question = question_list[q_index]
    clear_frame()

    lbl_q = ttk.Label(main_frame, text=current_question["question"], font=("微软雅黑",12), wraplength=680)
    lbl_q.pack(anchor="w", pady=10)

    var_ans = tk.StringVar()
    for opt in current_question["options"]:
        rb = ttk.Radiobutton(main_frame, text=opt, variable=var_ans, value=opt[0])
        rb.pack(anchor="w", pady=3)

    def submit():
        global score, q_index
        user = var_ans.get().upper()
        if not user:
            messagebox.showwarning("提示","请选择答案！")
            return
        if user == current_question["answer"]:
            score +=1
            messagebox.showinfo("正确", "✅回答正确！")
        else:
            wrong_questions.append(current_question)
            msg = f"❌回答错误\n正确答案：{current_question['answer']}\n{current_question['explain']}"
            messagebox.showinfo("答案解析", msg)
        q_index +=1
        show_one_question()

    ttk.Button(main_frame, text="提交答案", command=submit).pack(pady=15)
    ttk.Button(main_frame, text="放弃答题返回", command=show_home).pack()

# ===================== 错题本页面 =====================
def show_wrong_page():
    clear_frame()
    ttk.Label(main_frame, text="错题汇总", font=("黑体",16)).pack(anchor="w")
    txt = tk.Text(main_frame, wrap="word", font=("微软雅黑",11))
    txt.pack(fill="both", expand=True, pady=10)
    if not wrong_questions:
        txt.insert(tk.END, "暂无错题，继续加油！")
    else:
        for idx, q in enumerate(wrong_questions,1):
            txt.insert(tk.END, f"{idx}. {q['question']}\n")
            txt.insert(tk.END, f"正确答案：{q['answer']} 解析：{q['explain']}\n\n")
    txt.config(state="disabled")
    ttk.Button(main_frame, text="清空错题", command=lambda: [wrong_questions.clear(), show_wrong_page()]).pack(side="left", padx=5)
    ttk.Button(main_frame, text="返回首页", command=show_home).pack(side="left")

# 启动首页
show_home()
root.mainloop()