//
//  CCSpatialPlayView.swift
//  SwiftProject
//
//  Created by aaa on 2024/1/29.
//

import UIKit



//typealias CCSpatialPlayViewBlock = (_ dataDic:NSDictionary) -> ()

typealias CCSpatialPlayViewBlock = (_ dataDic:(type:CCTransformParameterType,title:String,name:String)) -> ()


class CCSpatialPlayView: UIView,UITableViewDelegate,UITableViewDataSource{

    var handle:CCSpatialPlayViewBlock?
    var mTableView:UITableView?
//    var datas = NSMutableArray()
    
    var datas : [(type:CCTransformParameterType,title:String,name:String)]!
    
    
//    var type:CCFileType = CCFileType_Photo_Normal
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
//        self.backgroundColor = UIColor.colorWithRGB(_r: 28, _g: 28, _b: 29)
        
        
        self.mTableView = UITableView.init(frame: self.bounds, style: UITableView.Style.grouped)
        self.addSubview(self.mTableView!)
        self.mTableView!.delegate = (self as UITableViewDelegate)
        self.mTableView!.dataSource = (self as UITableViewDataSource)
        self.mTableView!.backgroundColor = UIColor.clear
        self.mTableView!.backgroundView?.backgroundColor = UIColor.clear
        self.mTableView!.rowHeight = UITableView.automaticDimension
        self.mTableView!.sectionHeaderHeight = 0
        self.mTableView!.sectionFooterHeight = 0
        self.mTableView!.estimatedRowHeight = 70
        self.mTableView?.contentInset = UIEdgeInsets.init(top: 0, left: 0, bottom: 0, right: 0)
        self.mTableView!.scrollIndicatorInsets =  self.mTableView!.contentInset
        self.mTableView!.separatorStyle = UITableViewCell.SeparatorStyle.none
        
        mTableView!.register(CCSpatialPlayCell1.classForCoder(), forCellReuseIdentifier: CCSpatialPlayCell1Id)
        mTableView!.register(CCSpatialPlayCell2.classForCoder(), forCellReuseIdentifier: CCSpatialPlayCell2Id)
        
        addDatas()
    }
    
    func addDatas(){
//        let arr = [["title":"3D 格式",
//                    "name":"3D HSBS"],
//                   ["title":"分辨率",
//                    "name":"1080P"],
//                   ["title":"比特率",
//                    "name":"最佳"],
//                   ["title":"视频编码格式",
//                    "name":"H.265"],
//                   ["title":"水平视差调整",
//                    "name":"0"]]
        
        let popData = [
            (type:CCTransformParameterType.format3D,title:"3D 格式",name:"3D HSBS"),
            (type:CCTransformParameterType.resolution,title:"分辨率",name:"1080P"),
            (type:CCTransformParameterType.bitRate,title:"比特率",name:"最佳"),
            (type:CCTransformParameterType.formatVideoEncoding,title:"视频编码格式",name:"H.265"),
            (type:CCTransformParameterType.horizontalDialogueAdjustment,title:"水平视差调整",name:"0")]
        
        datas = popData
        self.mTableView!.reloadData()
    }
    
    func setSpatialParametersData(data:[(type:CCTransformParameterType,title:String,name:String)]) -> Void {
        datas = data
        self.mTableView!.reloadData()
    }
    
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 0.01
    }
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 0.01
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return CCSpatialPlayCell1H
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return datas.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        if(indexPath.row != 4){
            let cell:CCSpatialPlayCell1 = tableView.dequeueReusableCell(withIdentifier: CCSpatialPlayCell1Id)! as! CCSpatialPlayCell1
//            cell.selectionStyle = UITableViewCell.SelectionStyle.none
            cell.indexPath = indexPath
            cell.dataDic = datas[indexPath.row]
            if indexPath.row == datas.count - 1 {
                cell.mLine?.isHidden = true
            }else{
                cell.mLine?.isHidden = false
            }
            return cell
        }else{
            let cell:CCSpatialPlayCell2 = tableView.dequeueReusableCell(withIdentifier: CCSpatialPlayCell2Id)! as! CCSpatialPlayCell2
            cell.selectionStyle = UITableViewCell.SelectionStyle.none
            cell.indexPath = indexPath
            cell.dataDic = datas[indexPath.row]
            if indexPath.row == datas.count - 1 {
                cell.mLine?.isHidden = true
            }else{
                cell.mLine?.isHidden = false
            }
            return cell
        }
        
        
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        
        let model:(type:CCTransformParameterType,title:String,name:String) = datas[indexPath.row]
        self.handle!(model)
    }
    
    
}


let CCSpatialPlayCell1Id = "CCSpatialPlayCell1Id"
let CCSpatialPlayCell1H = 62.0
let CCSpatialPlayCell1W = SCREEN_Width - 48
class CCSpatialPlayCell1: UITableViewCell {
    
    
    var mLabel1:UILabel?
    var mLabel2:UILabel?
    var mImgView:UIImageView?
    var mLine:UIView?
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: UITableViewCell.CellStyle.default, reuseIdentifier: reuseIdentifier)
        self.backgroundColor = UIColor.clear
        self.contentView.backgroundColor = UIColor.clear
        
        mLabel1 = UILabel()
        mLabel1!.bounds = CGRect(x: 0, y: 0, width: 100, height: 0)
        mLabel1!.textColor = UIColor.white
        mLabel1!.font = UIFont.systemFont(ofSize: 14)
        self.contentView.addSubview(mLabel1!)
        
        mLabel2 = UILabel()
        mLabel2!.bounds = CGRect(x: 0, y: 0, width: 100, height: 0)
        mLabel2!.textColor = UIColor.hexStringToColor(hexString: "#828283")
        mLabel2!.font = UIFont.systemFont(ofSize: 14)
        self.contentView.addSubview(mLabel2!)
        
        mImgView = UIImageView()
        mImgView!.bounds = CGRect(x: 0, y: 0, width: 14, height: 14)
        mImgView!.centerY = CCSpatialPlayCell1H * 0.5
        mImgView!.right = CCSpatialPlayCell1W - 16
        self.contentView.addSubview(mImgView!)
        
        
        mLine = UIView()
        mLine!.frame = CGRect(x: 16, y: CCSpatialPlayCell1H - 0.5, width: CCSpatialPlayCell1W - 32, height: 0.5)
        mLine!.backgroundColor = UIColor.colorWithRGB(_r: 255, _g: 255, _b: 255, alpha: 0.5)
        self.contentView.addSubview(mLine!)
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    public var indexPath:IndexPath?
    private var _dataDic:(type:CCTransformParameterType,title:String,name:String)?
    var dataDic:(type:CCTransformParameterType,title:String,name:String)?{
        didSet{
            _dataDic = dataDic
            
            mImgView!.image = UIImage(named: "rig_icon")
            
            mLabel1!.text = _dataDic?.title
            mLabel1!.sizeToFit()
            mLabel1!.centerY = CCSpatialPlayCell1H * 0.5
            mLabel1!.left = 16
            
            mLabel2!.text = _dataDic?.name
            mLabel2!.sizeToFit()
            mLabel2!.centerY = CCSpatialPlayCell1H * 0.5
            mLabel2!.right = CCSpatialPlayCell1W - 35
            
            if(indexPath?.row != 4){
                mLine!.isHidden = false
            }else{
                mLine!.isHidden = true
            }
        }
    }
}




let CCSpatialPlayCell2Id = "CCSpatialPlayCell2Id"
let CCSpatialPlayCell2H = 62.0
let CCSpatialPlayCell2W = SCREEN_Width - 48
class CCSpatialPlayCell2: UITableViewCell {
    
    
    var mLabel1:UILabel?
    var mySwitch:UISwitch?
    var mImgView:UIImageView?
    var mLine:UIView?
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: UITableViewCell.CellStyle.default, reuseIdentifier: reuseIdentifier)
        self.backgroundColor = UIColor.clear
        self.contentView.backgroundColor = UIColor.clear
        
        mLabel1 = UILabel()
        mLabel1!.bounds = CGRect(x: 0, y: 0, width: 100, height: 0)
        mLabel1!.textColor = UIColor.white
        mLabel1!.font = UIFont.systemFont(ofSize: 14)
        self.contentView.addSubview(mLabel1!)
        
        mySwitch = UISwitch()
        mySwitch!.frame = CGRect(x: 100, y: 100, width: 58, height: 27)
        mySwitch!.right = CCSpatialPlayCell2W - 35
        mySwitch!.centerY = CCSpatialPlayCell2H * 0.5
        mySwitch!.isOn = false
        mySwitch!.clipsToBounds = true
        mySwitch!.layer.cornerRadius = mySwitch!.height * 0.5
        mySwitch!.backgroundColor = UIColor.gray
        mySwitch!.onTintColor = UIColor.blue
        self.contentView.addSubview(mySwitch!)
        mySwitch!.addTarget(self, action: #selector(switchValueChanged), for: .valueChanged)
        
        mImgView = UIImageView()
        mImgView!.bounds = CGRect(x: 0, y: 0, width: 14, height: 14)
        mImgView!.centerY = CCSpatialPlayCell1H * 0.5
        mImgView!.right = CCSpatialPlayCell1W - 16
        self.contentView.addSubview(mImgView!)
        
        
        mLine = UIView()
        mLine!.frame = CGRect(x: 16, y: CCSpatialPlayCell1H - 0.5, width: CCSpatialPlayCell1W - 32, height: 0.5)
        mLine!.backgroundColor = UIColor.colorWithRGB(_r: 255, _g: 255, _b: 255, alpha: 0.5)
        self.contentView.addSubview(mLine!)
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    @objc func switchValueChanged(_ sender: UISwitch) {
            if sender.isOn {
                // 开关被打开
                print("开关被打开")
            } else {
                // 开关被关闭
                print("开关被关闭")
            }
        }
    
    public var indexPath:IndexPath?
    private var _dataDic:(type:CCTransformParameterType,title:String,name:String)?
    var dataDic:(type:CCTransformParameterType,title:String,name:String)?{
        didSet{
            _dataDic = dataDic
            
            mImgView!.image = UIImage(named: "rig_icon")
            
            mLabel1!.text = _dataDic?.title
            mLabel1!.sizeToFit()
            mLabel1!.centerY = CCSpatialPlayCell1H * 0.5
            mLabel1!.left = 16
            
            
            if(indexPath?.row != 4){
                mLine!.isHidden = false
            }else{
                mLine!.isHidden = true
            }
        }
    }
}



