//
//  LNUserSearchUserCardView.swift
//  Gami
//
//  Created by OneeChan on 2026/3/19.
//

import Foundation
import UIKit
import SnapKit


class LNUserSearchUserCardView: UIView {
    private let avatar = UIImageView()
    private let onlineView = LNOnlineView()
    private let nameLabel = UILabel()
    private let genderView = LNGenderView()
    private let idLabel = UILabel()
    private let fansCountLabel = UILabel()
    
    private let followButton = UIButton()
    
    private var curItem: LNGameMateSearchResultVO?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupViews()
        LNEventDeliver.addObserver(self)
    }
    
    func update(_ item: LNGameMateSearchResultVO) {
        avatar.showAvatar(item.avatar)
        onlineView.isHidden = !item.online
        nameLabel.text = item.nickname
        genderView.update(item.gender, item.age)
        idLabel.text = "ID \(item.userNo)"
        fansCountLabel.text = .init(key: "A00243", item.fansCount)
        
        followButton.isHidden = item.userNo.isMyUid
        
        curItem = item
        
        updateFollowButton()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension LNUserSearchUserCardView: LNRelationManagerNotify {
    func onUserRelationChanged(uid: String, relation: LNUserRelationShip) {
        guard uid == curItem?.userNo else { return }
        curItem?.follow = relation.contains(.followed)
        updateFollowButton()
    }
}

extension LNUserSearchUserCardView {
    private func updateFollowButton() {
        if curItem?.follow == true {
            followButton.isEnabled = false
            followButton.setBackgroundImage(nil, for: .normal)
            followButton.setTitle(.init(key: "A00235"), for: .normal)
            followButton.titleLabel?.font = .body_xs
            followButton.setTitleColor(.text_2, for: .normal)
        } else {
            followButton.isEnabled = true
            followButton.setBackgroundImage(.primary_7, for: .normal)
            followButton.setTitle(.init(key: "A00225"), for: .normal)
            followButton.titleLabel?.font = .heading_h5
            followButton.setTitleColor(.text_1, for: .normal)
        }
    }
    
    private func setupViews() {
        avatar.layer.cornerRadius = 20
        avatar.clipsToBounds = true
        addSubview(avatar)
        avatar.snp.makeConstraints { make in
            make.width.height.equalTo(40)
            make.leading.equalToSuperview().offset(18)
            make.top.equalToSuperview().offset(2)
            make.bottom.equalToSuperview().offset(-22).priority(.medium)
        }
        
        addSubview(onlineView)
        onlineView.snp.makeConstraints { make in
            make.edges.equalTo(avatar).inset(-2)
        }
        
        let follow = buildFollow()
        addSubview(follow)
        follow.snp.makeConstraints { make in
            make.centerY.equalTo(avatar)
            make.trailing.equalToSuperview().offset(-16)
        }
        
        let infoView = buildInfoView()
        addSubview(infoView)
        infoView.snp.makeConstraints { make in
            make.leading.equalTo(avatar.snp.trailing).offset(12)
            make.centerY.equalTo(avatar)
            make.trailing.equalTo(follow.snp.leading).offset(-12)
        }
        
        onTap { [weak self] in
            guard let self else { return }
            guard let curItem else { return }
            pushToProfile(uid: curItem.userNo)
        }
    }
    
    private func buildInfoView() -> UIView {
        let container = UIView()
        
        let nameView = buildNameView()
        container.addSubview(nameView)
        nameView.snp.makeConstraints { make in
            make.leading.top.equalToSuperview()
            make.trailing.equalToSuperview()
        }
        
        let descView = buildDescView()
        container.addSubview(descView)
        descView.snp.makeConstraints { make in
            make.leading.trailing.equalToSuperview()
            make.bottom.equalToSuperview()
            make.top.equalTo(nameView.snp.bottom).offset(4)
        }
        
        return container
    }
    
    private func buildNameView() -> UIView {
        let container = UIView()
        
        nameLabel.font = .heading_h4
        nameLabel.textColor = .text_5
        container.addSubview(nameLabel)
        nameLabel.snp.makeConstraints { make in
            make.leading.equalToSuperview()
            make.verticalEdges.equalToSuperview()
        }
        
        container.addSubview(genderView)
        genderView.snp.makeConstraints { make in
            make.leading.equalTo(nameLabel.snp.trailing).offset(4)
            make.centerY.equalToSuperview()
            make.trailing.lessThanOrEqualToSuperview()
        }
        
        return container
    }
    
    private func buildDescView() -> UIView {
        let container = UIView()
        
        idLabel.font = .body_xs
        idLabel.textColor = .text_3
        container.addSubview(idLabel)
        idLabel.snp.makeConstraints { make in
            make.leading.equalToSuperview()
            make.verticalEdges.equalToSuperview()
        }
        
        fansCountLabel.font = .body_xs
        fansCountLabel.textColor = .text_3
        container.addSubview(fansCountLabel)
        fansCountLabel.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.leading.equalTo(idLabel.snp.trailing).offset(7)
            make.trailing.lessThanOrEqualToSuperview()
        }
        
        return container
    }
    
    private func buildFollow() -> UIView {
        followButton.layer.cornerRadius = 11
        followButton.clipsToBounds = true
        followButton.backgroundColor = .fill_2
        followButton.titleLabel?.font = .heading_h5
        
        followButton.setBackgroundImage(.primary_7, for: .normal)
        followButton.setTitle(.init(key: "A00225"), for: .normal)
        
        followButton.addAction(UIAction(handler: { [weak self] _ in
            guard let self else { return }
            guard let uid = curItem?.userNo else { return }
            LNRelationManager.shared.operateFollow(uid: uid, follow: true, handler: nil)
        }), for: .touchUpInside)
        
        followButton.snp.makeConstraints { make in
            make.width.equalTo(56)
            make.height.equalTo(22)
        }
        
        return followButton
    }
}

