//
//  AccountManageController.swift

//
//  Created by Leon on 2025/7/23.
//

import UIKit

class AccountManageController: CLBaseViewController {
    var deleteAccountButton: StyleButton!
    var titleView: TitleView!

    var thirdAccountBlock: UIView!
    var thirdAccountIcon: UIImageView!
    var thirdStackV : UIStackView!
    var thirdAccountLabel: UILabel!
    var thirdEmailLabel : UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        setupViews()
        setupDats()
        setupEvents()
    }

    private func setupViews() {
        let title = "Account Manage"
        navigationView.alpha0Title = title

        deleteAccountButton = {
            let v = StyleButton(type: .custom)
            v.defaultDestructive(size: .large)
            v.addTarget(self, action: #selector(deleteAccountAction), for: .touchUpInside)
            view.addSubview(v)
            v.snp.makeConstraints { make in
                make.leading.equalToSuperview().offset(CGFloat.lrs)
                make.trailing.equalToSuperview().offset(-CGFloat.lrs)
                make.bottom.equalToSuperview().offset(-16 - UIWindow.safeAreaBottom * 0.5)
            }
            v.setTitle("Delete Account", for: .normal)
            return v
        }()

        titleView = {
            let v = TitleView()
            v.title = title
            view.addSubview(v)
            v.snp.makeConstraints { make in
                make.leading.equalToSuperview()
                make.top.equalTo(navigationView.snp.bottom)
                make.trailing.equalToSuperview()
            }
            return v
        }()

        thirdAccountBlock = {
            let v = UIView()
            v.backgroundColor = .c.csbn
            v.layer.cornerRadius = 16
            v.layer.masksToBounds = true
            view.addSubview(v)
            v.snp.makeConstraints { make in
                make.leading.equalToSuperview().offset(CGFloat.lrs)
                make.top.equalTo(titleView.snp.bottom).offset(24)
                make.trailing.equalToSuperview().offset(-CGFloat.lrs)
                make.height.equalTo(80)
            }
            return v
        }()

        thirdAccountIcon = {
            let v = UIImageView()
            v.image = MWIconFont.image(fromIcon: .socialGoogle, size: CGSize(width: 24, height: 24), color: .c.ctpn)
            v.contentMode = .center
            v.layer.cornerRadius = 24
            v.layer.masksToBounds = true
            v.layer.borderWidth = 1
            v.layer.borderColor = UIColor.c.con.cgColor
            thirdAccountBlock.addSubview(v)
            v.snp.makeConstraints { make in
                make.leading.equalToSuperview().offset(16)
                make.centerY.equalToSuperview()
                make.width.equalTo(48)
                make.height.equalTo(48)
            }
            return v
        }()
        
        thirdStackV = {
            let v = UIStackView()
            v.axis = .vertical
            v.alignment = .leading
            v.spacing = 4
            thirdAccountBlock .addSubview(v)
            v.snp.makeConstraints { make in
                make.leading.equalTo(thirdAccountIcon.snp.trailing).offset(12)
                make.trailing.lessThanOrEqualToSuperview().offset(-16)
                make.centerY.equalToSuperview()
            }
            return v
        }()

        thirdAccountLabel = {
            let v = UILabel()
            v.text = "Third Account"
            v.font = .t.tts
            v.textColor = .c.ctpn
            thirdStackV.addArrangedSubview(v)
            return v
        }()
        
        thirdEmailLabel = {
            let v = UILabel()
            v.textColor = .c.ctsn
            v.font = .t.tbs
            thirdStackV.addArrangedSubview(v)
            v.text = "-"
            return v
        }()
    }

    private func setupDats() {
        guard let user = UserCore.shared.user else {return}
        
        let thirdType = user.thirdType ?? .APPLE
        switch thirdType {
        case .APPLE:
            thirdAccountIcon.image = MWIconFont.image(fromIcon: .socialApple, size: CGSize(width: 24, height: 24), color: .c.ctpn)
        case .GOOGLE:
            thirdAccountIcon.image = MWIconFont.image(fromIcon: .socialGoogle, size: CGSize(width: 24, height: 24), color: .c.ctpn)
        case .DISCORD:
            thirdAccountIcon.image = MWIconFont.image(fromIcon: .socialDiscord, size: CGSize(width: 24, height: 24), color: .c.ctpn)
//        default:
//            break
        }
        
        thirdAccountLabel.text = user.thirdNickname ?? "-"
        
        if let email = user.thirdEmail, email.count > 0{
            thirdEmailLabel.text = email
        }else {
            thirdEmailLabel.text = "-"
        }
        
    }

    private func setupEvents() {
    }

    @objc func deleteAccountAction() {
        let alert = Alert(title: "Delete Account", text: "Are you sure you want to delete your account?")
        let action1 = AlertAction(title: "Delete", actionStyle: .destructive) {[weak self] in
            self?.doDeleteAccount()
        }
        let action2 = AlertAction(title: "Cancel", actionStyle: .cancel) {
        }
        alert.addAction(action1)
        alert.addAction(action2)
        alert.show()
    }
    
    private func doDeleteAccount(){
//        #warning("test")
//        Hud.toast(str: "账号删除成功") { _ in
//            UserCore.shared.logout()
//            AppRouter.goBackRootController {
//                //NotificationCenter.post(name: .presentSignInVc, object: nil, userInfo: nil)
//            }
//        }
        
        Hud.showIndicator()
        UserProvider.request(UserAPI.userDel) { result in
            Hud.hideIndicator()
            switch result{
            case .success:
                Hud.toast(str: "账号删除成功") { _ in
                    UserCore.shared.logout()
                    AppRouter.goBackRootController {
                        
                    }
                }
            case .failure:
                return
            }
        }
    }
}
