//
//  TSSetingVC.swift
//  KittensTravelNotes
//
//  Created by 100Years on 2025/7/5.
//

let appid = "6743612470"
class TSSetingVC: TSBaseVC {
    
    lazy var viewModel: TSSetingVM = {
        let viewModel = TSSetingVM(target: self)
        return viewModel
    }()
    lazy var headerView: TSSetingTopView = {
        let headerView = TSSetingTopView()
        headerView.frame = CGRect(x: 0, y: 0, width: k_ScreenWidth - 32, height: 116)
        headerView.clickUpgrade = { [weak self]  in
            guard let self = self else { return }
            if PurchaseManager.default.isVip { return }
            TSPurchaseVC.show(target: self)
        }
        return headerView
    }()

    lazy var navBarView: TSBaseNavContentBarView = {
        let navBarView = TSBaseNavContentBarView()

        let label = UILabel.createLabel(text: "Setting".localized,font: .font(name: .CherryBombOneRegular, size: 30))
        navBarView.barView.addSubview(label)
        label.snp.makeConstraints { make in
           make.center.equalToSuperview()
        }
        
       return navBarView
    }()
    
    lazy var simpleTableView: TSBaseTableView = {
        let simpleTableView = TSBaseTableView()
        simpleTableView.register(TSSetingCell.self, forCellReuseIdentifier: "TSSetingCell")
        simpleTableView.tableHeaderView = headerView
        simpleTableView.delegate = self
        simpleTableView.dataSource = self
        return simpleTableView
    }()

    override func createView() {
        addGradientColorTop(alpha: 0.8)
        navBarContentView.addSubview(navBarView)
        navBarView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        contentView.addSubview(simpleTableView)
        simpleTableView.snp.makeConstraints { make in
            make.leading.equalTo(16)
            make.trailing.equalTo(-16)
            make.top.equalTo(16)
            make.bottom.equalTo(0)
        }
    }

    override func dealThings() {
        NotificationCenter.default.addObserver(self, selector: #selector(refreshView), name: .kAppUpdateNotification, object: nil)
        NotificationCenter.default.addObserver(self, selector: #selector(updateVipView), name: .kPurchaseDidChanged, object: nil)
        updateVipView()
        refreshView()
    }
    
    @objc func updateVipView() {
        kMainAsync {
            self.headerView.updateVipView()
            self.simpleTableView.reloadData()
        }
    }
    
    @objc func refreshView() {
//        self.viewModel.updateItem.isWhether = TSAppUpdateManager.isNeedUpdate
        self.simpleTableView.reloadData()
    }
}
extension TSSetingVC : UITableViewDataSource, UITableViewDelegate {
    
    public func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return viewModel.dataArray.count
    }
    
    public func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 64
    }
    
    public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if let cell = tableView.dequeueReusableCell(withIdentifier: "TSSetingCell") as? TSSetingCell{
            if let itemModel = viewModel.dataArray.safeObj(At: indexPath.row){
                cell.itemModel = itemModel
            }
            return cell
        }
        
        return UITableViewCell()
    }
    
    public func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if let itemModel = viewModel.dataArray.safeObj(At: indexPath.row){
            itemModel.tapBlock(itemModel,indexPath.row,nil)
        }
    }
}
