//
//  SettingsAccountView.swift
//  punchnet
//
//  Created by 安礼成 on 2026/1/16.
//

import SwiftUI

struct SettingsAccountView: View {
    @State var state: SettingsState = SettingsState()
    @Environment(UserContext.self) var userContext: UserContext
    
    var body: some View {
        
        ScrollView(.vertical, showsIndicators: false) {
            VStack(alignment: .leading) {
                Text("账户")
                
                if let loginCredit = userContext.loginCredit {
                    switch loginCredit {
                    case .token(let token):
                        TokenCreditView(token: token)
                    case .accountAndPasword(let account, let password):
                        AccountCreditView(username: account)
                    }
                }
                
                Text("网络")
                
                HStack(alignment: .top) {
                    Text("默认网络")
                    
                    VStack(alignment: .leading) {
                        Menu {
                            ForEach(state.networks, id: \.id) { network in
                                Button(network.name) {
                                    self.state.selectedNetwork = network
                                }
                            }
                        } label: {
                            Text(state.selectedNetwork.name)
                                .padding()
                                .background(Color.gray.opacity(0.2))
                                .cornerRadius(5)
                        }
                        
                        Button {
                            
                        } label: {
                            Text("进入管理平台")
                        }
                        
                    }
                    
                    Button {
                        
                    } label: {
                        Text("详情")
                    }
                }
                
            }
        }
        .padding()
        
    }
    
}

extension SettingsAccountView {
    
    // 基于账号密码的登陆
    struct AccountCreditView: View {
        @Environment(UserContext.self) var userContext: UserContext
        
        let username: String
        
        var body: some View {
            HStack {
                Image("logo")
                    .resizable()
                    .frame(width: 20, height: 20)
                
                Text(username)
                
                Spacer()
                
                Button {
                    
                } label: {
                    Text("修改密码")
                }
                
                Button {
                    Task { @MainActor in
                        try await VPNManager.shared.disableVpn()
                        self.userContext.isLogined = false
                    }
                } label: {
                    Text("退出登陆")
                }
            }
            .frame(width: 400)
        }
        
    }
    
    // 基于Token的登陆
    struct TokenCreditView: View {
        @Environment(UserContext.self) var userContext: UserContext
        let token: String
        
        var body: some View {
            HStack {
                Image("logo")
                    .resizable()
                    .frame(width: 20, height: 20)
                
                Text(token)
                
                Spacer()
                
                Button {
                    Task { @MainActor in
                        try await VPNManager.shared.disableVpn()
                        self.userContext.isLogined = false
                    }
                } label: {
                    Text("退出登陆")
                }
                
            }
            .frame(width: 400)
        }
        
    }
}

#Preview {
    SettingsAccountView()
}
