//
//  QSLContactCell.swift
//  QuickSearchLocation
//
//  Created by Destiny on 2024/12/11.
//

import UIKit

protocol QSLContactCellDelegate: NSObjectProtocol {
    
    func resortClickAction(model: QSLContactModel)
    
    func favorClickAction(model: QSLContactModel)
    
    func deleteClickAction(model: QSLContactModel)
}

class QSLContactCell: UITableViewCell {
    
    weak var delegate: QSLContactCellDelegate?
    
    var model: QSLContactModel?
    
    lazy var bgView: UIView = {
        
        let view = UIView()
        view.addRadius(radius: 8.rpx)
        view.backgroundColor = .white
        return view
    }()
    
    lazy var avatarImageView: UIImageView = {
        
        let imageView = UIImageView()
        imageView.image = UIImage(named: "friends_cell_other_avatar")
        return imageView
    }()
    
    lazy var nameLabel: UILabel = {
        
        let label = UILabel()
        label.text("儿子")
        label.mediumFont(16)
        label.textColor = QSLColor.Color_202020
        return label
    }()
    
    lazy var resortBtn: UIButton = {
        
        let btn = UIButton()
        btn.image(UIImage(named: "contact_resort_btn"))
        btn.addTarget(self, action: #selector(resortBtnAction), for: .touchUpInside)
        return btn
    }()
    
    lazy var lineView: UIView = {
        
        let view = UIView()
        view.backgroundColor = .hexStringColor(hexString: "#F0F0F0", alpha: 0.2)
        return view
    }()
    
    lazy var selectBtn: UIButton = {
        
        let btn = UIButton()
        btn.image(UIImage(named: "contact_btn_unselected"), .normal)
        btn.image(UIImage(named: "contact_btn_selected"), .selected)
        btn.title("默认该联系人")
        btn.font(13)
        btn.textColor(QSLColor.Color_202020)
        btn.setImageTitleLayout(.imgLeft, spacing: 6.rpx)
        btn.addTarget(self, action: #selector(favorBtnAction), for: .touchUpInside)
        return btn
    }()
    
    lazy var deleteBtn: UIButton = {
       
        let btn = UIButton()
        btn.image(UIImage(named: "contact_delete_btn"))
        btn.title("删除")
        btn.font(13)
        btn.textColor(QSLColor.Color_202020)
        btn.setImageTitleLayout(.imgLeft, spacing: 4.rpx)
        btn.addTarget(self, action: #selector(deleteBtnAction), for: .touchUpInside)
        return btn
    }()
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        initView()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func config(model: QSLContactModel) {
        
        self.model = model
        
        self.nameLabel.text = model.remark.count > 0 ? model.remark : model.phone
        
        self.selectBtn.isSelected = model.favor
    }
}

extension QSLContactCell {
    
    @objc func resortBtnAction() {
        
        if let model = self.model {
            delegate?.resortClickAction(model: model)
        }
    }
    
    @objc func favorBtnAction() {
        
        if let model = self.model {
            delegate?.favorClickAction(model: model)
        }
    }
    
    @objc func deleteBtnAction() {
        
        if let model = self.model {
            delegate?.deleteClickAction(model: model)
        }
    }
}

extension QSLContactCell {
    
    func initView() {
        
        self.backgroundColor = .clear
        self.contentView.backgroundColor = .clear
        
        self.contentView.addSubview(bgView)
        bgView.snp.makeConstraints { make in
            make.edges.equalTo(0)
        }
        
        bgView.addSubview(avatarImageView)
        avatarImageView.snp.makeConstraints { make in
            make.size.equalTo(CGSize(width: 48.rpx, height: 48.rpx))
            make.left.top.equalTo(16.rpx)
        }
        
        bgView.addSubview(nameLabel)
        nameLabel.snp.makeConstraints { make in
            make.centerY.equalTo(avatarImageView.snp.centerY)
            make.left.equalTo(avatarImageView.snp.right).offset(12.rpx)
        }
        
        bgView.addSubview(resortBtn)
        resortBtn.snp.makeConstraints { make in
            make.size.equalTo(CGSize(width: 92.rpx, height: 32.rpx))
            make.right.equalTo(-16.rpx)
            make.centerY.equalTo(avatarImageView.snp.centerY)
        }
        
        bgView.addSubview(lineView)
        lineView.snp.makeConstraints { make in
            make.left.equalTo(16.rpx)
            make.right.equalTo(-16.rpx)
            make.top.equalTo(76.rpx)
            make.height.equalTo(1.rpx)
        }
        
        bgView.addSubview(selectBtn)
        selectBtn.snp.makeConstraints { make in
            make.size.equalTo(CGSize(width: 100.rpx, height: 20.rpx))
            make.left.equalTo(lineView.snp.left)
            make.top.equalTo(lineView.snp.bottom).offset(8.rpx)
        }
        
        bgView.addSubview(deleteBtn)
        deleteBtn.snp.makeConstraints { make in
            make.size.equalTo(CGSize(width: 50.rpx, height: 20.rpx))
            make.right.equalTo(lineView.snp.right)
            make.centerY.equalTo(selectBtn.snp.centerY)
        }
    }
}
