//
//  CCMineView.swift
//  SwiftProject
//
//  Created by soldoros on 2024/1/10.
//

import UIKit

typealias CCMineViewBlock = (_ dataDic:NSDictionary) -> ()

class CCMineView: UIView,UITableViewDelegate,UITableViewDataSource{

    var handle:CCMineViewBlock?
    var mTableView:UITableView?
    var datas = NSMutableArray()
    
    var mImgView:UIImageView?
    var mLabel:UILabel?
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        
        let layer0 = CAGradientLayer()
        layer0.colors = [
          UIColor(red: 0.172, green: 0.153, blue: 0.212, alpha: 1).cgColor,
          UIColor(red: 0.271, green: 0.176, blue: 0.521, alpha: 1).cgColor
        ]
        layer0.locations = [0, 1]
        layer0.startPoint = CGPoint(x:0.5, y: 0)
        layer0.endPoint = CGPoint(x: 0.5, y: 1)
        layer0.frame = self.bounds
        self.layer.addSublayer(layer0)
        
        mImgView = UIImageView()
        mImgView!.bounds = CGRect(x: 0, y: 0, width: 60, height: 60)
        mImgView!.top = KStatusBarHeight+20
//        mImgView!.left = 32
        mImgView!.centerX = self.width*0.5
        self.addSubview(mImgView!)
        mImgView!.backgroundColor = BackGroundColor
        mImgView!.image = UIImage(named: "logo")
        mImgView!.clipsToBounds = true
        mImgView!.layer.cornerRadius = 4
       
        
//        mLabel = UILabel()
//        mLabel!.bounds = CGRect(x: 0, y: 0, width: 100, height: 0)
//        mLabel!.textColor = UIColor.white
//        mLabel!.font = UIFont.systemFont(ofSize: 16)
//        self.addSubview(mLabel!)
//        mLabel!.text = "VPCamera"
//        mLabel!.sizeToFit()
//        mLabel!.centerY = mImgView!.centerY
//        mLabel!.left = mImgView!.right + 15
        
        
        self.mTableView = UITableView.init(frame: CGRect.init(x: 0, y: 152, width: self.width, height: self.height - 100), 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(CCMineCell.classForCoder(), forCellReuseIdentifier: CCMineCellId)
        addDatas()
    }
    
    func addDatas(){
//        let arr = [["img":"Icon_Menu_all_Icon_Connect",
//                    "name":"分享给VR爱好者"],
//                   ["img":"Icon_Menu_all_Icon_Connect",
//                               "name":"联系我们"],
//                   ["img":"Icon_Menu_all_Icon_Connect",
//                               "name":"隐私政策"]]
        
        let arr = [["img":"Icon_Menu_all_Icon_Connect",
                               "name":NSLocalizedString("联系我们", comment: "")],
                   ["img":"Icon_Menu_all_Icon_Privacy",
                               "name":NSLocalizedString("隐私政策", comment: "")],
                   ["img":"userxieyi",
                               "name":NSLocalizedString("用户协议", comment: "")]]
        
        
        datas.addObjects(from: arr)
        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 CCMineCellH
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return datas.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell:CCMineCell = tableView.dequeueReusableCell(withIdentifier: CCMineCellId)! as! CCMineCell
        cell.selectionStyle = UITableViewCell.SelectionStyle.none
        cell.indexPath = indexPath
        cell.dataDic = (datas[indexPath.row] as! NSDictionary)
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        self.handle!((datas[indexPath.row] as! NSDictionary))
    }
    
    
}


let CCMineCellId = "CCMineCellId"
let CCMineCellH = 72.0
let CCMineCellW = SCREEN_Width - 108
class CCMineCell: UITableViewCell {
    
    var mImgView:UIImageView?
    var mLabel:UILabel?
    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
        
        mImgView = UIImageView()
        mImgView!.bounds = CGRect(x: 0, y: 0, width: 24, height: 24)
        mImgView!.centerY = CCMineCellH * 0.5
        mImgView!.left = 32
        self.contentView.addSubview(mImgView!)
        
        mLabel = UILabel()
        mLabel!.bounds = CGRect(x: 0, y: 0, width: 100, height: 0)
        mLabel!.textColor = UIColor.white
        mLabel!.font = UIFont.systemFont(ofSize: 14)
        self.contentView.addSubview(mLabel!)
        
        mLine = UIView()
        mLine!.frame = CGRect(x: 16, y: CCMineCellH - 0.5, width: CCMineCellW - 32, height: 0.5)
        mLine!.backgroundColor = UIColor.colorWithRGB(_r: 255, _g: 255, _b: 255, alpha: 0.2)
        self.contentView.addSubview(mLine!)
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    public var indexPath:IndexPath?
    private var _dataDic:NSDictionary?
    var dataDic:NSDictionary?{
        didSet{
            _dataDic = dataDic
            
            mImgView!.image = UIImage(named: _dataDic!["img"] as! String)
            mLabel!.text = (_dataDic!["name"] as! String)
            mLabel!.sizeToFit()
            mLabel!.centerY = CCMineCellH * 0.5
            mLabel!.left = mImgView!.right + 10
            
            if(indexPath?.row != 2){
                mLine!.isHidden = false
            }else{
                mLine!.isHidden = true
            }
        }
    }
}
