import tkinter as tk
from tkinter import ttk, messagebox

# 唐诗库，可自行继续添加更多
tang_poems = [
    {
        "title": "静夜思",
        "author": "李白",
        "content": "床前明月光，疑是地上霜。\n举头望明月，低头思故乡。"
    },
    {
        "title": "春晓",
        "author": "孟浩然",
        "content": "春眠不觉晓，处处闻啼鸟。\n夜来风雨声，花落知多少。"
    },
    {
        "title": "登鹳雀楼",
        "author": "王之涣",
        "content": "白日依山尽，黄河入海流。\n欲穷千里目，更上一层楼。"
    },
    {
        "title": "相思",
        "author": "王维",
        "content": "红豆生南国，春来发几枝。\n愿君多采撷，此物最相思。"
    },
    {
        "title": "咏鹅",
        "author": "骆宾王",
        "content": "鹅鹅鹅，曲项向天歌。\n白毛浮绿水，红掌拨清波。"
    },
    {
        "title": "江雪",
        "author": "柳宗元",
        "content": "千山鸟飞绝，万径人踪灭。\n孤舟蓑笠翁，独钓寒江雪。"
    },
    {
        "title": "望庐山瀑布",
        "author": "李白",
        "content": "日照香炉生紫烟，遥看瀑布挂前川。\n飞流直下三千尺，疑是银河落九天。"
    },
    {
        "title": "九月九日忆山东兄弟",
        "author": "王维",
        "content": "独在异乡为异客，每逢佳节倍思亲。\n遥知兄弟登高处，遍插茱萸少一人。"
    }
]


class TangPoemApp:
    def __init__(self, root):
        self.root = root
        self.root.title("唐诗三百首")
        self.root.geometry("620x520")
        self.root.resizable(False, False)

        self.poem_list = tang_poems
        self.current_index = 0

        # 搜索区域
        frame_search = ttk.Frame(root, padding=8)
        frame_search.pack(fill=tk.X)

        ttk.Label(frame_search, text="搜索：").pack(side=tk.LEFT)
        self.var_search = tk.StringVar()
        entry_search = ttk.Entry(frame_search, textvariable=self.var_search, width=35)
        entry_search.pack(side=tk.LEFT, padx=5)
        btn_search = ttk.Button(frame_search, text="查找", command=self.do_search)
        btn_search.pack(side=tk.LEFT)
        btn_reset = ttk.Button(frame_search, text="重置", command=self.reset_all)
        btn_reset.pack(side=tk.LEFT, padx=3)

        # 信息显示区
        frame_info = ttk.Frame(root, padding=10)
        frame_info.pack(fill=tk.X)

        self.var_title = tk.StringVar()
        self.var_author = tk.StringVar()
        ttk.Label(frame_info, textvariable=self.var_title, font=("SimHei", 18)).pack()
        ttk.Label(frame_info, textvariable=self.var_author, font=("SimHei", 12)).pack(pady=4)

        # 诗词正文
        frame_text = ttk.Frame(root, padding=10)
        frame_text.pack(fill=tk.BOTH, expand=True)
        self.text_content = tk.Text(frame_text, font=("SimHei", 14), width=50, height=10)
        self.text_content.pack(fill=tk.BOTH, expand=True)

        # 按钮区域
        frame_btn = ttk.Frame(root, padding=10)
        frame_btn.pack()
        self.btn_prev = ttk.Button(frame_btn, text="上一首", command=self.prev_poem)
        self.btn_prev.grid(row=0, column=0, padx=12)
        self.btn_next = ttk.Button(frame_btn, text="下一首", command=self.next_poem)
        self.btn_next.grid(row=0, column=1, padx=12)

        self.show_poem()

    def show_poem(self):
        """展示当前诗词"""
        if not self.poem_list:
            self.var_title.set("无诗词")
            self.var_author.set("")
            self.text_content.delete(1.0, tk.END)
            return
        p = self.poem_list[self.current_index]
        self.var_title.set(p["title"])
        self.var_author.set(f"作者：{p['author']}")
        self.text_content.delete(1.0, tk.END)
        self.text_content.insert(tk.END, p["content"])

    def prev_poem(self):
        if self.current_index > 0:
            self.current_index -= 1
            self.show_poem()
        else:
            messagebox.showinfo("提示", "已经是第一首")

    def next_poem(self):
        if self.current_index < len(self.poem_list) - 1:
            self.current_index += 1
            self.show_poem()
        else:
            messagebox.showinfo("提示", "已经是最后一首")

    def do_search(self):
        keyword = self.var_search.get().strip()
        if not keyword:
            return
        res = []
        for p in tang_poems:
            if keyword in p["title"] or keyword in p["author"] or keyword in p["content"]:
                res.append(p)
        if len(res) == 0:
            messagebox.showinfo("结果", "没有找到匹配诗词")
            return
        self.poem_list = res
        self.current_index = 0
        self.show_poem()

    def reset_all(self):
        """恢复全部诗词列表"""
        self.var_search.set("")
        self.poem_list = tang_poems
        self.current_index = 0
        self.show_poem()


if __name__ == "__main__":
    win = tk.Tk()
    app = TangPoemApp(win)
    win.mainloop()
