//
//  CCBottomMenuPopView.swift
//  SwiftProject
//
//  Created by Zhang, Joyce on 2024/2/23.
//

import UIKit

class CCBottomMenuPopView: UIView,UITableViewDelegate, UITableViewDataSource {
    
    //    @objc static let `default`: CCBottomMenuPopView = CCBottomMenuPopView()
        
        //oc外部调用 --- 将swift对象赋值给oc的，这样才能保证成员变量相同
        @objc static let share: CCBottomMenuPopView = CCBottomMenuPopView()
        
        var textArray:[String] = []
        var imageArray:[String] = []
        
        typealias SelectRowIndex = (_ index:Int)->Void
        var selectRowIndex: SelectRowIndex?
        
        
        //MARK: - lazy
        let OtherCellId = "CCBottomMenuCell"
        
        
        lazy var backButton:UIButton = {
            
            let button = UIButton(type: .custom)
            //        button.setImage(UIImage.init(named: "mine_edit_button_normal"), for: .normal)
            //        button.setImage(UIImage.init(named: "club_dynamic_list_cell_comment_button_selected"), for: .selected)
            button.backgroundColor = .clear
            button.addTarget(self, action: #selector(hide), for: .touchUpInside)
            
            return button
            
        }()
        
        
        lazy var listTableView:UITableView = {
            
            let tableView = UITableView(frame: CGRect.init(x: 0, y: KScreenHeight - Back_Height , width: KScreenWidth, height: Back_Height), style: .grouped)
            tableView.showsVerticalScrollIndicator = false
            tableView.backgroundColor = UIColor(hexString: "#161622")
            tableView.delegate = self
            tableView.dataSource = self
            tableView.register(CCBottomMenuCell.self, forCellReuseIdentifier: OtherCellId)
            tableView.isScrollEnabled = false
            //去掉下划线
            tableView.separatorStyle = .none
           
            
            return tableView
            
        }()
        
       private var Back_Height:CGFloat = 0
        
    private var Cell_Height:CGFloat = 64

    private var HeaderView_Height:CGFloat = 8
        
        //MAR: - init
        override init(frame: CGRect) {
            super.init(frame: frame)
            
            self.backgroundColor = UIColor.black.withAlphaComponent(0.3)
            
            //高度
            Back_Height = Cell_Height*CGFloat(textArray.count)+HeaderView_Height+Cell_Height + KTabbarSafeBottomMargin
            
            self.addSubview(backButton)
            backButton.addSubview(listTableView)
            
        }
        
        //提供oc调用
        @objc func show(imageNameArray:NSArray, textArray:NSArray,_ selectRowIndex: @escaping (_ index:Int) -> Void) -> Void {
            
            //初始化
            CCBottomMenuPopView.share.imageArray = imageNameArray as! [String]
            CCBottomMenuPopView.share.textArray = textArray as! [String]
            
            CCBottomMenuPopView.share.frame = CGRect.init(x: 0 , y: 0, width: KScreenWidth , height: KScreenHeight);
            CCBottomMenuPopView.share.backButton.frame = CCBottomMenuPopView.share.frame
            
            KWindow?.addSubview(CCBottomMenuPopView.share)
            KWindow?.bringSubviewToFront(CCBottomMenuPopView.share)
            
            //tableView高度
            CCBottomMenuPopView.share.Back_Height = CCBottomMenuPopView.share.Cell_Height*CGFloat(textArray.count)+CCBottomMenuPopView.share.HeaderView_Height+CCBottomMenuPopView.share.Cell_Height
            
            UIView.animate(withDuration: 0.25, animations: {

                CCBottomMenuPopView.share.listTableView.alpha = 1.0
                CCBottomMenuPopView.share.listTableView.frame = CGRect.init(x: 0, y: KScreenHeight - CCBottomMenuPopView.share.Back_Height, width: KScreenWidth, height: CCBottomMenuPopView.share.Back_Height)

            }) { (isFinished) in

                CCBottomMenuPopView.share.listTableView.reloadData()
            }
            
         CCBottomMenuPopView.share.selectRowIndex = selectRowIndex
        }
       
        
        //提供给swift调用
        static func show(imageNameArray:[String], textArray:[String],_ selectRowIndex: @escaping (_ index:Int) -> Void){
            
            //初始化
            CCBottomMenuPopView.share.imageArray = imageNameArray
            CCBottomMenuPopView.share.textArray = textArray

            CCBottomMenuPopView.share.frame = CGRect.init(x: 0 , y: 0, width: KScreenWidth , height: KScreenHeight);
            CCBottomMenuPopView.share.backButton.frame = CCBottomMenuPopView.share.frame
            
            KWindow?.addSubview(CCBottomMenuPopView.share)
            KWindow?.bringSubviewToFront(CCBottomMenuPopView.share)

            //tableView高度
            CCBottomMenuPopView.share.Back_Height = CCBottomMenuPopView.share.Cell_Height*CGFloat(textArray.count)+CCBottomMenuPopView.share.HeaderView_Height+CCBottomMenuPopView.share.Cell_Height
            
            
            UIView.animate(withDuration: 0.5, animations: {
                
                CCBottomMenuPopView.share.listTableView.alpha = 1.0
                CCBottomMenuPopView.share.listTableView.frame = CGRect.init(x: 0, y: KScreenHeight - CCBottomMenuPopView.share.Back_Height, width: KScreenWidth, height: CCBottomMenuPopView.share.Back_Height)
                
            }) { (isFinished) in
                
                CCBottomMenuPopView.share.listTableView.reloadData()
            }
            
            
            CCBottomMenuPopView.share.selectRowIndex = selectRowIndex
        }
        
        
        @objc func hide() {
            UIView.animate(withDuration: 0.25, animations: { [self] in
                self.listTableView.alpha = 0
                self.alpha = 0
                self.listTableView.frame = CGRect.init(x: 0, y: KScreenHeight + 10 , width: KScreenWidth, height: Back_Height)
            }) { (isFinished) in
                self.removeFromSuperview()
                self.alpha = 1
            }
        }
        
        //MARK: - ========== UITableViewDataSource ==========
        //cell 个数
        func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            
            if section == 0{
                return textArray.count
            }else if section == 1{
                return 1
            }else{
                return 0
            }
        }
        
        // 设置cell高度
        func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
            
            return Cell_Height
        }
        
        
        //设置cell
        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            
            var cell:CCBottomMenuCell? = (tableView.dequeueReusableCell(withIdentifier: OtherCellId) as! CCBottomMenuCell)
            
            if cell == nil
            {
                cell = CCBottomMenuCell(style: .subtitle, reuseIdentifier: OtherCellId)
            }
            
            //去掉点击效果
            cell?.selectionStyle = .none
            cell?.backgroundColor = .clear
            
            
            if indexPath.section == 0
            {
                let text:String = textArray[indexPath.row]
                cell?.titleLabel.text = text
                
                
                if imageArray.count == textArray.count
                {
                    cell?.iconImageView.isHidden = false
                    let imageStr:String = imageArray[indexPath.row]
                    cell?.iconImageView.image = UIImage.init(named: imageStr)
                    cell?.iconImageView.snp.updateConstraints { make in
                        make.width.equalTo(32)
                    }
                }
                
            }else if indexPath.section == 1
            {
                cell?.titleLabel.text = NSLocalizedString("取消", comment: "")
                cell?.iconImageView.isHidden = true
                cell?.iconImageView.image = nil
                cell?.iconImageView.snp.updateConstraints { make in
                    make.width.equalTo(0)
                }
            }
             return cell!
            
        }
        
        func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
            
            self.hide()
            
            if indexPath.section == 0
            {
                selectRowIndex?(indexPath.row)
            }
         }
        
        //MARK: - ========== UITableViewDelegate ==========
        func numberOfSections(in tableView: UITableView) -> Int {
            return 2
            
        }
        func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
            
            if section == 0 {
                return 0
            }else {
                return HeaderView_Height
            }
        }
        
        func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
            
            if section == 0 {
                return nil
            }else{
                let headerView :UIView = UIView.init(frame: CGRect.init(x: 0, y: 0, width: tableView.frame.size.width, height: HeaderView_Height))
                
                headerView.backgroundColor = .black
                
                return headerView
            }
        }
        
        func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
            return 0
        }
        
        func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
            return nil
        }
        
        
        required init?(coder: NSCoder) {
            fatalError("init(coder:) has not been implemented")
        }
        
    }

