//
//  LNProfileUserInfoView.swift
//  Lanu
//
//  Created by OneeChan on 2025/12/15.
//

import Foundation
import UIKit
import SnapKit


class LNProfileUserInfoView: UIView {
    private let userNameLabel = UILabel()
    private let genderView = LNGenderView()
    
    private let idLabel = UILabel()
    private let followCountLabel = UILabel()
    
    private let scoreLabel = UILabel()
    
    private var curDetail: LNUserProfileVO?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupViews()
    }
    
    func update(_ detail: LNUserProfileVO) {
        userNameLabel.text = detail.nickname
        genderView.update(detail.gender, detail.age)
        idLabel.text = "ID \(detail.userNo)"
        followCountLabel.text = .init(key: "A00234", detail.fansCount)
        
        scoreLabel.text = "\(detail.star)"
        scoreLabel.superview?.isHidden = !detail.playmate
        
        curDetail = detail
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension LNProfileUserInfoView {
    private func setupViews() {
        let star = buildStarView()
        addSubview(star)
        star.snp.makeConstraints { make in
            make.trailing.equalToSuperview().offset(-16)
            make.bottom.equalToSuperview().offset(-10)
        }
        
        let userView = UIStackView()
        userView.axis = .vertical
        userView.spacing = 2
        addSubview(userView)
        userView.snp.makeConstraints { make in
            make.leading.equalToSuperview().offset(16)
            make.verticalEdges.equalToSuperview()
            make.trailing.lessThanOrEqualTo(star.snp.leading).offset(-10)
        }
        
        let nameView = UIView()
        userView.addArrangedSubview(nameView)
        
        userNameLabel.font = .heading_h2
        userNameLabel.textColor = .text_1
        nameView.addSubview(userNameLabel)
        userNameLabel.snp.makeConstraints { make in
            make.leading.equalToSuperview()
            make.verticalEdges.equalToSuperview()
        }
        
        nameView.addSubview(genderView)
        genderView.snp.makeConstraints { make in
            make.centerY.equalTo(userNameLabel)
            make.leading.equalTo(userNameLabel.snp.trailing).offset(4)
            make.trailing.lessThanOrEqualToSuperview()
        }
        
        let idView = UIView()
        userView.addArrangedSubview(idView)
        
        idLabel.font = .body_s
        idLabel.textColor = .text_2
        idView.addSubview(idLabel)
        idLabel.snp.makeConstraints { make in
            make.leading.equalToSuperview()
            make.verticalEdges.equalToSuperview()
        }
        
        let copyButton = UIButton()
        copyButton.setImage(.icCopyLeft, for: .normal)
        copyButton.addAction(UIAction(handler: { [weak self] _ in
            guard let self else { return }
            guard let curDetail else { return }
            let pasteboard = UIPasteboard.general
            pasteboard.string = curDetail.userNo
            
            showToast(.init(key: "A00047"))
        }), for: .touchUpInside)
        idView.addSubview(copyButton)
        copyButton.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.leading.equalTo(idLabel.snp.trailing).offset(4)
            make.trailing.lessThanOrEqualToSuperview()
            make.width.height.equalTo(16)
        }
        
        let fansView = UIView()
        userView.addArrangedSubview(fansView)
        
        followCountLabel.font = .body_s
        followCountLabel.textColor = .text_2
        fansView.addSubview(followCountLabel)
        followCountLabel.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
    }
    
    private func buildStarView() -> UIView {
        let container = UIView()
        
        let star = LNStarScoreView()
        star.icSize = 18
        star.score = 1.0
        container.addSubview(star)
        star.snp.makeConstraints { make in
            make.leading.equalToSuperview()
            make.verticalEdges.equalToSuperview()
        }
        
        scoreLabel.font = .heading_h2
        scoreLabel.textColor = .text_1
        container.addSubview(scoreLabel)
        scoreLabel.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.leading.equalTo(star.snp.trailing).offset(4)
            make.trailing.equalToSuperview()
        }
        
        return container
    }
}
