//
//  LNRoomProfileCardPanel.swift
//  Gami
//
//  Created by OneeChan on 2026/3/16.
//

import Foundation
import UIKit
import SnapKit


class LNRoomProfileCardPanel: LNPopupView {
    private let reportButton = UIButton(type: .system)
    
    private let avatarView = UIImageView()
    
    private let nameLabel = UILabel()
    private let genderView = LNGenderView()
    private let userIdLabel = UILabel()
    
    private let skillSection = LNRoomProfileSkillView()
    private let actionsSection = LNRoomProfileBottomMenu()
    
    private var curDetail: LNUserProfileVO?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupViews()
    }
    
    func load(_ uid: String) {
        reportButton.isHidden = uid.isMyUid
        skillSection.isHidden = true
        actionsSection.update(uid)
        
        LNProfileManager.shared.getUserProfileDetail(uid: uid) { [weak self] detail in
            guard let self else { return }
            guard let detail else {
                dismiss()
                return
            }
            
            avatarView.sd_setImage(with: URL(string: detail.avatar))
            nameLabel.text = detail.nickname
            genderView.update(detail.gender, detail.age)
            userIdLabel.text = "ID \(detail.userNo)"
            
            skillSection.isHidden = uid.isMyUid || detail.skills.isEmpty
            skillSection.update(detail, detail.skills)
            
            curDetail = detail
        }
    }
    
    func toBeExample() {
        avatarView.sd_setImage(with: URL(string: myUserInfo.avatar))
        nameLabel.text = "Super Beautiful Girl"
        genderView.update(.female, 18)
        userIdLabel.text = "ID 12345678"
        
        var skills: [LNGameMateSkillVO] = []
        for type in LNGameMateManager.shared.curGameTypes {
            for skill in type.children {
                let item = LNGameMateSkillVO()
                item.icon = skill.icon
                item.name = skill.name
                item.cover = skill.icon
                item.price = 1
                item.unit = "Match"
                skills.append(item)
                if skills.count >= 3 {
                    break
                }
            }
            if skills.count >= 3 {
                break
            }
        }
        skillSection.update(myUserInfo, skills)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

private extension LNRoomProfileCardPanel {
    func setupViews() {
        container.backgroundColor = .fill_7
        
        let topMenu = buildTopMenu()
        container.addSubview(topMenu)
        topMenu.snp.makeConstraints { make in
            make.top.equalToSuperview()
            make.horizontalEdges.equalToSuperview()
        }
        
        avatarView.layer.cornerRadius = 38
        avatarView.layer.borderWidth = 1
        avatarView.layer.borderColor = UIColor.fill.cgColor
        avatarView.backgroundColor = .fill_7
        avatarView.clipsToBounds = true
        avatarView.contentMode = .scaleAspectFill
        avatarView.onTap { [weak self] in
            guard let self else { return }
            guard let curDetail else { return }
            dismiss()
            pushToProfile(uid: curDetail.userNo)
        }
        container.addSubview(avatarView)
        avatarView.snp.makeConstraints { make in
            make.top.equalToSuperview().offset(-17)
            make.centerX.equalToSuperview()
            make.width.height.equalTo(76)
        }
        
        let stackView = UIStackView()
        stackView.axis = .vertical
        stackView.distribution = .fill
        container.addSubview(stackView)
        stackView.snp.makeConstraints { make in
            make.horizontalEdges.equalToSuperview()
            make.top.equalTo(avatarView.snp.bottom).offset(17)
            make.bottom.equalToSuperview().offset(commonBottomInset)
        }
        
        stackView.addArrangedSubview(buildUserInfo())
        stackView.addArrangedSubview(buildSkillSection())
        stackView.addArrangedSubview(buildActionSection())
    }
    
    func buildTopMenu() -> UIView {
        let container = UIView()
        container.snp.makeConstraints { make in
            make.height.equalTo(56)
        }
        
        reportButton.tintColor = UIColor.text_2.withAlphaComponent(0.6)
        reportButton.setImage(.icWarning, for: .normal)
        reportButton.addAction(UIAction(handler: { [weak self] _ in
            guard let self else { return }
            guard let curDetail else { return }
            dismiss()
            pushToReport(uid: curDetail.userNo)
        }), for: .touchUpInside)
        container.addSubview(reportButton)
        reportButton.snp.makeConstraints { make in
            make.leading.equalToSuperview().offset(16)
            make.top.equalToSuperview().offset(16)
            make.width.height.equalTo(24)
        }
        
        return container
    }
    
    func buildUserInfo() -> UIView {
        let container = UIView()
        
        let nameView = UIView()
        container.addSubview(nameView)
        nameView.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.leading.greaterThanOrEqualToSuperview().offset(16)
            make.top.equalToSuperview().offset(3)
        }
        
        nameLabel.font = .heading_h2
        nameLabel.textColor = .text_1
        nameView.addSubview(nameLabel)
        nameLabel.snp.makeConstraints { make in
            make.leading.equalToSuperview()
            make.verticalEdges.equalToSuperview()
        }
        
        nameView.addSubview(genderView)
        genderView.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.leading.equalTo(nameLabel.snp.trailing).offset(4)
            make.trailing.equalToSuperview()
        }
        
        userIdLabel.font = .body_s
        userIdLabel.textColor = UIColor.text_1.withAlphaComponent(0.5)
        userIdLabel.textAlignment = .center
        container.addSubview(userIdLabel)
        userIdLabel.snp.makeConstraints { make in
            make.top.equalTo(nameView.snp.bottom).offset(4)
            make.centerX.equalToSuperview()
            make.bottom.equalToSuperview().offset(-10)
        }
        
        return container
    }
    
    func buildSkillSection() -> UIView {
        skillSection.panel = self
        
        return skillSection
    }
    
    func buildActionSection() -> UIView {
        actionsSection.panel = self
        
        return actionsSection
    }
}
