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

import Foundation
import UIKit
import SnapKit


class LNSkillUserInfoView: UIView {
    private let userNameLabel = UILabel()
    private let genderView = LNGenderView()
    private let idLabel = UILabel()
    private let languageLabel = UILabel()
    
    private let scoreLabel = UILabel()
    private let orderCountLabel = UILabel()
    
    private var curDetail: LNGameMateSkillDetailVO?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupViews()
    }
    
    func update(_ detail: LNGameMateSkillDetailVO) {
        userNameLabel.text = detail.nickname
        genderView.update(detail.gender, detail.age)
        scoreLabel.text = "\(detail.star)"
        orderCountLabel.text = "\(detail.orderCount)"
        idLabel.text = "ID \(detail.userNo)"
        languageLabel.text = detail.languageNames.joined(separator: " / ")
        languageLabel.superview?.isHidden = detail.languageNames.isEmpty
        
        curDetail = detail
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension LNSkillUserInfoView {
    private func setupViews() {
        let star = buildStarView()
        addSubview(star)
        star.snp.makeConstraints { make in
            make.trailing.equalToSuperview().offset(-16)
            make.bottom.equalToSuperview().offset(-8)
        }
        
        let userView = buildUserInfo()
        addSubview(userView)
        userView.snp.makeConstraints { make in
            make.leading.equalToSuperview().offset(16)
            make.verticalEdges.equalToSuperview()
            make.trailing.lessThanOrEqualTo(star.snp.leading).offset(-10)
        }
    }
    
    private func buildUserInfo() -> UIView {
        let userView = UIStackView()
        userView.axis = .vertical
        userView.spacing = 2
        userView.alignment = .leading
        
        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.centerY.equalToSuperview()
        }
        
        let copyButton = UIButton()
        copyButton.setImage(.icCopyRight, 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.verticalEdges.equalToSuperview()
            make.leading.equalTo(idLabel.snp.trailing).offset(4)
            make.trailing.equalToSuperview()
            make.width.height.equalTo(16)
        }
        
        let languageView = UIView()
        userView.addArrangedSubview(languageView)
        
        let languageIc = UIImageView()
        languageIc.image = .icLanguage.withRenderingMode(.alwaysTemplate)
        languageIc.tintColor = .text_2
        languageView.addSubview(languageIc)
        languageIc.snp.makeConstraints { make in
            make.leading.verticalEdges.equalToSuperview()
        }
        
        languageLabel.font = .body_s
        languageLabel.textColor = .text_2
        languageView.addSubview(languageLabel)
        languageLabel.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.trailing.lessThanOrEqualToSuperview()
            make.leading.equalTo(languageIc.snp.trailing).offset(4)
        }
        
        return userView
    }
    
    private func buildStarView() -> UIView {
        let container = UIView()
        
        scoreLabel.font = .heading_h2
        scoreLabel.textColor = .text_1
        container.addSubview(scoreLabel)
        scoreLabel.snp.makeConstraints { make in
            make.top.equalToSuperview()
            make.trailing.equalToSuperview()
        }
        
        let star = LNStarScoreView()
        star.icSize = 18
        star.score = 1.0
        container.addSubview(star)
        star.snp.makeConstraints { make in
            make.leading.equalToSuperview()
            make.centerY.equalTo(scoreLabel)
            make.trailing.equalTo(scoreLabel.snp.leading).offset(-4)
        }
        
        let orderView = UIView()
        container.addSubview(orderView)
        orderView.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.bottom.equalToSuperview()
            make.top.equalTo(scoreLabel.snp.bottom)
        }
        
        let orderLabel = UILabel()
        orderLabel.text = .init(key: "A00041")
        orderLabel.font = .body_xs
        orderLabel.textColor = .text_1
        orderView.addSubview(orderLabel)
        orderLabel.snp.makeConstraints { make in
            make.leading.equalToSuperview()
            make.centerY.equalToSuperview()
        }
        
        orderCountLabel.font = .heading_h5
        orderCountLabel.textColor = .text_1
        orderView.addSubview(orderCountLabel)
        orderCountLabel.snp.makeConstraints { make in
            make.leading.equalTo(orderLabel.snp.trailing).offset(2)
            make.verticalEdges.equalToSuperview()
            make.trailing.equalToSuperview()
        }
        
        return container
    }
}
