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

import UIKit

class QSLFriendAddAlertView: UIView {
    
    var viewController: UIViewController?
    
    var clickClosure: (() -> Void)?
    
    lazy var addBgView: UIView = {
        
        let view = UIView()
        view.backgroundColor = UIColor.hexStringColor(hexString: "#000000", alpha: 0.5)
        view.alpha = 0
        
        let tap = UITapGestureRecognizer(target: self, action: #selector(removeView))
        view.addGestureRecognizer(tap)
        return view
    }()
    
    lazy var addContentView: UIView = {
        
        let view = UIView(frame: CGRect(x: 0, y: 0, width: QSLConst.qsl_kScreenW, height: QSLConst.qsl_kScreenH - 100))
        view.backgroundColor = .white
        view.addCorner(conrners: [.topLeft, .topRight], radius: 20)
        return view
    }()
    
    lazy var addCloseButton: UIButton = {
    
        let button = UIButton(type: .custom)
        button.image(UIImage(named: "public_btn_close_AAA"))
        button.addTarget(self, action: #selector(removeView), for: .touchUpInside)
        return button
    }()
    
    lazy var addBgImageView: UIImageView = {
       
        let imageView = UIImageView()
        imageView.image = UIImage(named: "friends_add_bg")
        return imageView
    }()
    
    lazy var addMainView: UIView = {
       
        let scale = 1080.0 / 860.0
        let topBgHeight = qsl_kScreenW / scale
        let mainViewHeight = qsl_kScreenH - topBgHeight - 50.0
        
        let view = UIView(frame: CGRect(x: 0, y: topBgHeight - 50, width: qsl_kScreenW, height: mainViewHeight))
        view.backgroundColor = .white
        
        let aPath = UIBezierPath()
        aPath.lineWidth = 1.0
        aPath.lineCapStyle = .round
        aPath.lineJoinStyle = .round
        
        aPath.move(to: CGPoint(x: 0, y: 20))
        aPath.addQuadCurve(to: CGPoint(x: qsl_kScreenW, y: 20), controlPoint: CGPoint(x: qsl_kScreenW / 2, y: -20))
        aPath.addLine(to: CGPoint(x: qsl_kScreenW, y: mainViewHeight))
        aPath.addLine(to: CGPoint(x: 0, y: mainViewHeight))
        aPath.addLine(to: CGPoint(x: 0, y: 20))
        
        let maskLayer = CAShapeLayer()
        maskLayer.frame = view.bounds
        maskLayer.path = aPath.cgPath
        
        view.layer.mask = maskLayer
        
        return view
    }()
    
    lazy var addMainTitleLabel: UILabel = {
       
        let label = UILabel()
        label.mediumFont(20)
        label.textColor = QSLColor.textColor_333
        label.text = "添加好友"
        return label
    }()
    
    lazy var addMainSubTitleLabel: UILabel = {
       
        let label = UILabel()
        label.font(13)
        label.textColor = QSLColor.Color_888
        label.text = "查看实时定位，开启轨迹守护"
        return label
    }()
    
    lazy var addMainPhoneView: UIView = {
       
        let view = UIView()
        view.backgroundColor = QSLColor.backGroundColor
        view.addRadius(radius: 6)
        return view
    }()
    
    lazy var addMainPhoneTextField: UITextField = {
        
        let textF = UITextField()
        textF.font = UIFont.textF(16)
        textF.textColor = QSLColor.textColor_333
        textF.keyboardType = .phonePad
        textF.textAlignment = .left
        textF.maxTextNumber = 11
        
        textF.setPlaceholderAttribute(font: UIFont.textF(16), color: QSLColor.textColor_AAA)
        textF.placeholder = "输入手机号码 查看定位"
        
        return textF
    }()
    
    lazy var addMainAddrBookBtn: UIButton = {
       
        let button = UIButton(type: .custom)
        button.font(16)
        button.textColor(QSLColor.textColor_333)
        button.title("通讯录")
        return button
    }()
    
    lazy var lineIcon: UIImageView = {
       
        let _lineIcon = UIImageView()
        _lineIcon.image = UIImage.image(color: UIColor.hexStringColor(hexString: "#AAAAAA", alpha: 0.4), size: CGSize(width: 1, height: 24))
        return _lineIcon
    }()
    
    lazy var addMainSearchBtn: UIButton = {
       
        let button = UIButton.normal()
        button.title("立即添加")
        button.addRadius(radius: 22)
        button.isEnabled = false
        return button
    }()
    
    lazy var addMainTipsLabel: UILabel = {
       
        let label = UILabel()
        
        label.numberOfLines = 0
        label.textAlignment = .center
        
        label.font(12)
        label.textColor = QSLColor.textColor_D1
        label.text = "温馨提示：文本在线配置内容，直接使用添加好友文本配置接口。"
        return label
    }()
    
    class func show(vc: UIViewController, clickClosure: @escaping () -> Void) {
        
        let window = QSLFriendAddAlertView(frame: CGRect(x: 0, y: 0, width: QSLConst.qsl_kScreenW, height: QSLConst.qsl_kScreenH))
        window.viewController = vc
        window.clickClosure = clickClosure
        vc.view.addSubview(window)
        UIView.animate(withDuration: 0.4, delay: 0, usingSpringWithDamping: 0.95, initialSpringVelocity: 0.05) {
            window.addBgView.alpha = 1
            window.addContentView.qsl_y = 100
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.setUpUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // 关闭本view
    @objc func removeView() {
        
        UIView.animate(withDuration: 0.4, delay: 0, usingSpringWithDamping: 0.95, initialSpringVelocity: 0.05) { [weak self] in
            self?.addBgView.alpha = 0
            self?.addContentView.qsl_y = QSLConst.qsl_kScreenH
        } completion: { [weak self] finished in
            self?.removeFromSuperview()
        }
    }
    
}

extension QSLFriendAddAlertView {
    
    func setUpUI() {
        
        self.backgroundColor = .clear
        
        addSubview(addBgView)
        addSubview(addContentView)
        
        addContentView.addSubview(addBgImageView)
        addContentView.addSubview(addCloseButton)
        addContentView.addSubview(addMainView)
        
        addMainView.addSubview(addMainTitleLabel)
        addMainView.addSubview(addMainSubTitleLabel)
        addMainView.addSubview(addMainPhoneView)
        
        addMainPhoneView.addSubview(addMainAddrBookBtn)
        addMainPhoneView.addSubview(lineIcon)
        addMainPhoneView.addSubview(addMainPhoneTextField)
        
        addMainView.addSubview(addMainSearchBtn)
        addMainView.addSubview(addMainTipsLabel)
        
        addBgView.snp.makeConstraints { make in
            make.edges.equalTo(0)
        }
        
        addContentView.snp.makeConstraints { make in
            make.top.equalTo(100)
            make.left.right.bottom.equalTo(0)
        }
        addContentView.qsl_y = qsl_kScreenH
        
        let scale = 1080.0 / 860.0
        let topBgHeight = qsl_kScreenW / scale
        addBgImageView.snp.makeConstraints { make in
            make.left.top.right.equalTo(0)
            make.height.equalTo(topBgHeight)
        }
        
        addCloseButton.snp.makeConstraints { make in
            make.size.equalTo(CGSize(width: 24, height: 24))
            make.top.equalTo(16)
            make.right.equalTo(-20)
        }
        
        addMainTitleLabel.snp.makeConstraints { make in
            make.top.equalTo(56)
            make.centerX.equalTo(addMainView.snp.centerX)
        }
        
        addMainSubTitleLabel.snp.makeConstraints { make in
            make.top.equalTo(addMainTitleLabel.snp.bottom).offset(2)
            make.centerX.equalTo(addMainView.snp.centerX)
        }
        
        addMainPhoneView.snp.makeConstraints { make in
            make.left.equalTo(24)
            make.right.equalTo(-24)
            make.height.equalTo(56)
            make.top.equalTo(addMainSubTitleLabel.snp.bottom).offset(20)
        }
        
        addMainAddrBookBtn.snp.makeConstraints { make in
            make.top.bottom.right.equalTo(0)
            make.width.equalTo(72)
        }
        
        lineIcon.snp.makeConstraints { make in
            make.right.equalTo(addMainAddrBookBtn.snp.left)
            make.centerY.equalTo(addMainPhoneView.snp.centerY)
        }
        
        addMainPhoneTextField.snp.makeConstraints { make in
            make.left.equalTo(16)
            make.right.equalTo(lineIcon.qsl_left).offset(-16)
            make.top.bottom.equalTo(0)
        }
        
        addMainSearchBtn.snp.makeConstraints { make in
            make.size.equalTo(CGSize(width: 200, height: 44))
            make.top.equalTo(addMainPhoneView.snp.bottom).offset(36)
            make.centerX.equalTo(snp.centerX)
        }
        
        addMainTipsLabel.snp.makeConstraints { make in
            make.left.equalTo(60)
            make.right.equalTo(-60)
            make.bottom.equalTo(-QSLConst.qsl_kTabbarBottom - 20)
        }
    }
}
