#include "ui.h"  
#include <stdio.h>  
#include <stdlib.h>  
#include <string.h>  

#ifdef _WIN32  
#define CLEAR_COMMAND "cls"  
#else  
#define CLEAR_COMMAND "clear"  
#endif  

void ui_init() {  
    system(CLEAR_COMMAND);  
    printf("\n%s%s╔══════════════════════════════════════════════════╗%s\n", COLOR_BOLD, COLOR_CYAN, COLOR_RESET);  
    printf("%s%s║         文件压缩/解压缩系统 - 欢迎使用           ║%s\n", COLOR_BOLD, COLOR_CYAN, COLOR_RESET);  
    printf("%s%s╚══════════════════════════════════════════════════╝%s\n\n", COLOR_BOLD, COLOR_CYAN, COLOR_RESET);  
}  

void ui_cleanup() {  
    printf("\n%s%s╔══════════════════════════════════════════════════╗%s\n", COLOR_BOLD, COLOR_CYAN, COLOR_RESET);  
    printf("%s%s║            感谢使用，再见！                    ║%s\n", COLOR_BOLD, COLOR_CYAN, COLOR_RESET);  
    printf("%s%s╚══════════════════════════════════════════════════╝%s\n", COLOR_BOLD, COLOR_CYAN, COLOR_RESET);  
}  

char ui_show_main_menu() {  
    printf("\n%s%s┌──────────────── 主菜单 ────────────────┐%s\n", COLOR_BOLD, COLOR_YELLOW, COLOR_RESET);  
    printf("%s│  [C] 压缩文件                          │%s\n", COLOR_YELLOW, COLOR_RESET);  
    printf("%s│  [D] 解压文件                          │%s\n", COLOR_YELLOW, COLOR_RESET);  
    printf("%s│  [Q] 退出程序                          │%s\n", COLOR_YELLOW, COLOR_RESET);  
    printf("%s└────────────────────────────────────────┘%s\n", COLOR_YELLOW, COLOR_RESET);  
    
    printf("\n%s请输入您的选择 > %s", COLOR_GREEN, COLOR_RESET);  
    
    char choice;  
    scanf(" %c", &choice);  
    while (getchar() != '\n');  
    return choice;  
}  

void ui_prompt_filename(const char* prompt, char* buffer, size_t size) {  
    printf("\n%s%s%s%s", COLOR_BOLD, COLOR_BLUE, prompt, COLOR_RESET);  
    fgets(buffer, (int)size, stdin);  
    buffer[strcspn(buffer, "\n")] = '\0';  
}  

void ui_prompt_info(const char* prompt, char* buffer, size_t size) {  
    printf("%s%s%s%s", COLOR_BOLD, COLOR_BLUE, prompt, COLOR_RESET);  
    fgets(buffer, (int)size, stdin);  
    buffer[strcspn(buffer, "\n")] = '\0';  
}  

int ui_prompt_encryption_method() {  
    printf("\n%s%s┌──────────── 选择加密方式 ────────────┐%s\n", COLOR_BOLD, COLOR_MAGENTA, COLOR_RESET);  
    printf("%s│  [0] 偏移量加密                      │%s\n", COLOR_MAGENTA, COLOR_RESET);  
    printf("%s│  [1] XOR 加密                        │%s\n", COLOR_MAGENTA, COLOR_RESET);  
    printf("%s└──────────────────────────────────────┘%s\n", COLOR_MAGENTA, COLOR_RESET);  
    
    printf("\n%s请选择加密方式 > %s", COLOR_GREEN, COLOR_RESET);  
    
    char method_choice;  
    scanf(" %c", &method_choice);  
    while (getchar() != '\n');  
    return (method_choice == '1') ? 1 : 0;  
}  

int ui_prompt_decoding_method() {  
    printf("\n%s%s┌──────────── 选择解码方式 ────────────┐%s\n", COLOR_BOLD, COLOR_MAGENTA, COLOR_RESET);  
    printf("%s│  [1] 基于解码树的逐位解码           │%s\n", COLOR_MAGENTA, COLOR_RESET);  
    printf("%s│  [2] 基于编码表的匹配解码           │%s\n", COLOR_MAGENTA, COLOR_RESET);  
    printf("%s│  [3] 基于哈希的快速解码             │%s\n", COLOR_MAGENTA, COLOR_RESET);  
    printf("%s└─────────────────────────────────────┘%s\n", COLOR_MAGENTA, COLOR_RESET);  
    
    printf("\n%s请选择解码方式 (1-3) > %s", COLOR_GREEN, COLOR_RESET);  
    
    int decode_method = 0;  
    scanf("%d", &decode_method);  
    while (getchar() != '\n');  
    return decode_method;  
}  

void ui_display_message(const char* message) {  
    printf("\n%s%s══════════════════════════════════════════%s\n", COLOR_BOLD, COLOR_BLUE, COLOR_RESET);  
    printf("%s%s  %s%s\n", COLOR_BOLD, COLOR_BLUE, message, COLOR_RESET);  
    printf("%s%s══════════════════════════════════════════%s\n", COLOR_BOLD, COLOR_BLUE, COLOR_RESET);  
}  

void ui_display_error(const char* message) {  
    printf("\n%s%s[错误] %s%s\n", COLOR_BOLD, COLOR_RED, message, COLOR_RESET);  
}  

void ui_display_success(const char* message) {  
    printf("\n%s%s[成功] %s%s\n", COLOR_BOLD, COLOR_GREEN, message, COLOR_RESET);  
}  

void ui_display_progress(const char* operation, int percentage) {  
    printf("%s%s[%s] %3d%% ", COLOR_BOLD, COLOR_CYAN, operation, percentage);  
    const int bar_width = 50;  
    int pos = bar_width * percentage / 100;  
    
    printf("[");  
    for (int i = 0; i < bar_width; ++i) {  
        if (i < pos) printf("=");  
        else if (i == pos) printf(">");  
        else printf(" ");  
    }  
    printf("] %s\r", COLOR_RESET);  
    fflush(stdout);  
    
    if (percentage == 100) printf("\n");  
}  

void ui_pause(void) {  
    printf("\n%s按回车键继续...%s", COLOR_YELLOW, COLOR_RESET);  
    getchar();  
}