//
//  QSLMessageTableViewCell.swift
//  QuickSearchLocation
//
//  Created by Destiny on 2024/11/27.
//

import UIKit

enum QSLMessageType: Int {
    case sent      = 1  // 你的好友请求已经发送
    case accepted  = 2  // 你的好友请求已经被接受
    case refused   = 3  // 你的好友请求已经被拒绝
    case resort    = 4  // 好友发来的求救
    case delete    = 5  // 你的好友删除了你
}

protocol QSLMessageTableViewCellDelegate: NSObjectProtocol {
    
    func contactBtnAction(phone: String)
}

class QSLMessageTableViewCell: UITableViewCell {
    
    weak var delegate: QSLMessageTableViewCellDelegate?
    
    var model: QSLMessageModel?
    
    lazy var avatarImageView: UIImageView = {
       
        let imageView = UIImageView()
        imageView.image = UIImage(named: "friends_cell_other_avatar")
        return imageView
    }()
    
    lazy var nameTitleLabel: UILabel = {
      
        let label = UILabel()
        label.text("儿子")
        label.mediumFont(15)
        label.textColor = .hexStringColor(hexString: "#404040")
        return label
    }()
    
    lazy var timeLabel: UILabel = {
       
        let label = UILabel()
        label.text("2023-10-20  09:43")
        label.textColor = .hexStringColor(hexString: "#A7A7A7")
        label.font(13)
        return label
    }()
    
    lazy var contentLabel: UILabel = {
       
        let label = UILabel()
        label.numberOfLines = 2
        label.text("已拒绝 您的好友申请")
        label.textColor = .hexStringColor(hexString: "#A7A7A7")
        label.font(13)
        label.setSpecificTextColor("已拒绝", color: .hexStringColor(hexString: "#FF5146"))
        return label
    }()
    
    lazy var lineView: UIView = {
       
        let view = UIView()
        view.backgroundColor = .hexStringColor(hexString: "#F0F0F0", alpha: 0.2)
        return view
    }()
    
    lazy var resortView: UIView = {
        
        let view = UIView()
        view.isHidden = true
        view.gradientBackgroundColor(color1: .hexStringColor(hexString: "#FFDFDF", alpha: 0), color2:  .hexStringColor(hexString: "#FFCDCD"), width: QSLConst.qsl_kScreenW, height: 88.rpx, direction: .horizontal)
        return view
    }()
    
    lazy var contactBtn: UIButton = {
        
        let btn = UIButton()
        btn.setBackgroundImage(UIImage(named: "message_contact_btn"), for: .normal)
        btn.addTarget(self, action: #selector(contactBtnAction), for: .touchUpInside)
        return btn
    }()
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        initializeView()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    @objc func contactBtnAction() {
        
        if let model = self.model {
            delegate?.contactBtnAction(phone: model.senderPhone)
        }
    }
    
    func config(model: QSLMessageModel) {
        
        self.model = model
        
        self.nameTitleLabel.text = model.senderPhone
        
        self.timeLabel.text = Date.timestampToFormatterTimeString(timestamp: model.createTime)
        
        switch QSLMessageType(rawValue: model.type) {
        case .sent:
            self.contentLabel.text = "已发出好友申请"
            self.resortView.isHidden = true
            break
        case .accepted:
            self.contentLabel.text = "已同意 您的好友申请"
            self.contentLabel.setSpecificTextColor("已同意", color: .hexStringColor(hexString: "#15CBA1"))
            self.resortView.isHidden = true
            break
        case .refused:
            self.contentLabel.text = "已拒绝 您的好友申请"
            self.contentLabel.setSpecificTextColor("已拒绝", color: .hexStringColor(hexString: "#FF5146"))
            self.resortView.isHidden = true
            break
        case .resort:
            self.contentLabel.text = "您的好友需要紧急求助，请您尽快联系他！"
            self.resortView.isHidden = false
            break
        case .delete:
            self.contentLabel.text = "已删除您的好友"
            self.resortView.isHidden = true
            break
        default:
            break
        }
    }
}

extension QSLMessageTableViewCell {
    
    func initializeView() {
        
        self.contentView.addSubview(resortView)
        resortView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        self.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(12.rpx)
        }
        
        self.contentView.addSubview(nameTitleLabel)
        nameTitleLabel.snp.makeConstraints { make in
            make.left.equalTo(avatarImageView.snp.right).offset(8.rpx)
            make.bottom.equalTo(avatarImageView.snp.centerY)
        }
        
        self.contentView.addSubview(timeLabel)
        timeLabel.snp.makeConstraints { make in
            make.right.equalTo(-12.rpx)
            make.centerY.equalTo(nameTitleLabel.snp.centerY)
        }
        
        self.contentView.addSubview(contentLabel)
        contentLabel.snp.makeConstraints { make in
            make.left.equalTo(avatarImageView.snp.right).offset(8.rpx)
            make.right.equalTo(-129.rpx)
            make.top.equalTo(nameTitleLabel.snp.bottom).offset(5.rpx)
        }
        
        self.contentView.addSubview(lineView)
        lineView.snp.makeConstraints { make in
            make.left.equalTo(68.rpx)
            make.right.equalTo(-20.rpx)
            make.bottom.equalTo(0)
            make.height.equalTo(1.rpx)
        }
        
        self.resortView.addSubview(contactBtn)
        contactBtn.snp.makeConstraints { make in
            make.size.equalTo(CGSize(width: 92.rpx, height: 32.rpx))
            make.right.equalTo(-12.rpx)
            make.bottom.equalTo(-12.rpx)
        }
    }
}
