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

# 简易唐诗数据集，可以继续扩充，这里先放一部分，你后续可以添加完整300首
poem_list = [
    {"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("800x600")

        # 当前选中索引
        self.index = 0
        self.data = poem_list

        # 顶部搜索框
        self.top_frame = ttk.Frame(root, padding=5)
        self.top_frame.pack(fill="x")
        ttk.Label(self.top_frame, text="搜索标题/作者：").pack(side="left")
        self.search_var = tk.StringVar()
        self.search_entry = ttk.Entry(self.top_frame, textvariable=self.search_var)
        self.search_entry.pack(side="left", fill="x", expand=True, padx=5)
        ttk.Button(self.top_frame, text="查找", command=self.search_poem).pack(side="left")

        # 主体左右分割
        self.main_frame = ttk.Frame(root)
        self.main_frame.pack(fill="both", expand=True, padx=5, pady=5)

        # 左侧列表框
        self.left_frame = ttk.Frame(self.main_frame)
        self.left_frame.pack(side="left", fill="y")
        self.listbox = tk.Listbox(self.left_frame, width=25, font=("宋体", 11))
        self.scroll_bar = ttk.Scrollbar(self.left_frame, orient="vertical", command=self.listbox.yview)
        self.listbox.configure(yscrollcommand=self.scroll_bar.set)
        self.listbox.pack(side="left", fill="y")
        self.scroll_bar.pack(side="right", fill="y")

        # 右侧阅读区
        self.right_frame = ttk.Frame(self.main_frame)
        self.right_frame.pack(side="right", fill="both", expand=True)
        self.text_view = scrolledtext.ScrolledText(self.right_frame, font=("宋体", 14), wrap="word")
        self.text_view.pack(fill="both", expand=True)

        # 底部按钮栏
        self.bottom_frame = ttk.Frame(root, padding=5)
        self.bottom_frame.pack(fill="x")
        ttk.Button(self.bottom_frame, text="上一首", command=self.prev_poem).pack(side="left", padx=3)
        ttk.Button(self.bottom_frame, text="下一首", command=self.next_poem).pack(side="left", padx=3)
        ttk.Button(self.bottom_frame, text="重置列表", command=self.reset_list).pack(side="left", padx=3)

        # 绑定列表点击事件
        self.listbox.bind("<<ListboxSelect>>", self.on_select)

        self.reset_list()

    def refresh_list(self, arr):
        self.listbox.delete(0, tk.END)
        for i, p in enumerate(arr):
            self.listbox.insert(tk.END, f"{p['title']} — {p['author']}")

    def reset_list(self):
        self.data = poem_list
        self.refresh_list(self.data)

    def show_content(self, poem):
        self.text_view.delete(1.0, tk.END)
        out = f"《{poem['title']}》\n作者：{poem['author']}\n\n{poem['content']}"
        self.text_view.insert(tk.END, out)

    def on_select(self, event):
        sel = self.listbox.curselection()
        if sel:
            self.index = sel[0]
            self.show_content(self.data[self.index])

    def next_poem(self):
        if self.index < len(self.data) - 1:
            self.index += 1
            self.listbox.selection_clear(0, tk.END)
            self.listbox.selection_set(self.index)
            self.show_content(self.data[self.index])

    def prev_poem(self):
        if self.index > 0:
            self.index -= 1
            self.listbox.selection_clear(0, tk.END)
            self.listbox.selection_set(self.index)
            self.show_content(self.data[self.index])

    def search_poem(self):
        keyword = self.search_var.get().strip()
        if not keyword:
            self.reset_list()
            return
        res = []
        for p in poem_list:
            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.data = res
        self.refresh_list(self.data)


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