//
//  LNMineUserInfoView.swift
//  Lanu
//
//  Created by OneeChan on 2025/12/19.
//

import Foundation
import UIKit
import SnapKit


class LNMineUserInfoView: UIView {
    private let avatar = UIImageView()
    private let nameLabel = UILabel()
    private let idLabel = UILabel()
    private let fansCountLabel = UILabel()
    private let followCountLabel = UILabel()
    private let visitorCountLabel = UILabel()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupViews()
        
        LNEventDeliver.addObserver(self)
        onUserInfoChanged(userInfo: myUserInfo)
        onMyRelationInfoChanged()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension LNMineUserInfoView: LNProfileManagerNotify, LNRelationManagerNotify {
    func onUserInfoChanged(userInfo: LNUserProfileVO) {
        guard userInfo.userNo.isMyUid else { return }
        avatar.showAvatar(userInfo.avatar)
        nameLabel.text = userInfo.nickname
        idLabel.text = userInfo.userNo
    }
    
    func onMyRelationInfoChanged() {
        fansCountLabel.text = "\(myRelationInfo.fansCount)"
        followCountLabel.text = "\(myRelationInfo.followCount)"
        visitorCountLabel.text = "\(myRelationInfo.visitCount)"
    }
}

extension LNMineUserInfoView {
    private func setupViews() {
        let container = UIView()
        container.backgroundColor = .fill
        container.layer.cornerRadius = 12
        addSubview(container)
        container.snp.makeConstraints { make in
            make.horizontalEdges.equalToSuperview()
            make.bottom.equalToSuperview()
            make.top.equalToSuperview().offset(16)
        }
        
        let home = UIImageView()
        home.image = .icMineHome
        home.onTap { [weak self] in
            guard let self else { return }
            pushToProfile(uid: myUid)
        }
        container.addSubview(home)
        home.snp.makeConstraints { make in
            make.top.trailing.equalToSuperview()
            make.size.equalTo(home.image?.size ?? .zero)
        }
        
        let avatar = buildAvatar()
        addSubview(avatar)
        avatar.snp.makeConstraints { make in
            make.leading.equalToSuperview().offset(16)
            make.top.equalToSuperview()
        }
        
        let userInfo = buildUserInfo()
        container.addSubview(userInfo)
        userInfo.snp.makeConstraints { make in
            make.leading.equalTo(avatar.snp.trailing).offset(10)
            make.bottom.equalTo(avatar)
            make.trailing.equalTo(home.snp.leading).offset(6)
        }
        
        let relation = buildRelation()
        container.addSubview(relation)
        relation.snp.makeConstraints { make in
            make.horizontalEdges.equalToSuperview().inset(16)
            make.bottom.equalToSuperview().offset(-16)
            make.top.equalTo(userInfo.snp.bottom).offset(16)
        }
    }
    
    private func buildAvatar() -> UIView {
        avatar.layer.cornerRadius = 35
        avatar.layer.borderWidth = 2
        avatar.layer.borderColor = UIColor.fill.cgColor
        avatar.backgroundColor = .fill
        avatar.clipsToBounds = true
        avatar.contentMode = .scaleAspectFill
        avatar.onTap { [weak self] in
            guard let self else { return }
            pushToProfile(uid: myUid)
        }
        avatar.snp.makeConstraints { make in
            make.width.height.equalTo(70)
        }
        
        return avatar
    }
    
    private func buildRelation() -> UIView {
        let stackView = UIStackView()
        stackView.axis = .horizontal
        stackView.spacing = 16
        stackView.distribution = .fillEqually
        
        let followView = buildUserCountView(title: .init(key: "A00213"), countLabel: followCountLabel)
        followView.onTap { [weak self] in
            guard let self else { return }
            pushToRelationList(uid: myUid, tabType: .follow)
        }
        stackView.addArrangedSubview(followView)
        
        let line1 = buildLine()
        stackView.addSubview(line1)
        line1.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.centerX.equalTo(followView.snp.trailing)
        }
        
        let fansView = buildUserCountView(title: .init(key: "A00214"), countLabel: fansCountLabel)
        fansView.onTap { [weak self] in
            guard let self else { return }
            pushToRelationList(uid: myUid, tabType: .fans)
        }
        stackView.addArrangedSubview(fansView)
        
        let line2 = buildLine()
        stackView.addSubview(line2)
        line2.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.centerX.equalTo(fansView.snp.trailing)
        }
        
        let visitorView = buildUserCountView(title: .init(key: "B00109"), countLabel: visitorCountLabel)
        visitorView.onTap { [weak self] in
            guard let self else { return }
            pushToVisitors()
        }
        stackView.addArrangedSubview(visitorView)
        
        return stackView
    }
    
    private func buildUserCountView(title: String, countLabel: UILabel) -> UIView {
        let container = UIView()
        
        countLabel.font = .heading_h2
        countLabel.textColor = .text_5
        container.addSubview(countLabel)
        countLabel.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.leading.greaterThanOrEqualToSuperview()
            make.top.equalToSuperview()
        }
        
        let textLabel = UILabel()
        textLabel.text = title
        textLabel.font = .body_s
        textLabel.textColor = .text_3
        textLabel.textAlignment = .center
        container.addSubview(textLabel)
        textLabel.snp.makeConstraints { make in
            make.horizontalEdges.equalToSuperview()
            make.bottom.equalToSuperview()
            make.top.equalTo(countLabel.snp.bottom).offset(2)
        }
        
        return container
    }
    
    private func buildLine() -> UIView {
        let line = UIView()
        line.backgroundColor = .init(hex: "#D9D9D9")
        line.snp.makeConstraints { make in
            make.width.equalTo(1)
            make.height.equalTo(22)
        }
        
        return line
    }
    
    private func buildUserInfo() -> UIView {
        let container = UIView()
        
        nameLabel.font = .heading_h1_5
        nameLabel.textColor = .text_5
        container.addSubview(nameLabel)
        nameLabel.snp.makeConstraints { make in
            make.horizontalEdges.equalToSuperview()
            make.top.equalToSuperview()
        }
        
        let ID = UILabel()
        ID.text = .init(key: "ID")
        ID.font = .heading_h5
        ID.textColor = .text_3
        container.addSubview(ID)
        ID.snp.makeConstraints { make in
            make.leading.bottom.equalToSuperview()
            make.top.equalTo(nameLabel.snp.bottom).offset(2)
        }
        
        idLabel.font = .body_xs
        idLabel.textColor = .text_4
        container.addSubview(idLabel)
        idLabel.snp.makeConstraints { make in
            make.leading.equalTo(ID.snp.trailing).offset(4)
            make.centerY.equalTo(ID)
        }
        
        let copyButton = UIButton()
        copyButton.setImage(.icCopyLeft, for: .normal)
        copyButton.addAction(UIAction(handler: { _ in
            let pasteboard = UIPasteboard.general
            pasteboard.string = myUid
            
            showToast(.init(key: "A00047"))
        }), for: .touchUpInside)
        container.addSubview(copyButton)
        copyButton.snp.makeConstraints { make in
            make.leading.equalTo(idLabel.snp.trailing).offset(4)
            make.centerY.equalTo(ID)
            make.trailing.lessThanOrEqualToSuperview()
        }
        
        return container
    }
}

#if DEBUG

import SwiftUI

struct LNMineUserInfoViewPreview: UIViewRepresentable {
    func makeUIView(context: Context) -> some UIView {
        let container = UIView()
        container.backgroundColor = .lightGray
        
        let view = LNMineUserInfoView()
        container.addSubview(view)
        view.snp.makeConstraints { make in
            make.leading.trailing.centerY.equalToSuperview()
        }
        
        return container
    }
    
    func updateUIView(_ uiView: UIViewType, context: Context) { }
}

#Preview(body: {
    LNMineUserInfoViewPreview()
})
#endif
