//
//  QSLRequestCell.swift
//  QuickSearchLocation
//
//  Created by Destiny on 2024/12/5.
//

import UIKit

enum QSLRequestType: Int {
    case wait     = 1
    case agreed   = 2
    case refused  = 3
}

protocol QSLRequestCellDelegate: NSObjectProtocol {
    
    func refuseBtnAction(model: QSLRequestModel)
    
    func accpetBtnAction(model: QSLRequestModel)
}

class QSLRequestCell: UITableViewCell {
    
    weak var delegate: QSLRequestCellDelegate?
    
    var model: QSLRequestModel?
    
    lazy var avatarImageView: UIImageView = {
       
        let imageView = UIImageView()
        imageView.image = UIImage(named: "friends_cell_other_avatar")
        return imageView
    }()
    
    lazy var titleLabel: UILabel = {
       
        let label = UILabel()
        label.text("用户1388888888向您发出了好友申请")
        label.mediumFont(15)
        label.textColor = .hexStringColor(hexString: "#404040")
        label.setSpecificTextColor("1388888888", color: .hexStringColor(hexString: "#15CBA1"))
        return label
    }()
    
    lazy var timeLabel: UILabel = {
        
        let label = UILabel()
        label.text("2023-10-20  09:43")
        label.font(13)
        label.textColor = .hexStringColor(hexString: "#A7A7A7")
        return label
    }()
    
    lazy var refuseBtn: UIButton = {
       
        let btn = UIButton()
        btn.backgroundColor = QSLColor.backGroundColor
        btn.title("拒绝")
        btn.textColor(.hexStringColor(hexString: "#A7A7A7"))
        btn.addRadius(radius: 16.rpx)
        btn.addBorder(borderWidth: 1.rpx, borderColor: .hexStringColor(hexString: "#E2E2E2"))
        btn.addTarget(self, action: #selector(refuseBtnAction), for: .touchUpInside)
        return btn
    }()
    
    lazy var agreeBtn: UIButton = {
       
        let btn = UIButton()
        btn.gradientBackgroundColor(color1: .hexStringColor(hexString: "#15CBA1"), color2: .hexStringColor(hexString: "#15CBA1"), width: 120.rpx, height: 32.rpx, direction: .horizontal)
        btn.title("同意")
        btn.addRadius(radius: 16.rpx)
        btn.addBorder(borderWidth: 1.rpx, borderColor: .hexStringColor(hexString: "#E2E2E2"))
        btn.addTarget(self, action: #selector(agreeBtnAction), 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")
    }
    
    @objc func refuseBtnAction() {
        
        if let model = self.model {
            delegate?.refuseBtnAction(model: model)
        }
    }
    
    @objc func agreeBtnAction() {
        
        if let model = self.model {
            delegate?.accpetBtnAction(model: model)
        }
    }
    
    func config(model: QSLRequestModel) {
        
        self.model = model
        self.titleLabel.text = "用户\(model.userPhone)向您发出了好友申请"
        self.titleLabel.setSpecificTextColor(model.userPhone, color: .hexStringColor(hexString: "#15CBA1"))
        self.timeLabel.text = Date.timestampToFormatterTimeString(timestamp: model.createTime)
        
        switch QSLRequestType(rawValue: model.status) {
        case .wait:
            self.agreeBtn.isHidden = false
            self.refuseBtn.isEnabled = true
            self.refuseBtn.title("拒绝")
            self.refuseBtn.backgroundColor = QSLColor.backGroundColor
            self.refuseBtn.layer.borderColor = UIColor.hexStringColor(hexString: "#E2E2E2").cgColor
            self.refuseBtn.textColor(.hexStringColor(hexString: "#A7A7A7"))
            break
        case .agreed:
            self.agreeBtn.isHidden = true
            self.refuseBtn.isEnabled = false
            self.refuseBtn.backgroundColor = .white
            self.refuseBtn.layer.borderColor = QSLColor.themeMainColor.cgColor
            self.refuseBtn.title("已同意")
            self.refuseBtn.textColor(QSLColor.themeMainColor)
            break
        case .refused:
            self.agreeBtn.isHidden = true
            self.refuseBtn.isEnabled = false
            self.refuseBtn.title("已拒绝")
            self.refuseBtn.backgroundColor = QSLColor.backGroundColor
            self.refuseBtn.layer.borderColor = UIColor.hexStringColor(hexString: "#E2E2E2").cgColor
            self.refuseBtn.textColor(.hexStringColor(hexString: "#A7A7A7"))
            break
        default:
            break
        }
    }
}

extension QSLRequestCell {
    
    func initView() {
        
        contentView.addSubview(avatarImageView)
        avatarImageView.snp.makeConstraints { make in
            make.size.equalTo(CGSize(width: 48.rpx, height: 48.rpx))
            make.left.equalTo(12.rpx)
            make.top.equalTo(20.rpx)
        }
        
        contentView.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { make in
            make.left.equalTo(avatarImageView.snp.right).offset(8.rpx)
            make.right.equalTo(-12.rpx)
            make.bottom.equalTo(avatarImageView.snp.centerY)
        }
        
        contentView.addSubview(timeLabel)
        timeLabel.snp.makeConstraints { make in
            make.left.equalTo(avatarImageView.snp.right).offset(8.rpx)
            make.right.equalTo(-12.rpx)
            make.top.equalTo(titleLabel.snp.bottom).offset(3.rpx)
        }
        
        contentView.addSubview(refuseBtn)
        refuseBtn.snp.makeConstraints { make in
            make.size.equalTo(CGSize(width: 120.rpx, height: 32.rpx))
            make.left.equalTo(avatarImageView.snp.right).offset(8.rpx)
            make.top.equalTo(timeLabel.snp.bottom).offset(16.rpx)
        }
        
        contentView.addSubview(agreeBtn)
        agreeBtn.snp.makeConstraints { make in
            make.size.equalTo(CGSize(width: 120.rpx, height: 32.rpx))
            make.left.equalTo(refuseBtn.snp.right).offset(12.rpx)
            make.top.equalTo(timeLabel.snp.bottom).offset(16.rpx)
        }
    }
}
