//
//  QSLRoadMainView.swift
//  QuickSearchLocation
//
//  Created by Destiny on 2024/12/3.
//

import UIKit

protocol QSLRoadMainViewDelegate: NSObjectProtocol {
    
    func startTimeClickAction()
    
    func endTimeClickAction()
    
    func searchClickAction()
}

class QSLRoadMainView: UIView {
    
    weak var delegate: QSLRoadMainViewDelegate?
    
    lazy var contentView: UIView = {
        
        let view = UIView(frame: CGRect(x: 0, y: 0, width: QSLConst.qsl_kScreenW, height: 369.rpx))
        view.backgroundColor = QSLColor.backGroundColor
        view.addFourCorner(topLeft: 12.rpx, topRight: 12.rpx, bottomLeft: 0, bottomRight: 0)
        return view
    }()
    
    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("儿子的轨迹")
        label.mediumFont(16)
        label.textColor = QSLColor.Color_202020
        return label
    }()
    
    lazy var timeView: UIView = {
      
        let view = UIView()
        view.backgroundColor = .white
        view.addRadius(radius: 8.rpx)
        return view
    }()
    
    lazy var startTimeView: UIView = {
       
        let view = UIView()
        view.backgroundColor = QSLColor.backGroundColor
        view.addRadius(radius: 4.rpx)
        view.addBorder(borderWidth: 1.rpx, borderColor: .hexStringColor(hexString: "#F2F2F2"))
        
        view.isUserInteractionEnabled = true
        let tap = UITapGestureRecognizer(target: self, action: #selector(startTimeAction))
        view.addGestureRecognizer(tap)
        return view
    }()
    
    lazy var startTimeLabel: UILabel = {
       
        let label = UILabel()
        label.text("开始时间：2023-07-07 14:33")
        label.font(14)
        label.textColor = QSLColor.Color_202020
        return label
    }()
    
    lazy var startTimeArrow: UIImageView = {
       
        let imageView = UIImageView()
        imageView.image = UIImage(named: "route_time_arrow")
        return imageView
    }()
    
    lazy var endTimeView: UIView = {
       
        let view = UIView()
        view.backgroundColor = QSLColor.backGroundColor
        view.addRadius(radius: 4.rpx)
        view.addBorder(borderWidth: 1.rpx, borderColor: .hexStringColor(hexString: "#F2F2F2"))
        
        view.isUserInteractionEnabled = true
        let tap = UITapGestureRecognizer(target: self, action: #selector(endTimeAction))
        view.addGestureRecognizer(tap)
        return view
    }()
    
    lazy var endTimeLabel: UILabel = {
       
        let label = UILabel()
        label.text("结束时间：2023-07-08 14:33")
        label.font(14)
        label.textColor = QSLColor.Color_202020
        return label
    }()
    
    lazy var endTimeArrow: UIImageView = {
       
        let imageView = UIImageView()
        imageView.image = UIImage(named: "route_time_arrow")
        return imageView
    }()
    
    lazy var startAddressPoint: UIView = {
        
        let view = UIView()
        view.addRadius(radius: 3.5.rpx)
        view.backgroundColor = .hexStringColor(hexString: "#12C172")
        return view
    }()
    
    lazy var startAddressLabel: UILabel = {
        
        let label = UILabel()
        label.numberOfLines = 0
        label.text("起点：")
        label.font(13)
        label.textColor = .hexStringColor(hexString: "#A7A7A7")
        return label
    }()
    
    lazy var lineView: UIView = {
       
        let view = UIView()
        view.backgroundColor = .hexStringColor(hexString: "#F5F5F5")
        return view
    }()
    
    lazy var endAddressPoint: UIView = {
        
        let view = UIView()
        view.addRadius(radius: 3.5.rpx)
        view.backgroundColor = .hexStringColor(hexString: "#F3353A")
        return view
    }()
    
    lazy var endAddressLabel: UILabel = {
        
        let label = UILabel()
        label.numberOfLines = 0
        label.text("终点：")
        label.font(13)
        label.textColor = .hexStringColor(hexString: "#A7A7A7")
        return label
    }()
    
    lazy var searchBtn: UIButton = {
       
        let btn = UIButton()
        btn.gradientBackgroundColor(color1: .hexStringColor(hexString: "#15CBA1"), color2: .hexStringColor(hexString: "#1FE0BA"), width: 280.rpx, height: 44.rpx, direction: .horizontal)
        btn.addRadius(radius: 22.rpx)
        btn.title("查询轨迹")
        btn.textColor(.white)
        btn.mediumFont(16.rpx)
        btn.addTarget(self, action: #selector(searchAction), for: .touchUpInside)
        return btn
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        initView()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension QSLRoadMainView {
    
    @objc func searchAction() {
        delegate?.searchClickAction()
    }
    
    @objc func startTimeAction() {
        delegate?.startTimeClickAction()
    }
    
    @objc func endTimeAction() {
        delegate?.endTimeClickAction()
    }
}

extension QSLRoadMainView {
    
    func initView() {
        
        self.backgroundColor = .clear
        addSubview(contentView)
        contentView.snp.makeConstraints { make in
            make.left.right.bottom.equalTo(0)
            make.height.equalTo(369.rpx)
        }
        
        contentView.addSubview(avatarImageView)
        avatarImageView.snp.makeConstraints { make in
            make.size.equalTo(CGSize(width: 32.rpx, height: 32.rpx))
            make.left.equalTo(12.rpx)
            make.top.equalTo(16.rpx)
        }
        
        contentView.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { make in
            make.left.equalTo(avatarImageView.snp.right).offset(8.rpx)
            make.centerY.equalTo(avatarImageView.snp.centerY)
        }
        
        contentView.addSubview(timeView)
        timeView.snp.makeConstraints { make in
            make.left.equalTo(8.rpx)
            make.right.equalTo(-8.rpx)
            make.top.equalTo(56.rpx)
            make.bottom.equalTo(-QSLConst.qsl_kTabbarBottom)
        }
        
        timeView.addSubview(startTimeView)
        startTimeView.snp.makeConstraints { make in
            make.left.equalTo(20.rpx)
            make.right.equalTo(-17.rpx)
            make.top.equalTo(20.rpx)
            make.height.equalTo(36.rpx)
        }
        
        startTimeView.addSubview(startTimeLabel)
        startTimeLabel.snp.makeConstraints { make in
            make.left.equalTo(12.rpx)
            make.centerY.equalToSuperview()
        }
        
        startTimeView.addSubview(startTimeArrow)
        startTimeArrow.snp.makeConstraints { make in
            make.size.equalTo(CGSize(width: 20.rpx, height: 20.rpx))
            make.right.equalTo(-12.rpx)
            make.centerY.equalToSuperview()
        }
        
        timeView.addSubview(endTimeView)
        endTimeView.snp.makeConstraints { make in
            make.left.equalTo(20.rpx)
            make.right.equalTo(-17.rpx)
            make.top.equalTo(startTimeView.snp.bottom).offset(12.rpx)
            make.height.equalTo(36.rpx)
        }
        
        endTimeView.addSubview(endTimeLabel)
        endTimeLabel.snp.makeConstraints { make in
            make.left.equalTo(12.rpx)
            make.centerY.equalToSuperview()
        }
        
        endTimeView.addSubview(endTimeArrow)
        endTimeArrow.snp.makeConstraints { make in
            make.size.equalTo(CGSize(width: 20.rpx, height: 20.rpx))
            make.right.equalTo(-12.rpx)
            make.centerY.equalToSuperview()
        }
        
        timeView.addSubview(startAddressPoint)
        startAddressPoint.snp.makeConstraints { make in
            make.size.equalTo(CGSize(width: 7.rpx, height: 7.rpx))
            make.left.equalTo(20.rpx)
            make.top.equalTo(endTimeView.snp.bottom).offset(32.rpx)
        }
        
        timeView.addSubview(startAddressLabel)
        startAddressLabel.snp.makeConstraints { make in
            make.left.equalTo(startAddressPoint.snp.right).offset(10.rpx)
            make.right.equalTo(-17.rpx)
            make.centerY.equalTo(startAddressPoint.snp.centerY)
        }
        
        timeView.addSubview(lineView)
        lineView.snp.makeConstraints { make in
            make.width.equalTo(1.rpx)
            make.height.equalTo(27.rpx)
            make.top.equalTo(startAddressPoint.snp.bottom).offset(8.rpx)
            make.centerX.equalTo(startAddressPoint.snp.centerX)
        }
        
        timeView.addSubview(endAddressPoint)
        endAddressPoint.snp.makeConstraints { make in
            make.size.equalTo(CGSize(width: 7.rpx, height: 7.rpx))
            make.left.equalTo(20.rpx)
            make.top.equalTo(lineView.snp.bottom).offset(8.rpx)
        }
        
        timeView.addSubview(endAddressLabel)
        endAddressLabel.snp.makeConstraints { make in
            make.left.equalTo(endAddressPoint.snp.right).offset(10.rpx)
            make.right.equalTo(-17.rpx)
            make.centerY.equalTo(endAddressPoint.snp.centerY)
        }
        
        timeView.addSubview(searchBtn)
        searchBtn.snp.makeConstraints { make in
            make.size.equalTo(CGSize(width: 280.rpx, height: 44.rpx))
            make.centerX.equalToSuperview()
            make.bottom.equalTo(-24.rpx)
        }
    }
}
