import pygame
import sys

# 初始化Pygame
pygame.init()

# 屏幕设置
WIDTH, HEIGHT = 750, 550
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("模拟投票系统")

# 颜色定义
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
GRAY = (128, 128, 128)
LIGHT_GRAY = (220, 220, 220)
BLUE = (70, 130, 180)
LIGHT_BLUE = (173, 216, 230)
GREEN = (34, 139, 34)

# 修复中文显示：使用系统中文字体
def get_chinese_font(size):
    font_names = [
        "SimHei", "Microsoft YaHei", "SimSun",
        "PingFang SC", "Heiti SC",
        "WenQuanYi Micro Hei", "Noto Sans CJK SC"
    ]
    
    for font_name in font_names:
        try:
            font = pygame.font.SysFont(font_name, size)
            test_text = font.render("测试", True, BLACK)
            if test_text.get_width() > 10:
                return font
        except:
            continue
    
    print("警告：未找到支持中文的字体，中文可能显示为方块")
    return pygame.font.Font(None, size)

# 字体设置
font_large = get_chinese_font(36)
font_medium = get_chinese_font(26)
font_small = get_chinese_font(20)

# 投票选项类
class VoteOption:
    def __init__(self, x, y, width, height, name):
        self.rect = pygame.Rect(x, y, width, height)
        self.name = name
        self.votes = 0
        self.is_hovered = False
    
    def draw(self, screen, total_votes):
        # 绘制选项背景
        bg_color = LIGHT_BLUE if self.is_hovered else LIGHT_GRAY
        pygame.draw.rect(screen, bg_color, self.rect, border_radius=5)
        pygame.draw.rect(screen, BLACK, self.rect, 2, border_radius=5)
        
        # 绘制选项名称
        name_text = font_medium.render(self.name, True, BLACK)
        screen.blit(name_text, (self.rect.x + 15, self.rect.y + 12))
        
        # 计算百分比
        if total_votes > 0:
            percent = self.votes / total_votes
        else:
            percent = 0
        
        # 绘制进度条背景
        bar_x = self.rect.x + 180
        bar_y = self.rect.y + 15
        bar_width = self.rect.width - 320
        bar_height = 20
        pygame.draw.rect(screen, WHITE, (bar_x, bar_y, bar_width, bar_height))
        pygame.draw.rect(screen, BLACK, (bar_x, bar_y, bar_width, bar_height), 1)
        
        # 绘制进度条填充
        fill_width = int(bar_width * percent)
        pygame.draw.rect(screen, GREEN, (bar_x, bar_y, fill_width, bar_height))
        
        # 绘制票数和百分比
        vote_text = font_small.render(f"{self.votes}票 ({percent*100:.1f}%)", True, BLACK)
        screen.blit(vote_text, (self.rect.x + self.rect.width - 120, self.rect.y + 15))
    
    def handle_event(self, event):
        if event.type == pygame.MOUSEMOTION:
            self.is_hovered = self.rect.collidepoint(event.pos)
        if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
            if self.rect.collidepoint(event.pos):
                self.votes += 1
                return True
        return False

# 按钮类
class Button:
    def __init__(self, x, y, w, h, text, color=LIGHT_GRAY, hover_color=GRAY):
        self.rect = pygame.Rect(x, y, w, h)
        self.text = text
        self.color = color
        self.hover_color = hover_color
        self.is_hovered = False

    def draw(self, screen):
        current_color = self.hover_color if self.is_hovered else self.color
        pygame.draw.rect(screen, current_color, self.rect, border_radius=5)
        pygame.draw.rect(screen, BLACK, self.rect, 2, border_radius=5)
        text_surface = font_medium.render(self.text, True, BLACK)
        screen.blit(text_surface, (self.rect.x + self.rect.width//2 - text_surface.get_width()//2,
                                  self.rect.y + self.rect.height//2 - text_surface.get_height()//2))

    def handle_event(self, event):
        if event.type == pygame.MOUSEMOTION:
            self.is_hovered = self.rect.collidepoint(event.pos)
        if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
            if self.rect.collidepoint(event.pos):
                return True
        return False

# 主程序
def main():
    # 投票主题
    title = "你最喜欢的编程语言是？"
    
    # 创建投票选项
    option_names = ["Python", "Java", "C++", "JavaScript", "其他"]
    options = []
    start_y = 120
    option_height = 50
    option_width = 600
    option_x = (WIDTH - option_width) // 2
    
    for i, name in enumerate(option_names):
        y = start_y + i * (option_height + 10)
        options.append(VoteOption(option_x, y, option_width, option_height, name))
    
    # 创建按钮
    reset_button = Button(250, 480, 120, 45, "重置投票")
    exit_button = Button(400, 480, 120, 45, "退出系统")
    
    clock = pygame.time.Clock()
    
    while True:
        # 计算总票数
        total_votes = sum(opt.votes for opt in options)
        
        screen.fill(WHITE)
        
        # 绘制大标题
        main_title = font_large.render("模拟投票调查系统", True, BLACK)
        screen.blit(main_title, (WIDTH//2 - main_title.get_width()//2, 20))
        
        # 绘制投票主题
        topic_text = font_medium.render(title, True, BLUE)
        screen.blit(topic_text, (WIDTH//2 - topic_text.get_width()//2, 75))
        
        # 绘制投票选项
        for option in options:
            option.draw(screen, total_votes)
        
        # 绘制总票数
        total_text = font_medium.render(f"总投票数: {total_votes}", True, BLACK)
        screen.blit(total_text, (WIDTH//2 - total_text.get_width()//2, 440))
        
        # 绘制按钮
        reset_button.draw(screen)
        exit_button.draw(screen)
        
        # 操作提示
        hint = font_small.render("点击选项即可投票 | 进度条实时显示占比", True, GRAY)
        screen.blit(hint, (WIDTH//2 - hint.get_width()//2, HEIGHT - 30))
        
        # 事件处理
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
            
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_ESCAPE:
                    pygame.quit()
                    sys.exit()
            
            # 处理选项投票
            for option in options:
                option.handle_event(event)
            
            # 重置按钮
            if reset_button.handle_event(event):
                for option in options:
                    option.votes = 0
            
            # 退出按钮
            if exit_button.handle_event(event):
                pygame.quit()
                sys.exit()
        
        pygame.display.flip()
        clock.tick(60)

if __name__ == "__main__":
    main()