//
//  QSLContactSendFailAlertView.swift
//  QuickSearchLocation
//
//  Created by Destiny on 2024/12/12.
//

import UIKit

class QSLContactSendFailAlertView: UIView {
    
    var contactList: [String]?
    
    lazy var contentView: UIView = {
        
        let contentView = UIView(frame: CGRect(x: 0, y: 0, width: QSLConst.qsl_kScreenW - 60.rpx, height: 152.0.rpx))
        contentView.backgroundColor = .white
        contentView.addRadius(radius: 8.rpx)
        return contentView
    }()
    
    lazy var titleLabel: UILabel = {
      
        let label = UILabel()
        label.text("添加紧急联系人")
        label.mediumFont(17)
        label.textColor = QSLColor.Color_202020
        return label
    }()
    
    lazy var contentLabel: UILabel = {
        
        let label = UILabel()
        label.text("部分号码发送失败，请检查号码重试!")
        label.font(15)
        label.textColor = .hexStringColor(hexString: "#404040")
        return label
    }()
    
    lazy var infoView: UIView = {
       
        let view = UIView()
        view.addRadius(radius: 4.rpx)
        view.backgroundColor = QSLColor.backGroundColor
        view.addBorder(borderWidth: 1.rpx, borderColor: .hexStringColor(hexString: "#F2F2F2"))
        return view
    }()
    
    lazy var infoTableView: UITableView = {
        
        let tableView = UITableView(frame: .zero, style: .grouped)
        tableView.backgroundColor = QSLColor.backGroundColor
        tableView.separatorStyle = .none
        tableView.showsVerticalScrollIndicator = false
        tableView.delegate = self
        tableView.dataSource = self
        tableView.tableViewNeverAdjustContentInset()
        tableView.bounces = true
        tableView.isUserInteractionEnabled = true
        tableView.isScrollEnabled = true
        tableView.contentInsetAdjustmentBehavior = .never
        tableView.register(cellClass: QSLContactFailCell.self)
        
        return tableView
    }()
    
    lazy var oneButton: UIButton = {
      
        let btn = UIButton()
        btn.addRadius(radius: 20.rpx)
        btn.title("我知道了")
        btn.textColor(.white)
        btn.mediumFont(16)
        btn.gradientBackgroundColor(color1: .hexStringColor(hexString: "#15CBA1"), color2: .hexStringColor(hexString: "#1FE0BA"), width: 150.rpx, height: 40.rpx, direction: .horizontal)
        btn.addTarget(self, action: #selector(oneBtnAction), for: .touchUpInside)
        return btn
    }()
    
    lazy var closeButton: UIButton = {
       
        let btn = UIButton()
        btn.setBackgroundImage(UIImage(named: "public_btn_close_AAA"), for: .normal)
        btn.addTarget(self, action: #selector(removeView), for: .touchUpInside)
        return btn
    }()
    
    class func alert(view: UIView, contactList: [String]) {
        
        let window = QSLContactSendFailAlertView(frame: CGRect(x: 0, y: 0, width: QSLConst.qsl_kScreenW, height: QSLConst.qsl_kScreenH))
        window.contactList = contactList
        window.infoTableView.reloadData()
        let height = 20.rpx * contactList.count + 32.rpx + 10.rpx * (contactList.count - 1)
        window.infoView.snp.updateConstraints { make in
            make.height.equalTo(height)
        }
        window.contentView.snp.updateConstraints { make in
            make.height.equalTo(187.rpx + height)
        }
        view.addSubview(window)
        
        UIView.animate(withDuration: 0.4, delay: 0, usingSpringWithDamping: 0.95, initialSpringVelocity: 0.05) {
            window.backgroundColor = .hexStringColor(hexString: "#000000", alpha: 0.7)
            window.contentView.isHidden = false
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        initUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // 单按钮点击事件
    @objc func oneBtnAction() {
        removeView()
    }
    
    // 移除
    @objc func removeView() {
        
        UIView.animate(withDuration: 0.4, delay: 0, usingSpringWithDamping: 0.95, initialSpringVelocity: 0.05) { [weak self] in
            self?.backgroundColor = UIColor.init(white: 0, alpha: 0)
            self?.contentView.isHidden = true
        } completion: { [weak self] finished in
            self?.removeFromSuperview()
        }
    }
}

extension QSLContactSendFailAlertView: UITableViewDelegate, UITableViewDataSource {
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return self.contactList?.count ?? 0
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCell(cellType: QSLContactFailCell.self, cellForRowAt: indexPath)
        cell.selectionStyle = .none
        if let phone = self.contactList?[indexPath.section] {
            cell.config(phone: phone)
        }
        return cell
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 20.rpx
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 0.0001
    }
    
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 10.rpx
    }
}

extension QSLContactSendFailAlertView {
    
    func initUI() {
        
        addSubview(contentView)
        contentView.snp.makeConstraints { make in
            make.width.equalTo(QSLConst.qsl_kScreenW - 60.rpx)
            make.height.equalTo(203.0.rpx)
            make.center.equalToSuperview()
        }
        
        contentView.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(24.rpx)
        }
        
        contentView.addSubview(contentLabel)
        contentLabel.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(titleLabel.snp.bottom).offset(16.rpx)
        }
        
        contentView.addSubview(infoView)
        infoView.snp.makeConstraints { make in
            make.left.equalTo(24.rpx)
            make.right.equalTo(-24.rpx)
            make.top.equalTo(contentLabel.snp.bottom).offset(8.rpx)
            make.height.equalTo(82.rpx)
        }
        
        infoView.addSubview(infoTableView)
        infoTableView.snp.makeConstraints { make in
            make.left.equalTo(0)
            make.right.equalTo(0)
            make.top.equalTo(16.rpx)
            make.bottom.equalTo(-16.rpx)
        }
        
        contentView.addSubview(oneButton)
        oneButton.snp.makeConstraints { make in
            make.size.equalTo(CGSize(width: 150.rpx, height: 40.rpx))
            make.centerX.equalToSuperview()
            make.bottom.equalTo(-24.rpx)
        }
        
        contentView.addSubview(closeButton)
        closeButton.snp.makeConstraints { make in
            make.size.equalTo(CGSize(width: 24.rpx, height: 24.rpx))
            make.top.equalTo(12.rpx)
            make.right.equalTo(-12.rpx)
        }
    }
}
