//
//  CCDeviceOperationListView.swift
//  SwiftProject
//
//  Created by Zhang, Joyce on 2024/2/24.
//

import UIKit

private let Back_Height = 700

class CCDeviceOperationListView: UIView,UITableViewDelegate, UITableViewDataSource {
    
    var dataArray: [[String:String]]=[["iconImageName":"device_vr","name":NSLocalizedString("雷鸟Air2", comment: ""),"tipsImageName":"vr_1","tips":NSLocalizedString("左右按键同时按一次", comment: "")],
                                      ["iconImageName":"device_vr","name":NSLocalizedString("雷鸟Air Pro", comment: ""),"tipsImageName":"vr_2","tips":NSLocalizedString("右侧拨轮下按3s", comment: "")],
                                      ["iconImageName":"device_vr","name":"Xreal Alr2","tipsImageName":"vr_3","tips":NSLocalizedString("右侧中间键长按3s", comment: "")],
                                      ["iconImageName":"device_vr","name":"Rokid max","tipsImageName":"vr_4","tips":NSLocalizedString("右侧亮度循环按键", comment: "")]]
    
    let TopCellId = "CCDeviceOperationListCell"
    
    lazy var backView: UIView = {
        let imageView = UIView()
        imageView.backgroundColor = UIColor(hexString: "#1F1E20")
        
        return imageView
    }()
    
    lazy var listTableView:UITableView = {
        
        let tableView = UITableView(frame: CGRect.zero, style: .plain)
        tableView.showsVerticalScrollIndicator = false
        tableView.backgroundColor = UIColor(hexString: "#1F1E20")
//        tableView.backgroundColor = .orange
        tableView.delegate = self
        tableView.dataSource = self
        tableView.register(CCDeviceOperationListCell.self, forCellReuseIdentifier: TopCellId)
        tableView.contentInsetAdjustmentBehavior = .never
        tableView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0 )
//        tableView.estimatedRowHeight = 0
//        tableView.estimatedSectionHeaderHeight = 0
//        tableView.estimatedSectionFooterHeight = 0
        //去掉下划线
        tableView.separatorStyle = .none
        return tableView
        
    }()
    
    lazy var closeButton: UIButton = {
        
        let button = UIButton(type: .custom)
        //        button.setImage(UIImage.init(named: "login_button_normal"), for: .normal)
        //        button.setImage(UIImage.init(named: "login_ckeck_button_selected"), for: .highlighted)
        button.backgroundColor = KMain_Color
        button.layer.cornerRadius = 8
        button.clipsToBounds = true
        button.titleLabel?.font = KFont_Medium(14)
        button.titleLabel?.textColor = KTextColor_White
        button.setTitle("了解了", for: .normal)
        button.setTitleColor(KTextColor_White, for: .normal)
        button.addTarget(self, action: #selector(closeButtonAction), for: .touchUpInside)
        
        return button
        
    }();
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.backgroundColor = UIColor.black.withAlphaComponent(0.5)
        
        self.addSubview(backView)
        backView.addSubview(closeButton)
        backView.addSubview(listTableView)
        
        
        backView.frame = CGRect.init(x: 0, y: KScreenHeight+10  , width: KScreenWidth, height: CGFloat(Back_Height))
        
        
        closeButton.snp.makeConstraints { (make) in
            make.bottom.equalTo(backView.snp.bottom).offset(-36)
            make.centerX.equalTo(backView.snp.centerX)
            make.width.equalTo(345)
            make.height.equalTo(54)
        }
        
        listTableView.snp.makeConstraints { (make) in
            make.bottom.equalTo(closeButton.snp.top).offset(-16)
            make.leading.equalTo(backView.snp.leading)
            make.trailing.equalTo(backView.snp.trailing)
            make.top.equalToSuperview()
            
        }
        
        show()
    }
    
    func show() {
        UIView.animate(withDuration: 0.25, animations: {
            self.listTableView.alpha = 1.0
            
            self.backView.frame = CGRect.init(x: 0, y: Int(KScreenHeight) - Back_Height, width: Int(KScreenWidth), height: Back_Height)
        }) { (isFinished) in
            
            self.listTableView.reloadData()
        }
    }
    
    
    func hide() {
        UIView.animate(withDuration: 0.25, animations: { [self] in
            self.listTableView.alpha = 0
            self.backView.frame = CGRect.init(x: 0, y: KScreenHeight + 10 , width: KScreenWidth, height: CGFloat(Back_Height))
        }) { (isFinished) in
            self.removeFromSuperview()
        }
    }
    
    
    @objc func closeButtonAction(_ btn:UIButton) {
        print("点击了取消")
        self.hide()
    }
    
    //MARK: - ========== UITableViewDataSource ==========
    //cell 个数
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return dataArray.count
    }
    
    // 设置cell高度
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 125
    }
    
    
    //设置cell
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        var cell:CCDeviceOperationListCell? = (tableView.dequeueReusableCell(withIdentifier: TopCellId) as! CCDeviceOperationListCell)
        
        if cell == nil {
            cell = CCDeviceOperationListCell(style: .subtitle, reuseIdentifier: TopCellId)
        }
        
        //去掉点击效果
        cell?.selectionStyle = .none
        cell?.backgroundColor = .clear
        
        let dict = dataArray[indexPath.row]
        let iconImageName:String = dict["iconImageName"]!
        let tipsImageName:String = dict["tipsImageName"]!
        let name:String = dict["name"]!
        let tips:String = dict["tips"]!
        cell?.setCellData(iconImageName: iconImageName, name: name, tipsImageName: tipsImageName, tips: tips)
        return cell!
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
    }
    
    //MARK: - ========== UITableViewDelegate ==========
    func numberOfSections(in tableView: UITableView) -> Int {
        
        return 1
        
    }
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 52
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        
        
        let headerView :UIView = UIView.init(frame: CGRect.init(x: 0, y: 0, width: tableView.frame.size.width, height: 52))
        headerView.backgroundColor = backView.backgroundColor//.clear
        //title
        let label = UILabel()
        label.text = "常用AR设备操作"
        label.backgroundColor = UIColor.clear
        label.font = KFont_Medium(18)
        label.textColor = KTextColor_White
        label.textAlignment = .center
        
        headerView.addSubview(label)
        //布局
        label.snp.makeConstraints { (make) in
            make.centerY.equalTo(headerView.snp.centerY)
            make.centerX.equalTo(headerView.snp.centerX)
        }
        
        return headerView
    }
    
    
    
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}
