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

# --- 模拟数据库 (实际应用中可以读取 JSON 或 CSV 文件) ---
# 这里只列举几首作为示例，你可以按此格式继续添加
POEMS_DATA = [
    {
        "title": "静夜思",
        "author": "李白",
        "content": "床前明月光，疑是地上霜。\n举头望明月，低头思故乡。",
        "type": "五言绝句"
    },
    {
        "title": "春晓",
        "author": "孟浩然",
        "content": "春眠不觉晓，处处闻啼鸟。\n夜来风雨声，花落知多少。",
        "type": "五言绝句"
    },
    {
        "title": "登鹳雀楼",
        "author": "王之涣",
        "content": "白日依山尽，黄河入海流。\n欲穷千里目，更上一层楼。",
        "type": "五言绝句"
    },
    {
        "title": "望庐山瀑布",
        "author": "李白",
        "content": "日照香炉生紫烟，遥看瀑布挂前川。\n飞流直下三千尺，疑是银河落九天。",
        "type": "七言绝句"
    },
    {
        "title": "赋得古原草送别",
        "author": "白居易",
        "content": "离离原上草，一岁一枯荣。\n野火烧不尽，春风吹又生。\n远芳侵古道，晴翠接荒城。\n又送王孙去，萋萋满别情。",
        "type": "五言律诗"
    }
]

class TangPoetryApp:
    def __init__(self, root):
        self.root = root
        self.root.title("唐诗三百首精选 - Python版")
        self.root.geometry("800x600")

        # 设置样式
        style = ttk.Style()
        style.configure("TButton", font=("微软雅黑", 10))
        
        self._create_widgets()
        self._fill_list(POEMS_DATA)

    def _create_widgets(self):
        # --- 左侧布局：搜索和列表 ---
        left_frame = tk.Frame(self.root, width=250, bg="#f0f0f0")
        left_frame.pack(side="left", fill="y", padx=10, pady=10)

        tk.Label(left_frame, text="搜索诗词/诗人:", bg="#f0f0f0").pack(anchor="w")
        self.search_var = tk.StringVar()
        self.search_var.trace("w", self._on_search)
        tk.Entry(left_frame, textvariable=self.search_var).pack(fill="x", pady=5)

        self.poem_listbox = tk.Listbox(left_frame, font=("微软雅黑", 11))
        self.poem_listbox.pack(expand=True, fill="both", pady=5)
        self.poem_listbox.bind("<<ListboxSelect>>", self._on_select)

        tk.Button(left_frame, text="随机读诗", command=self._random_poem, bg="#e1e1e1").pack(fill="x")

        # --- 右侧布局：内容展示 ---
        right_frame = tk.Frame(self.root, bg="white")
        right_frame.pack(side="right", expand=True, fill="both", padx=10, pady=10)

        self.title_label = tk.Label(right_frame, text="请选择一首诗", font=("微软雅黑", 20, "bold"), bg="white")
        self.title_label.pack(pady=20)

        self.author_label = tk.Label(right_frame, text="", font=("微软雅黑", 12), bg="white", fg="#666")
        self.author_label.pack()

        self.content_text = scrolledtext.ScrolledText(
            right_frame, font=("楷体", 18), bg="white", bd=0, relief="flat"
        )
        self.content_text.pack(expand=True, fill="both", pady=20)
        self.content_text.tag_configure("center", justify='center')

    def _fill_list(self, data):
        self.poem_listbox.delete(0, tk.END)
        self.current_display_data = data
        for poem in data:
            self.poem_listbox.insert(tk.END, f" {poem['title']} ({poem['author']})")

    def _on_search(self, *args):
        query = self.search_var.get().strip().lower()
        filtered = [p for p in POEMS_DATA if query in p['title'].lower() or query in p['author'].lower()]
        self._fill_list(filtered)

    def _on_select(self, event):
        selection = self.poem_listbox.curselection()
        if selection:
            index = selection[0]
            self._display_poem(self.current_display_data[index])

    def _display_poem(self, poem):
        self.title_label.config(text=poem['title'])
        self.author_label.config(text=f"[{poem['type']}] {poem['author']}")
        self.content_text.delete(1.0, tk.END)
        self.content_text.insert(tk.END, poem['content'], "center")

    def _random_poem(self):
        poem = random.choice(POEMS_DATA)
        self._display_poem(poem)

if __name__ == "__main__":
    root = tk.Tk()
    app = TangPoetryApp(root)
    root.mainloop()