//
//  QSLFriendTableViewCell.swift
//  QuickSearchLocation
//
//  Created by Destiny on 2024/11/26.
//

import UIKit

protocol QSLFriendTableViewCellDelegate: NSObjectProtocol {
    
    func moreBtnAction(model: QSLUserModel, btn: UIButton)
    
    func roadBtnClickAction(model: QSLUserModel)
}

class QSLFriendTableViewCell: UITableViewCell {
    
    weak var delegate: QSLFriendTableViewCellDelegate?
    
    var userModel: QSLUserModel?
    
    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_avatar")
        return imageView
    }()
    
    lazy var nameLabel: UILabel = {
       
        let label = UILabel()
        label.text("用户")
        label.mediumFont(16)
        label.textColor = QSLColor.Color_202020
        return label
    }()
    
    lazy var moreBtn: UIButton = {
       
        let btn = UIButton()
        btn.image(UIImage(named: "friends_cell_more_btn"))
        btn.addTarget(self, action: #selector(moreBtnAction), for: .touchUpInside)
        return btn
    }()
    
    lazy var locateIcon: UIImageView = {
       
        let imageView = UIImageView()
        imageView.image = UIImage(named: "friends_cell_location")
        return imageView
    }()
    
    lazy var addrLabel: UILabel = {
       
        let label = UILabel()
        label.numberOfLines = 0
        label.font(13)
        label.textColor = .hexStringColor(hexString: "#404040")
        label.text = "未知"
        label.changeLineSpace(space: 4)
        return label
    }()
    
    lazy var lineView: UIView = {
       
        let view = UIView()
        view.backgroundColor = .hexStringColor(hexString: "#F0F0F0", alpha: 0.2)
        return view
    }()
    
    lazy var timeLabel: UILabel = {
      
        let label = UILabel()
        label.text("未知")
        label.font(13)
        label.textColor = .hexStringColor(hexString: "#A7A7A7")
        return label
    }()
    
    lazy var checkBtn: UIButton = {
       
        let btn = UIButton()
        btn.addRadius(radius: 16.rpx)
        btn.gradientBackgroundColor(color1: .hexStringColor(hexString: "#15CBA1"), color2: .hexStringColor(hexString: "#1FE0BA"), width: 92.rpx, height: 32.rpx, direction: .horizontal)
        btn.title("查看轨迹")
        btn.textColor(.white)
        btn.mediumFont(15)
        btn.addTarget(self, action: #selector(checkBtnAction), for: .touchUpInside)
        return btn
    }()
    
    lazy var blurView: UIView = {
       
        let effect = UIBlurEffect(style: .light)
        let blur = UIVisualEffectView(effect: effect)
        blur.isHidden = true
        return blur
    }()
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        initUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func config(model: QSLUserModel) {
        self.userModel = model
        
        if model.isMine {
            self.timeLabel.text = Date().formateCurrentDate
            self.moreBtn.isHidden = true
        } else {
            self.moreBtn.isHidden = false
            
            if model.location.timestamp > 0 {
                self.timeLabel.text = Date.timestampToFormatterTimeString(timestamp: "\(model.location.timestamp)")
            } else {
                self.timeLabel.text = "未知"
            }
        }
        
        if model.remark.count > 0 {
            self.nameLabel.text = model.remark
        } else {
            self.nameLabel.text = model.phone
        }
        
        if model.location.addr.count > 0 {
            self.addrLabel.text = model.location.addr
        } else {
            self.addrLabel.text = "未知"
        }
        
        if !QSLBaseManager.shared.isVip() && !model.isMine {
            self.blurView.isHidden = false
        } else {
            if model.blockedMe {
                self.blurView.isHidden = false
            } else {
                self.blurView.isHidden = true
            }
        }
    }
}

extension QSLFriendTableViewCell {
    
    @objc func moreBtnAction() {
        
        if let model = self.userModel {
            delegate?.moreBtnAction(model: model, btn: self.moreBtn)
        }
    }
    
    @objc func checkBtnAction() {
        
        if let model = self.userModel {
            delegate?.roadBtnClickAction(model: model)
        }
    }
}

extension QSLFriendTableViewCell {
    
    func initUI() {
        
        self.backgroundColor = .clear
        
        self.contentView.addSubview(bgView)
        bgView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        bgView.addSubview(avatarImageView)
        avatarImageView.snp.makeConstraints { make in
            make.size.equalTo(CGSize(width: 48.rpx, height: 48.rpx))
            make.left.equalTo(16.rpx)
            make.top.equalTo(22.rpx)
        }
        
        bgView.addSubview(nameLabel)
        nameLabel.snp.makeConstraints { make in
            make.left.equalTo(avatarImageView.snp.right).offset(8.rpx)
            make.top.equalTo(avatarImageView.snp.top).offset(5.rpx)
        }
        
        bgView.addSubview(moreBtn)
        moreBtn.snp.makeConstraints { make in
            make.size.equalTo(CGSize(width: 24.rpx, height: 24.rpx))
            make.right.equalTo(-16.rpx)
            make.top.equalTo(20.rpx)
        }
        
        bgView.addSubview(locateIcon)
        locateIcon.snp.makeConstraints { make in
            make.size.equalTo(CGSize(width: 16.rpx, height: 16.rpx))
            make.left.equalTo(avatarImageView.snp.right).offset(8.rpx)
            make.top.equalTo(nameLabel.snp.bottom).offset(8.rpx)
        }
        
        bgView.addSubview(addrLabel)
        addrLabel.snp.makeConstraints { make in
            make.left.equalTo(locateIcon.snp.right)
            make.right.equalTo(-16.rpx)
            make.top.equalTo(locateIcon.snp.top)
        }
        
        bgView.addSubview(blurView)
        blurView.snp.makeConstraints { make in
            make.edges.equalTo(addrLabel.snp.edges)
        }
        
        bgView.addSubview(lineView)
        lineView.snp.makeConstraints { make in
            make.left.equalTo(16.rpx)
            make.right.equalTo(-16.rpx)
            make.height.equalTo(1.rpx)
            make.bottom.equalTo(-47.rpx)
        }
        
        bgView.addSubview(timeLabel)
        timeLabel.snp.makeConstraints { make in
            make.left.equalTo(16.rpx)
            make.bottom.equalTo(-14.rpx)
        }
        
        bgView.addSubview(checkBtn)
        checkBtn.snp.makeConstraints { make in
            make.size.equalTo(CGSize(width: 92.rpx, height: 32.rpx))
            make.right.equalTo(-16.rpx)
            make.centerY.equalTo(timeLabel.snp.centerY)
        }
    }
}
