import pygame
import random

# 初始化 Pygame
pygame.init()

# 窗口设置
WIDTH, HEIGHT = 600, 500
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("🔮 趣味星空占卜师 (纯鼠标版)")

# 颜色定义
BG_COLOR = (15, 15, 35)
TEXT_COLOR = (255, 255, 255)
GOLD = (255, 215, 0)
PANEL_BG = (40, 40, 70)
BTN_COLOR = (60, 60, 100)
BTN_HOVER = (90, 90, 140)
BTN_ACTIVE = (120, 50, 150)

# 字体
try:
    font_title = pygame.font.SysFont("simhei", 32)
    font_btn = pygame.font.SysFont("simhei", 22)
    font_res = pygame.font.SysFont("simhei", 20)
except:
    font_title = pygame.font.Font(None, 32)
    font_btn = pygame.font.Font(None, 22)
    font_res = pygame.font.Font(None, 20)

# 星座数据
ZODIAC_NAMES = ["白羊座","金牛座","双子座","巨蟹座","狮子座","处女座","天秤座","天蝎座","射手座","摩羯座","水瓶座","双鱼座"]
ZODIAC_FORTUNE = {
    "白羊座": "今日宜：冲动消费。忌：看银行卡余额。",
    "金牛座": "今日宜：干饭。忌：别人抢你的肉。",
    "双子座": "今日宜：精神分裂式聊天。忌：被当成骗子。",
    "巨蟹座": "今日宜：躲在被窝里。忌：面对现实。",
    "狮子座": "今日宜：照镜子。忌：被别人的美貌闪瞎。",
    "处女座": "今日宜：挑刺。忌：被别人挑刺。",
    "天秤座": "今日宜：纠结。忌：做决定。",
    "天蝎座": "今日宜：暗中观察。忌：眼神太犀利。",
    "射手座": "今日宜：跑路。忌：被老板抓包。",
    "摩羯座": "今日宜：加班。忌：想下班。",
    "水瓶座": "今日宜：发疯。忌：正常人理解你。",
    "双鱼座": "今日宜：做梦。忌：闹钟响。"
}

# 生肖数据 (结合2026马年)
ZODIAC_ANIMALS = ["鼠","牛","虎","兔","龙","蛇","马","羊","猴","鸡","狗","猪"]
TAISUI_2026 = {
    "马": "值太岁+刑太岁！今年你就像个陀螺，不仅自己转，还容易被人甩飞。建议多睡觉，少折腾！",
    "鼠": "冲太岁！今年你的钱包可能像漏水的船，建议把钱换成砖头藏起来。",
    "兔": "破太岁！今年人际关系容易翻车，建议对同事保持‘礼貌而不失尴尬的微笑’。",
    "牛": "害太岁！今年容易遇到‘猪队友’，建议把队友当成NPC，不要走心。",
    "蛇": "刑太岁！今年脾气容易像爆竹，建议随身携带灭火器。"
}

# 按钮类
class Button:
    def __init__(self, x, y, w, h, text, action=None, value=None):
        self.rect = pygame.Rect(x, y, w, h)
        self.text = text
        self.action = action
        self.value = value
        self.is_hovered = False
        self.is_active = False

    def draw(self, screen):
        if self.is_active:
            color = BTN_ACTIVE
        elif self.is_hovered:
            color = BTN_HOVER
        else:
            color = BTN_COLOR
        pygame.draw.rect(screen, color, self.rect, border_radius=8)
        pygame.draw.rect(screen, GOLD, self.rect, 2, border_radius=8)
        
        txt = font_btn.render(self.text, True, TEXT_COLOR)
        screen.blit(txt, (self.rect.centerx - txt.get_width()//2, self.rect.centery - txt.get_height()//2))

    def check_hover(self, pos):
        self.is_hovered = self.rect.collidepoint(pos)

    def check_click(self, pos):
        return self.rect.collidepoint(pos)

# 游戏状态
mode = "zodiac"  # zodiac / shengxiao
selected_month = None
selected_day = None
selected_year = None
result_lines = []

# 生成按钮
top_buttons = [
    Button(150, 60, 120, 40, "🌟 星座查询", "switch_mode", "zodiac"),
    Button(330, 60, 120, 40, "🐉 生肖查询", "switch_mode", "shengxiao")
]

# 星座查询按钮
month_buttons = [Button(50 + (i%4)*130, 130 + (i//4)*45, 120, 35, f"{i+1}月", "set_month", i+1) for i in range(12)]
day_buttons = [Button(50 + (i%6)*90, 240 + (i//6)*40, 80, 30, str(i+1), "set_day", i+1) for i in range(31)]
query_btn = Button(200, 370, 200, 45, "✨ 施法查询 ✨", "query_zodiac")

# 生肖查询按钮
year_buttons = []
for i, year in enumerate(range(1980, 2010)):
    year_buttons.append(Button(50 + (i%6)*90, 130 + (i//6)*45, 80, 35, str(year), "set_year", year))
shengxiao_query_btn = Button(200, 370, 200, 45, "🔮 算算运势 🔮", "query_shengxiao")

def get_zodiac(m, d):
    if (m == 3 and d >= 21) or (m == 4 and d <= 19): return "白羊座"
    elif (m == 4 and d >= 20) or (m == 5 and d <= 20): return "金牛座"
    elif (m == 5 and d >= 21) or (m == 6 and d <= 21): return "双子座"
    elif (m == 6 and d >= 22) or (m == 7 and d <= 22): return "巨蟹座"
    elif (m == 7 and d >= 23) or (m == 8 and d <= 22): return "狮子座"
    elif (m == 8 and d >= 23) or (m == 9 and d <= 22): return "处女座"
    elif (m == 9 and d >= 23) or (m == 10 and d <= 23): return "天秤座"
    elif (m == 10 and d >= 24) or (m == 11 and d <= 22): return "天蝎座"
    elif (m == 11 and d >= 23) or (m == 12 and d <= 21): return "射手座"
    elif (m == 12 and d >= 22) or (m == 1 and d <= 19): return "摩羯座"
    elif (m == 1 and d >= 20) or (m == 2 and d <= 18): return "水瓶座"
    elif (m == 2 and d >= 19) or (m == 3 and d <= 20): return "双鱼座"
    return None

def handle_action(action, value):
    global mode, selected_month, selected_day, selected_year, result_lines
    
    if action == "switch_mode":
        mode = value
        result_lines = []
        selected_month = selected_day = selected_year = None
    elif action == "set_month":
        selected_month = value
        result_lines = []
    elif action == "set_day":
        selected_day = value
        result_lines = []
    elif action == "set_year":
        selected_year = value
        result_lines = []
    elif action == "query_zodiac":
        if selected_month and selected_day:
            z = get_zodiac(selected_month, selected_day)
            if z:
                result_lines = [f"🌟 你的星座是：{z}", ZODIAC_FORTUNE[z]]
            else:
                result_lines = ["⚠️ 宇宙无法识别这个日期，请检查输入！"]
        else:
            result_lines = ["⚠️ 请先点击月份和日期哦！"]
    elif action == "query_shengxiao":
        if selected_year:
            animal = ZODIAC_ANIMALS[(selected_year - 4) % 12]
            line1 = f"🐉 你出生于 {selected_year} 年，生肖属：【{animal}】"
            taisui = TAISUI_2026.get(animal, "今年运势平稳，适合躺平，不适合翻身，因为容易闪到腰。")
            result_lines = [line1, f"💥 2026马年运势：{taisui}"]
        else:
            result_lines = ["⚠️ 请先点击你的出生年份哦！"]

# 主循环
running = True
clock = pygame.time.Clock()

while running:
    screen.fill(BG_COLOR)
    mouse_pos = pygame.mouse.get_pos()
    
    # 标题
    title = font_title.render("🔮 趣味星空占卜师 🔮", True, GOLD)
    screen.blit(title, (WIDTH//2 - title.get_width()//2, 15))
    
    # 顶部按钮
    for btn in top_buttons:
        btn.is_active = (btn.value == mode)
        btn.check_hover(mouse_pos)
        btn.draw(screen)
        
    # 根据模式绘制按钮
    current_buttons = []
    if mode == "zodiac":
        hint = font_btn.render(f"当前选择: {selected_month or '?'}月 {selected_day or '?'}日", True, TEXT_COLOR)
        screen.blit(hint, (WIDTH//2 - hint.get_width()//2, 100))
        for btn in month_buttons + day_buttons + [query_btn]:
            if btn.action == "set_month": btn.is_active = (btn.value == selected_month)
            elif btn.action == "set_day": btn.is_active = (btn.value == selected_day)
            else: btn.is_active = False
            btn.check_hover(mouse_pos)
            btn.draw(screen)
            current_buttons.append(btn)
    else:
        hint = font_btn.render(f"当前选择: {selected_year or '?'} 年", True, TEXT_COLOR)
        screen.blit(hint, (WIDTH//2 - hint.get_width()//2, 100))
        for btn in year_buttons + [shengxiao_query_btn]:
            if btn.action == "set_year": btn.is_active = (btn.value == selected_year)
            else: btn.is_active = False
            btn.check_hover(mouse_pos)
            btn.draw(screen)
            current_buttons.append(btn)
            
    # 绘制结果
    if result_lines:
        pygame.draw.rect(screen, PANEL_BG, (50, 430, 500, 60), border_radius=10)
        for i, line in enumerate(result_lines):
            res_txt = font_res.render(line, True, GOLD if i==0 else TEXT_COLOR)
            screen.blit(res_txt, (60, 438 + i * 25))

    pygame.display.flip()
    clock.tick(60)
    
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        elif event.type == pygame.MOUSEBUTTONDOWN:
            # 检查顶部按钮
            for btn in top_buttons:
                if btn.check_click(mouse_pos):
                    handle_action(btn.action, btn.value)
            # 检查当前模式按钮
            for btn in current_buttons:
                if btn.check_click(mouse_pos):
                    handle_action(btn.action, btn.value)

pygame.quit()