import pygame
from datetime import datetime

# -------------------------- 工具函数：生肖、星座计算 --------------------------
def get_zodiac(year):
    """获取生肖"""
    zodiac_list = ["鼠", "牛", "虎", "兔", "龙", "蛇", "马", "羊", "猴", "鸡", "狗", "猪"]
    start_year = 1900
    index = (year - start_year) % 12
    return zodiac_list[index]

def get_constellation(month, day):
    """获取星座"""
    dates = ((1, 20, "水瓶座"), (2, 19, "双鱼座"), (3, 21, "白羊座"),
             (4, 20, "金牛座"), (5, 21, "双子座"), (6, 22, "巨蟹座"),
             (7, 23, "狮子座"), (8, 23, "处女座"), (9, 23, "天秤座"),
             (10, 24, "天蝎座"), (11, 23, "射手座"), (12, 22, "摩羯座"))
    c_name = "摩羯座"
    for m, d, name in dates:
        if month == m and day >= d or month == m + 1 and day < dates[m % 12][1]:
            c_name = name
            break
    return c_name

constellation_info = {
    "水瓶座": "独立创新，追求自由，思维独特",
    "双鱼座": "温柔浪漫，富有想象力，感性细腻",
    "白羊座": "热情冲动，勇敢直率，行动力强",
    "金牛座": "稳重踏实，热爱美好，耐心执着",
    "双子座": "思维敏捷，善于沟通，好奇心旺盛",
    "巨蟹座": "顾家敏感，内心柔软，重视情感",
    "狮子座": "自信大气，热情慷慨，渴望认可",
    "处女座": "细致严谨，追求完美，善于分析",
    "天秤座": "优雅和善，追求公平，审美出众",
    "天蝎座": "洞察力强，爱恨分明，意志力强",
    "射手座": "乐观开朗，热爱自由，向往远方",
    "摩羯座": "沉稳自律，吃苦耐劳，目标坚定"
}

# -------------------------- Pygame初始化 --------------------------
pygame.init()
W, H = 640, 480
screen = pygame.display.set_mode((W, H))
pygame.display.set_caption("星座生肖查询器")
clock = pygame.time.Clock()

# 字体（兼容中文）
try:
    font_big = pygame.font.Font("simhei.ttf", 32)
    font_mid = pygame.font.Font("simhei.ttf", 24)
    font_small = pygame.font.Font("simhei.ttf", 18)
except:
    font_big = pygame.font.SysFont("SimHei", 32)
    font_mid = pygame.font.SysFont("SimHei", 24)
    font_small = pygame.font.SysFont("SimHei", 18)

# 颜色
BG_COLOR = (25, 25, 45)
TEXT_COLOR = (240, 240, 255)
BOX_COLOR = (60, 60, 90)
BOX_ACTIVE = (90, 90, 140)
BTN_COLOR = (70, 120, 180)
BTN_HOVER = (100, 160, 220)

# 输入框数据
input_year = ""
input_month = ""
input_day = ""
active_box = 0  #0年 1月 2日
input_boxes = [
    pygame.Rect(120, 120, 120, 40),
    pygame.Rect(280, 120, 100, 40),
    pygame.Rect(420, 120, 100, 40)
]
box_text = ["", "", ""]

# 查询按钮
btn_rect = pygame.Rect(220, 190, 200, 45)

# 查询结果
result_zodiac = ""
result_star = ""
result_desc = ""

running = True
while running:
    mouse_x, mouse_y = pygame.mouse.get_pos()
    hover_btn = btn_rect.collidepoint(mouse_x, mouse_y)

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

        # 鼠标点击
        if event.type == pygame.MOUSEBUTTONDOWN:
            # 判断点击哪个输入框
            active_box = -1
            for i, rect in enumerate(input_boxes):
                if rect.collidepoint(event.pos):
                    active_box = i
            # 查询按钮点击
            if hover_btn and active_box == -1:
                try:
                    y = int(box_text[0])
                    m = int(box_text[1])
                    d = int(box_text[2])
                    # 简单日期校验
                    if 1900 <= y <= 2100 and 1 <= m <= 12 and 1 <= d <= 31:
                        result_zodiac = get_zodiac(y)
                        result_star = get_constellation(m, d)
                        result_desc = constellation_info[result_star]
                    else:
                        result_zodiac = "输入有误"
                        result_star = ""
                        result_desc = "请输入合法日期(1900-2100)"
                except:
                    result_zodiac = "输入有误"
                    result_star = ""
                    result_desc = "请填写完整数字日期！"

        # 键盘输入
        if event.type == pygame.KEYDOWN:
            if active_box >= 0:
                if event.key == pygame.K_BACKSPACE:
                    box_text[active_box] = box_text[active_box][:-1]
                elif event.unicode.isdigit():
                    # 限制长度
                    max_len = [4, 2, 2][active_box]
                    if len(box_text[active_box]) < max_len:
                        box_text[active_box] += event.unicode

    # 绘制画面
    screen.fill(BG_COLOR)
    # 标题
    title = font_big.render("✨ 星座 · 生肖查询工具", True, TEXT_COLOR)
    screen.blit(title, (W//2 - title.get_width()//2, 40))

    # 标签文字
    screen.blit(font_mid.render("年：", True, TEXT_COLOR), (80, 122))
    screen.blit(font_mid.render("月：", True, TEXT_COLOR), (240, 122))
    screen.blit(font_mid.render("日：", True, TEXT_COLOR), (380, 122))

    # 绘制输入框
    for idx, rect in enumerate(input_boxes):
        color = BOX_ACTIVE if idx == active_box else BOX_COLOR
        pygame.draw.rect(screen, color, rect, border_radius=6)
        pygame.draw.rect(screen, TEXT_COLOR, rect, 2, border_radius=6)
        txt_surf = font_mid.render(box_text[idx], True, TEXT_COLOR)
        screen.blit(txt_surf, (rect.x + 8, rect.y + 4))

    # 查询按钮
    btn_c = BTN_HOVER if hover_btn else BTN_COLOR
    pygame.draw.rect(screen, btn_c, btn_rect, border_radius=8)
    btn_text = font_mid.render("开始查询", True, (255,255,255))
    screen.blit(btn_text, (btn_rect.centerx - btn_text.get_width()//2, btn_rect.y+8))

    # 展示结果
    y_offset = 260
    if result_zodiac:
        t1 = font_mid.render(f"生肖：{result_zodiac}", True, TEXT_COLOR)
        screen.blit(t1, (80, y_offset))
    if result_star:
        t2 = font_mid.render(f"星座：{result_star}", True, TEXT_COLOR)
        screen.blit(t2, (320, y_offset))
    if result_desc:
        t3 = font_small.render(f"简介：{result_desc}", True, (180,200,255))
        screen.blit(t3, (80, y_offset + 40))

    pygame.display.flip()
    clock.tick(60)

pygame.quit()