//
//  YHInformationFillTipsAlertView.swift
//  galaxy
//
//  Created by davidhuangA on 2024/1/26.
//  Copyright © 2024 https://www.galaxy-immi.com. All rights reserved.
//

import UIKit
//import AttributedString

class YHInformationFillTipsAlertView: UIView {
    typealias Block = (Bool) -> ()
    var block: Block?
    var centerView: UIView!
    var titleLabel: UILabel!
    var messageLabel: UILabel!
    var agreeButton: UIButton!
    var disAgressButton: UIButton!
    
    //头部背景图片
    lazy var headBkgImagV: UIImageView = {
        let imagV = UIImageView()
        imagV.image = UIImage(named: "submit_success_bkg")
        imagV.contentMode = .scaleAspectFill
        return imagV
    }()
    
    //头部背景图片
    lazy var successIcon: UIImageView = {
        let imagV = UIImageView()
        imagV.image = UIImage(named: "submit_success_icon")
        imagV.contentMode = .scaleAspectFill
        return imagV
    }()
    
    
    var homeTableView: UITableView = {
        let tableView = UITableView(frame: .zero, style: .plain)
        tableView.backgroundColor = .clear
        tableView.separatorStyle = .none
        tableView.showsHorizontalScrollIndicator = true
        tableView.showsVerticalScrollIndicator = false
        tableView.rowHeight = UITableView.automaticDimension
        //        tableView.estimatedRowHeight = 100
        tableView.register(YHInformationFillTipsCell.self,forCellReuseIdentifier: YHInformationFillTipsCell.cellReuseIdentifier)
        return tableView
    }()
    
    
    
    //类方法
    static func showCheckView(callBack: @escaping Block) {
        let view = YHInformationFillTipsAlertView(frame: CGRect(x: 0, y: 0, width: KScreenWidth, height: KScreenHeight))
        view.block = callBack
        let window = UIApplication.shared.yhKeyWindow()
        window?.addSubview(view)
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = UIColor(white: 0.5, alpha: 0.1)
        //        let tap = UITapGestureRecognizer(target: self, action: #selector(handleTap))
        //        tap.delegate = self
        //        addGestureRecognizer(tap)
        
        setView()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setView() {
        backgroundColor = UIColor(hex: 0x000000, alpha: 0.5)
        centerView = {
            let view = UIView()
            view.backgroundColor = .white
            view.layer.cornerRadius = 8
            return view
        }()
        addSubview(centerView)
        
        let viewH = KScreenHeight > 680 ? 680 : (KScreenHeight - 64)
        centerView.snp.makeConstraints { make in
            make.bottom.equalToSuperview()
            make.width.equalTo(KScreenWidth)
            make.height.equalTo(viewH)
        }
        
        
        let bottomView = UIView()
        centerView.addSubview(bottomView)
        bottomView.snp.makeConstraints { make in
            make.left.right.bottom.equalToSuperview()
        }
        
        let checkBtn = UIButton(type: .custom)
        checkBtn.addTarget(self, action: #selector(onClickReadyBtn), for: .touchUpInside)
        checkBtn.setTitle("准备完毕", for: .normal)
        checkBtn.setTitleColor(.white, for: .normal)
        checkBtn.layer.cornerRadius = 8
        checkBtn.clipsToBounds = true
        
        bottomView.addSubview(checkBtn)
        checkBtn.snp.makeConstraints { make in
            make.bottom.equalToSuperview().offset(-40)
            make.left.equalTo(16)
            make.right.equalTo(-16)
            make.height.equalTo(48)
        }
        checkBtn.layoutIfNeeded()//非常重要！！！！ 
        checkBtn.backgroundGradient()
        
        
        
        let lable00 = UILabel(text: "我确认我所提供的材料真实有效，并知悉其中内容。")
        lable00.font = kFont(size: 12)
        lable00.textColor = UIColor(red: 0.533, green: 0.561, blue: 0.596, alpha: 1)
        let tap = UITapGestureRecognizer(target: self, action: #selector(onClickAgree))
        lable00.isUserInteractionEnabled = true
        lable00.addGestureRecognizer(tap)
        
        bottomView.addSubview(lable00)
        lable00.snp.makeConstraints { make in
            make.bottom.equalTo(checkBtn.snp.top).offset(-14)
            make.left.equalTo(41)
            make.right.equalTo(-16)
            make.height.equalTo(20)
            make.top.equalTo(16)
        }
        
        let selectedBtn = UIButton(type: .custom)
        selectedBtn.setImage(UIImage(named: "selected_icon_0"), for: .normal)
        selectedBtn.setImage(UIImage(named: "selected_icon_1"), for: .selected)
        selectedBtn.addTarget(self, action: #selector(onClickAgree), for: .touchUpInside)
        bottomView.addSubview(selectedBtn)
        selectedBtn.snp.makeConstraints { make in
            make.centerY.equalTo(lable00)
            make.left.equalTo(21)
            make.height.width.equalTo(12)
        }
        agreeButton = selectedBtn
        
        
        let topView = UIView()
        centerView.addSubview(topView)
        topView.snp.makeConstraints { make in
            make.left.right.top.equalToSuperview()
        }
        
        
        let lable01 = UILabel(text: "信息填写提示")
        lable01.font = UIFont.PFSC_B(ofSize: 18)
        lable01.textColor = UIColor(red: 0.133, green: 0.133, blue: 0.133, alpha: 1)
        
        topView.addSubview(lable01)
        lable01.snp.makeConstraints { make in
            make.left.equalTo(21)
            make.right.equalTo(-21)
            make.top.equalTo(16)
            make.height.equalTo(24)
        }
        
        
        let line = UIView()
        line.backgroundColor = UIColor(red: 0.941, green: 0.941, blue: 0.941, alpha: 1)
        topView.addSubview(line)
        line.snp.makeConstraints { make in
            make.left.equalTo(21)
            make.right.equalTo(-21)
            make.top.equalTo(lable01.snp.bottom).offset(12)
            make.height.equalTo(0.5)
            make.bottom.equalToSuperview()
        }
        
        
        //middle view
        let middleView = UIView()
        centerView.addSubview(middleView)
        middleView.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.top.equalTo(topView.snp.bottom).offset(-20)
            make.bottom.equalTo(bottomView.snp.top)
        }
        
        middleView.addSubview(homeTableView)
        homeTableView.snp.makeConstraints { make in
            make.edges.equalTo(0)
        }
        homeTableView.delegate = self
        homeTableView.dataSource = self
    }
    
    @objc func onClickReadyBtn() {
        
        if agreeButton.isSelected == true {
            block?(true)
            dismiss()
        } else {
            YHHUD.flash(message: "请先勾选确认知悉内容")
        }
        
    }
    
    @objc func onClickAgree() {
        agreeButton.isSelected = !agreeButton.isSelected
    }
    
    
    
    @objc func agree() {
        block?(true)
        dismiss()
    }
    
    @objc private func handleTap(_ sender: AnyObject?) {
        print("处理点击手势")
        dismiss()
    }
    
    @objc func disagree() {
        block?(false)
        dismiss()
    }
    
    func dismiss() {
        removeFromSuperview()
    }
}

extension YHInformationFillTipsAlertView: UIGestureRecognizerDelegate {
    func gestureRecognizer(_ gestureRecognizer:UIGestureRecognizer, shouldReceive touch:UITouch) -> Bool {
        if touch.view == self {
            return true
        }
        return false
    }
}



// MARK: - UITableViewDelegate 和 UITableViewDataSource
extension YHInformationFillTipsAlertView : UITableViewDelegate,UITableViewDataSource {
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 0
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        return UIView()
    }
    
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 0
    }
    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        return UIView()
    }
    
    
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        
        return UITableView.automaticDimension
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withClass: YHInformationFillTipsCell.self)
        cell.selectionStyle = .none
        return cell
    }
}
