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

import Foundation
import UIKit
import SnapKit


class LNUserRelationItemCell: UITableViewCell {
    private let avatar = UIImageView()
    private let nameLabel = UILabel()
    private let genderView = LNGenderView()
    private let followLabel = UILabel()
    private let followButton = UIButton()
    
    private var curItem: LNRelationUserVO?
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        setupViews()
        
        LNEventDeliver.addObserver(self)
    }
    
    func update(_ item: LNRelationUserVO) {
        avatar.showAvatar(item.avatar)
        nameLabel.text = item.nickname
        genderView.update(item.gender, item.age)
        
        curItem = item
        
        updateFollowButton()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension LNUserRelationItemCell {
    @objc
    private func handleFollowClick() {
        guard let curItem else { return }
        if curItem.relateion.contains(.followed) == true {
            LNCommonAlertView.showUnfollowAlert(uid: curItem.userNo)
        } else {
            LNRelationManager.shared.operateFollow(uid: curItem.userNo, follow: true, handler: nil)
        }
    }
}

extension LNUserRelationItemCell: LNRelationManagerNotify {
    func onUserRelationChanged(uid: String, relation: LNUserRelationShip) {
        guard curItem?.userNo == uid else { return }
        curItem?.relateion = relation
        updateFollowButton()
    }
}

extension LNUserRelationItemCell {
    private func updateFollowButton() {
        if curItem?.relateion.contains(.followed) == true {
            if curItem?.relateion.contains(.fans) == true {
                followLabel.isHidden = true
                followLabel.text = nil
                followButton.setBackgroundImage(nil, for: .normal)
                followButton.setImage(.icRelationFriend.withRenderingMode(.alwaysTemplate), for: .normal)
                followButton.tintColor = .text_2
            } else {
                followLabel.text = .init(key: "A00235")
                followLabel.isHidden = false
                followLabel.font = .body_xs
                followLabel.textColor = .text_2
                followButton.setBackgroundImage(nil, for: .normal)
                followButton.setImage(nil, for: .normal)
            }
        } else {
            if curItem?.relateion.contains(.fans) == true {
                followLabel.isHidden = true
                followLabel.text = nil
                followButton.setBackgroundImage(.primary_7, for: .normal)
                followButton.setImage(.icRelationFriend.withRenderingMode(.alwaysTemplate), for: .normal)
                followButton.tintColor = .text_1
            } else {
                followLabel.text = .init(key: "A00225")
                followLabel.isHidden = false
                followLabel.font = .heading_h5
                followLabel.textColor = .text_1
                followButton.setBackgroundImage(.primary_7, for: .normal)
                followButton.setImage(nil, for: .normal)
            }
        }
    }
    
    private func setupViews() {
        avatar.layer.cornerRadius = 20
        avatar.clipsToBounds = true
        contentView.addSubview(avatar)
        avatar.snp.makeConstraints { make in
            make.leading.equalToSuperview().offset(21)
            make.verticalEdges.equalToSuperview().inset(15)
            make.width.height.equalTo(40)
        }
        
        nameLabel.font = .heading_h4
        nameLabel.textColor = .text_5
        contentView.addSubview(nameLabel)
        nameLabel.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.leading.equalTo(avatar.snp.trailing).offset(12)
        }
        
        followButton.layer.cornerRadius = 11
        followButton.backgroundColor = .fill_2
        followButton.clipsToBounds = true
        followButton.addTarget(self, action: #selector(handleFollowClick), for: .touchUpInside)
        contentView.addSubview(followButton)
        followButton.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.trailing.equalToSuperview().offset(-16)
            make.height.equalTo(22)
            make.width.greaterThanOrEqualTo(48)
        }
        
        followLabel.font = .body_xs
        followLabel.text = .init(key: "A00235")
        followLabel.textColor = .text_2
        followButton.addSubview(followLabel)
        followLabel.snp.makeConstraints { make in
            make.center.equalToSuperview()
            make.leading.equalToSuperview().offset(5)
        }
        
        contentView.addSubview(genderView)
        genderView.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.leading.equalTo(nameLabel.snp.trailing).offset(4)
            make.trailing.lessThanOrEqualTo(followButton.snp.leading).offset(-12)
        }
    }
}
