//
//  QSLHomeFriendTableViewCell.swift
//  QuickSearchLocation
//
//  Created by mac on 2024/4/12.
//

import UIKit
import MarqueeLabel

protocol QSLHomeFriendTableViewCellDelegate: NSObjectProtocol {
    
    func routeBtnAction(model: QSLUserModel)
    
    func locateBtnAction(model: QSLUserModel)
}

class QSLHomeFriendTableViewCell: UITableViewCell {
    
    var model: QSLUserModel?
    
    weak var delegate: QSLHomeFriendTableViewCellDelegate?
    
    lazy var bgView: UIView = {
       
        let view = UIView()
        view.backgroundColor = .white
        view.addRadius(radius: 8.rpx)
        return view
    }()
    
    lazy var avatarImageView: UIImageView = {
        
        let imageView = UIImageView()
        imageView.image = UIImage(named: "friends_cell_avatar")
        return imageView
    }()
    
    lazy var tagImageView: UIImageView = {
       
        let imageView = UIImageView()
        imageView.isHidden = true
        imageView.image = UIImage(named: "friends_cell_tag")
        return imageView
    }()
    
    lazy var nameLabel: UILabel = {
       
        let label = UILabel()
        label.text = "用户123"
        label.font = UIFont.textM(16)
        label.textColor = QSLColor.Color_202020
        return label
    }()
    
    lazy var timeLabel: UILabel = {
       
        let label = UILabel()
        label.text = "1分钟前"
        label.font(12)
        label.textColor = .hexStringColor(hexString: "#A7A7A7")
        return label
    }()
    
    lazy var locateButton: UIButton = {
       
        let button = UIButton()
        button.setBackgroundImage(UIImage(named: "home_friends_locate_btn"), for: .normal)
        button.addTarget(self, action: #selector(locateBtnAction), for: .touchUpInside)
        return button
    }()
    
    lazy var locateIcon: UIImageView = {
       
        let imageView = UIImageView()
        imageView.image = UIImage(named: "friends_cell_location")
        return imageView
    }()
    
    lazy var addrLabel: MarqueeLabel = {
       
        let label = MarqueeLabel(frame: .zero, duration: 8.0, fadeLength: 10)
        label.font(13)
        label.textColor = .hexStringColor(hexString: "#A7A7A7")
        label.text = "广东奥林匹克广东奥林匹克广东奥林匹克  "
        return label
    }()
    
    lazy var checkButton: UIButton = {
       
        let button = UIButton(frame: CGRect(x: 0, y: 0, width: 72.rpx, height: 28.rpx))
        button.mediumFont(15)
        button.textColor(QSLColor.themeMainColor)
        button.title("轨迹")
        
        button.addCorner(conrners: .allCorners, radius: 22.rpx, borderWidth: 1.rpx, borderColor: QSLColor.themeMainColor)
        
        button.addTarget(self, action: #selector(checkButtonAction), for: .touchUpInside)
        return button
    }()
    
    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)
        
        self.setCellUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    @objc func locateBtnAction() {
        
        if let model = self.model {
            delegate?.locateBtnAction(model: model)
        }
    }
    
    @objc func checkButtonAction() {
        
        if let model = self.model {
            delegate?.routeBtnAction(model: model)
        }
    }
    
    func config(model: QSLUserModel) {
        
        self.model = model
        
        if model.isMine {
            
            self.timeLabel.text = "10秒前"
        } else {
            
            if model.location.timestamp > 0 {
                let date = Date.timestampToFormatterDate(timestamp: "\(model.location.timestamp)")
                self.timeLabel.text = date.callTimeAfterNow()
            } 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 QSLHomeFriendTableViewCell {
    
    func setCellUI() {
        
        self.backgroundColor = .clear
        self.contentView.backgroundColor = .clear
        contentView.addSubview(bgView)
        bgView.snp.makeConstraints { make in
            make.left.equalTo(12.rpx)
            make.right.equalTo(-12.rpx)
            make.top.equalTo(0)
            make.bottom.equalTo(-8.rpx)
        }
        
        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(16.rpx)
        }
        
//        bgView.addSubview(tagImageView)
//        tagImageView.snp.makeConstraints { make in
//            make.centerX.equalTo(avatarImageView.snp.centerX)
//            make.centerY.equalTo(avatarImageView.snp.bottom)
//        }
        
        bgView.addSubview(nameLabel)
        nameLabel.snp.makeConstraints { make in
            make.top.equalTo(avatarImageView.snp.top).offset(4.rpx)
            make.left.equalTo(avatarImageView.snp.right).offset(8.rpx)
        }
        
        bgView.addSubview(timeLabel)
        timeLabel.snp.makeConstraints { make in
            make.centerY.equalTo(nameLabel.snp.centerY)
            make.left.equalTo(nameLabel.snp.right).offset(8.rpx)
        }
        
        bgView.addSubview(locateButton)
        locateButton.snp.makeConstraints { make in
            make.size.equalTo(CGSize(width: 24.rpx, height: 24.rpx))
            make.left.equalTo(timeLabel.snp.right).offset(6.rpx)
            make.centerY.equalTo(timeLabel.snp.centerY)
        }
        
        bgView.addSubview(checkButton)
        checkButton.snp.makeConstraints { make in
            make.size.equalTo(CGSize(width: 72.rpx, height: 28.rpx))
            make.right.equalTo(-12.rpx)
            make.centerY.equalTo(nameLabel.snp.centerY)
        }
        
        bgView.addSubview(locateIcon)
        locateIcon.snp.makeConstraints { make in
            make.size.equalTo(CGSize(width: 16.rpx, height: 16.rpx))
            make.bottom.equalTo(avatarImageView.snp.bottom).offset(5.rpx)
            make.left.equalTo(avatarImageView.snp.right).offset(8.rpx)
        }
        
        bgView.addSubview(addrLabel)
        addrLabel.snp.makeConstraints { make in
            make.centerY.equalTo(locateIcon.snp.centerY)
            make.left.equalTo(locateIcon.snp.right)
            make.height.equalTo(20.rpx)
            make.right.equalTo(-12.rpx)
        }
        
        bgView.addSubview(blurView)
        blurView.snp.makeConstraints { make in
            make.edges.equalTo(addrLabel.snp.edges)
        }
    }
}
