//
//  DamusPurpleAccountView.swift
//  damus
//
//  Created by Daniel D’Aquino on 2024-01-26.
//

import SwiftUI

struct DamusPurpleAccountView: View {
    var colorScheme: ColorScheme = .dark
    let damus_state: DamusState
    let account: DamusPurple.Account
    let pfp_size: CGFloat = 90.0
    
    var body: some View {
        VStack {
            ProfilePicView(pubkey: account.pubkey, size: pfp_size, highlight: .custom(Color.black.opacity(0.4), 1.0), profiles: damus_state.profiles, disable_animation: damus_state.settings.disable_animation, damusState: damus_state)
                .background(Color.black.opacity(0.4).clipShape(Circle()))
                .shadow(color: .black, radius: 10, x: 0.0, y: 5)
            
            profile_name
            
            if account.active {
                active_account_badge
            }
            else {
                inactive_account_badge
            }
            
            // TODO: Generalize this view instead of setting up dividers and paddings manually
            VStack {
                HStack {
                    Text("Expiry date", comment: "Label for Purple subscription expiry date")
                    Spacer()
                    let formattedDate = DateFormatter.localizedString(from: account.expiry, dateStyle: .short, timeStyle: .none)
                    Text(formattedDate)
                }
                .padding(.horizontal)
                .padding(.top, 20)
                
                Divider()
                    .padding(.horizontal)
                    .padding(.vertical, 10)
                
                HStack {
                    Text("Account creation", comment: "Label for Purple account creation date")
                    Spacer()
                    let formattedDate = DateFormatter.localizedString(from: account.created_at, dateStyle: .short, timeStyle: .none)
                    Text(formattedDate)
                }
                .padding(.horizontal)
                
                Divider()
                    .padding(.horizontal)
                    .padding(.vertical, 10)
                
                HStack {
                    Text("Subscriber number", comment: "Label for Purple account subscriber number")
                    Spacer()
                    Text(verbatim: "#\(account.subscriber_number)")
                }
                .padding(.horizontal)
                .padding(.bottom, 20)
            }
            .foregroundColor(.white.opacity(0.8))
            .background(.ultraThinMaterial, in: RoundedRectangle(cornerRadius: 12, style: .continuous))
            .padding()
        }
    }
    
    var profile_name: some View {
        let display_name = self.profile_display_name()
        return HStack(alignment: .center, spacing: 5) {
            Text(display_name)
                .font(.title)
                .bold()
                .foregroundStyle(.white)
            
            SupporterBadge(
                percent: nil,
                purple_account: account,
                style: .full,
                text_color: .white
            )
        }
    }
    
    var active_account_badge: some View {
        HStack(spacing: 3) {
            Image("check-circle.fill")
                .resizable()
                .frame(width: 15, height: 15)
            
            Text("Active account", comment: "Badge indicating user has an active Damus Purple account")
                .font(.caption)
                .bold()
        }
        .foregroundColor(Color.white)
        .padding(.vertical, 3)
        .padding(.horizontal, 8)
        .background(PinkGradient)
        .cornerRadius(30.0)
    }
    
    var inactive_account_badge: some View {
        HStack(spacing: 3) {
            Image("warning")
                .resizable()
                .frame(width: 15, height: 15)
            
            Text("Expired account", comment: "Badge indicating user has an expired Damus Purple account")
                .font(.caption)
                .bold()
        }
        .foregroundColor(DamusColors.danger)
        .padding(.vertical, 3)
        .padding(.horizontal, 8)
        .background(DamusColors.dangerTertiary)
        .cornerRadius(30.0)
    }
    
    func profile_display_name() -> String {
        let profile = try? damus_state.profiles.lookup(id: account.pubkey)
        let display_name = DisplayName(profile: profile, pubkey: account.pubkey).displayName
        return display_name
    }
}

#Preview("Active") {
    DamusPurpleAccountView(
        damus_state: test_damus_state,
        account: DamusPurple.Account(
            pubkey: test_pubkey,
            created_at: Date.now,
            expiry: Date.init(timeIntervalSinceNow: 60 * 60 * 24 * 30),
            subscriber_number: 7,
            active: true,
            attributes: []
        )
    )
}

#Preview("Expired") {
    DamusPurpleAccountView(
        damus_state: test_damus_state,
        account: DamusPurple.Account(
            pubkey: test_pubkey,
            created_at: Date.init(timeIntervalSinceNow: -60 * 60 * 24 * 37),
            expiry: Date.init(timeIntervalSinceNow: -60 * 60 * 24 * 7),
            subscriber_number: 7,
            active: false,
            attributes: []
        )
    )
}
