//
//  SubscriptionStatusBar.swift
//  wake
//
//  Created by fairclip on 2025/8/19.
//

import SwiftUI

// MARK: - 订阅状态枚举
enum SubscriptionStatus {
    case free
    case pioneer(expiryDate: Date)
    
    var title: String {
        switch self {
        case .free:
            return "Free"
        case .pioneer:
            return "Pioneer"
        }
    }
    
    var hasExpiry: Bool {
        switch self {
        case .free:
            return false
        case .pioneer:
            return true
        }
    }
    
    var backgroundColor: Color {
        switch self {
        case .free:
            return Theme.Colors.freeBackground // 浅橙色背景
        case .pioneer:
            return Theme.Colors.pioneerBackground // 橙色背景
        } 
    }
    
    var textColor: Color {
        switch self {
        case .free:
            return Theme.Colors.textPrimary
        case .pioneer:
            return Theme.Colors.textPrimary
        }
    }
}

// MARK: - 订阅状态栏组件
struct SubscriptionStatusBar: View {
    let status: SubscriptionStatus
    let onSubscribeTap: (() -> Void)?
    
    init(status: SubscriptionStatus, onSubscribeTap: (() -> Void)? = nil) {
        self.status = status
        self.onSubscribeTap = onSubscribeTap
    }
    
    var body: some View {
        HStack(spacing: 16) {
            VStack(alignment: .leading, spacing: 20) {
                // 订阅类型标题
                Text(status.title)
                    .font(Typography.font(for: .headline, family: .quicksandBold, size: 32))
                    .foregroundColor(status.textColor)
                
                // 过期时间或订阅按钮
                if case .pioneer(let expiryDate) = status {
                    VStack(alignment: .leading, spacing: 4) {
                        Text("Expires on :")
                            .font(Typography.font(for: .body, family: .quicksandRegular))
                            .foregroundColor(status.textColor.opacity(0.7))
                        
                        Text(formatDate(expiryDate))
                           .font(Typography.font(for: .body, family: .quicksandRegular))
                            .foregroundColor(status.textColor)
                    }
                } else {
                    Button(action: {
                        onSubscribeTap?()
                    }) {
                        Text("Subscribe")
                           .font(Typography.font(for: .title, family: .quicksandRegular, size: 16))
                            .foregroundColor(Theme.Colors.textPrimary)
                            .padding(.horizontal, 12)
                            .padding(.vertical, 6)
                            .background(Theme.Gradients.backgroundGradient) 
                            .cornerRadius(Theme.CornerRadius.extraLarge)
                    }
                }
            }
            
            Spacer()
            
            // 播放按钮图标
            Circle()
                .fill(Color.black)
                .frame(width: 60, height: 60)
                .overlay(
                    Image(systemName: "play.fill")
                        .foregroundColor(.white)
                        .font(.title2)
                        .offset(x: 2) // 微调播放图标位置
                )
        }
        .padding(20)
        .background(status.backgroundColor)
        .cornerRadius(20)
        .shadow(color: Color.black.opacity(0.1), radius: 4, x: 0, y: 2)
    }
    
    // MARK: - 日期格式化
    private func formatDate(_ date: Date) -> String {
        let formatter = DateFormatter()
        formatter.dateFormat = "MMMM d, yyyy"
        return formatter.string(from: date)
    }
}

// MARK: - 预览
#Preview("Free Status") {
    VStack(spacing: 20) {
        SubscriptionStatusBar(
            status: .free,
            onSubscribeTap: {
                print("Subscribe tapped")
            }
        )
        .padding()
        
        SubscriptionStatusBar(
            status: .pioneer(expiryDate: Calendar.current.date(byAdding: .month, value: 6, to: Date()) ?? Date())
        )
        .padding()
    }
    .background(Color(.systemGroupedBackground))
}
