//
//  YHConnectListCellTableViewCell.swift
//  galaxy
//
//  Created by Dufet on 2025/9/25.
//  Copyright © 2025 https://www.galaxy-immi.com. All rights reserved.
//

import UIKit

class YHConnectListCell: UITableViewCell {
    
    static let cellReuseIdentifier = "YHConnectListCell"
    
    var addFriendBlock: ((String?) -> Void) = { (_: String?) in }
    var contact: YHContact = YHContact()
    
    private lazy var avatarImageView: UIImageView = {
        let imageView = UIImageView()
        imageView.layer.borderWidth = 0.5
        imageView.layer.borderColor = UIColor.brandGrayColor3.cgColor
        imageView.layer.cornerRadius = 22
        imageView.clipsToBounds = true
        imageView.contentMode = .scaleAspectFill
        return imageView
    }()

    private lazy var nameLabel: UILabel = {
        let label = UILabel()
        label.font = UIFont.PFSC_B(ofSize: 15)
        label.textColor = UIColor.brandGrayColor8
        return label
    }()

    private lazy var titleLabel: UILabel = {
        let label = UILabel()
        label.font = UIFont.PFSC_R(ofSize: 13)
        label.textColor = UIColor.brandGrayColor8
        return label
    }()

    private lazy var detailLabel: UILabel = {
        let label = UILabel()
        label.font = UIFont.PFSC_R(ofSize: 11)
        label.textColor = UIColor.brandGrayColor6
        label.numberOfLines = 2
        return label
    }()

    lazy var addFriendButton: UIButton = {
        let button = UIButton(type: .custom)
        button.setTitle("添加好友", for: .normal)
        button.titleLabel?.font = UIFont.PFSC_M(ofSize: 11)
        button.setTitleColor(UIColor.brandGrayColor8, for: .normal)
        button.layer.borderWidth = 0.5
        button.layer.borderColor = UIColor.brandGrayColor5.cgColor
        button.addTarget(self, action: #selector(didAddFriendBtnClicked), for: .touchUpInside)
        return button
    }()
    
    private lazy var statusLabel: UILabel = {
        let label = UILabel()
        label.font = UIFont.PFSC_R(ofSize: 11)
        label.textColor = UIColor.brandGrayColor6
        label.text = "已发送好友申请"
        return label
    }()

    private lazy var separatorLine: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor.brandGrayColor3
        return view
    }()

    required init?(coder: NSCoder) {
        super.init(coder: coder)
    }

    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupUI()
    }
    
    @objc func didAddFriendBtnClicked() {
        
        let view = YHFriendRequestView.createView()
        view.userModel = self.contact
        view.sendBlock = {
            [weak self] text in
            guard let self = self else { return }
            self.addFriendBlock(text)
        }
        view.show()
    }

    private func setupUI() {
        selectionStyle = .none

        // 添加所有子视图
        contentView.addSubview(avatarImageView)
        contentView.addSubview(nameLabel)
        contentView.addSubview(titleLabel)
        contentView.addSubview(detailLabel)
        contentView.addSubview(statusLabel)
        contentView.addSubview(addFriendButton)
        contentView.addSubview(separatorLine)

        // SnapKit 约束设置
        avatarImageView.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(20)
            make.top.equalToSuperview().offset(20)
            make.width.height.equalTo(44)
            make.bottom.lessThanOrEqualTo(-20)
        }

        nameLabel.snp.makeConstraints { make in
            make.top.equalTo(avatarImageView.snp.top)
            make.left.equalTo(avatarImageView.snp.right).offset(10)
            make.right.equalTo(-120)
            make.height.equalTo(20)
        }
        
        statusLabel.snp.makeConstraints { make in
            make.right.equalToSuperview().offset(-20)
            make.centerY.equalTo(nameLabel)
            make.height.equalTo(15)
        }

        addFriendButton.snp.makeConstraints { make in
            make.right.equalToSuperview().offset(-20)
            make.bottom.equalTo(nameLabel.snp.bottom)
            make.width.equalTo(68)
            make.height.equalTo(25)
        }

        titleLabel.snp.makeConstraints { make in
            make.left.equalTo(avatarImageView.snp.right).offset(10)
            make.top.equalTo(nameLabel.snp.bottom).offset(3)
            make.right.equalToSuperview().offset(-20)
            make.height.equalTo(18)
        }

        detailLabel.snp.makeConstraints { make in
            make.left.equalTo(avatarImageView.snp.right).offset(10)
            make.top.equalTo(titleLabel.snp.bottom).offset(4)
            make.right.equalToSuperview().offset(-20)
            make.height.equalTo(15)
            make.bottom.lessThanOrEqualTo(-20)
        }

        separatorLine.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(20)
            make.right.equalToSuperview().offset(-20)
            make.bottom.equalToSuperview()
            make.height.equalTo(0.5)
        }
    }

    func updateModel(_ contact: YHContact) {
        
        self.contact = contact
        avatarImageView.sd_setImage(with: URL(string: contact.avatar), placeholderImage: UIImage(named: ""))
        nameLabel.text = contact.username
        detailLabel.text = contact.bio
        
        var text1 = ""
        var text1Arr: [String] = []
        if !self.contact.position.isEmpty {
            text1Arr.append(self.contact.position)
        }
        if !self.contact.industry.isEmpty {
            text1Arr.append(self.contact.industry)
        }
        if text1Arr.count > 0 {
            text1 = text1Arr.joined(separator: " | ")
        }
        titleLabel.text = text1
        
        detailLabel.snp.updateConstraints { make in
            make.top.equalTo(titleLabel.snp.bottom).offset(contact.bio.isEmpty ? 0 : 4)
            make.height.equalTo(contact.bio.isEmpty ? 0 : 15)
        }
        
        if contact.type == YHContactRelation.stranger.rawValue {
            addFriendButton.isHidden = false
            statusLabel.isHidden = true

        } else {
            addFriendButton.isHidden = true
            statusLabel.isHidden = false
            if contact.type == YHContactRelation.friend.rawValue {
                statusLabel.text = "对方已是您的好友"
                
            } else if contact.type == YHContactRelation.unvalidate.rawValue {
                statusLabel.text = "已发送好友申请"
    
            } else {
                statusLabel.text = ""
            }
        }
    }
}
