//
//  TSSetingVC.swift
//  TSLiveWallpaper
//
//  Created by 100Years on 2024/12/20.
//

class TSMineVC: TSBaseVC, UITableViewDataSource, UITableViewDelegate {

    lazy var tableView:UITableView = {
        let tableView = UITableView()
        tableView.initBaseTableView(reuseClass: ["TSMineCell"], isUseMJRefresh: false, delegate: self)
        return tableView
    }()
    
    lazy var navBarView: TSBaseNavContentBarView = {
        let navBarView = TSBaseNavContentBarView()
        let titleImageView = UIImageView.createImageView(imageName: "nav_title_setting",contentMode: .scaleToFill)
        
        navBarView.barView.addSubview(titleImageView)
        titleImageView.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.left.equalTo(16)
        }

        return navBarView
    }()
    
    lazy var dataArray:[TSBasicSectionModel] = {
        var dataArray = [TSBasicSectionModel]()
        let sectionModel = TSBasicSectionModel()
        dataArray.append(sectionModel)
        
        sectionModel.addSubItemModel(
            TSBasicItemModel.createItemModel(
            leftTitle: "Rate us".localized,
            rightViewStyle: 0,
            rightString: "",
            rightIsHave: true,
            height: 80,
            rectCorner:.allCorners,
            tapBlock: {[weak self] itemModel, index, view  in
                
                guard let self = self else { return }
                let appStoreLink = "itms-apps://itunes.apple.com/app/id\(TSConfig.appid)"
                if let url = URL(string: appStoreLink + "?action=write-review"),
                   UIApplication.shared.canOpenURL(url) {
                    UIApplication.shared.open(url)
                }
            }))
        
        sectionModel.addSubItemModel(
            TSBasicItemModel.createItemModel(
            leftTitle: "Share us".localized,
            rightViewStyle: 0,
            rightString: "",
            rightIsHave: true,
            height: 80,
            rectCorner:.allCorners,
            tapBlock: {[weak self] itemModel, index, view  in
                guard let self = self else { return }

                let httpAppStoreLink = "https://apps.apple.com/app/id\(TSConfig.appid)"
                let text = "I'm using Sweeter to decorate my phone, there are not only themes, wallpapers, widgets, but also dynamic island and super useful tools, come and try with me!".localized
                let url = URL(string: httpAppStoreLink)!
                let image = UIImage(named: "App-Icon")!
                let vc = UIActivityViewController(activityItems: [image, text, url], applicationActivities: nil)
                vc.completionWithItemsHandler = { activity, value, _, error in
                    if let type = activity, type == .copyToPasteboard {
                        UIPasteboard.general.string = httpAppStoreLink
                    }
                }
            
                if UIDevice.isPad {
                    if let popover = vc.popoverPresentationController {
                        popover.sourceView = self.view // 设置锚点视图
                        popover.sourceRect = CGRect(x: self.view.bounds.midX, y: self.view.bounds.midY, width: 0, height: 0) // 设置弹窗位置为屏幕中心
                        popover.permittedArrowDirections = [] // 禁止箭头指向
                    }
                }
                
                self.present(vc, animated: true)
                
            }))
        
        sectionModel.addSubItemModel(
            TSBasicItemModel.createItemModel(
            leftTitle: "Privacy Policy".localized,
            rightViewStyle: 0,
            rightString: "",
            rightIsHave: true,
            height: 80,
            rectCorner:.allCorners,
            tapBlock: {[weak self] itemModel, index, view  in
                guard let self = self else { return }
                let vc = TSBusinessWebVC(urlType: .privacy)
                vc.hidesBottomBarWhenPushed = true
                navigationController?.pushViewController(vc, animated: true)
            }))
        
        sectionModel.addSubItemModel(
            TSBasicItemModel.createItemModel(
            leftTitle: "Terms of Service".localized,
            rightViewStyle: 0,
            rightString: "",
            rightIsHave: true,
            height: 80,
            rectCorner:.allCorners,
            tapBlock: {[weak self] itemModel, index, view  in
                guard let self = self else { return }
                let vc = TSBusinessWebVC(urlType: .terms)
                vc.hidesBottomBarWhenPushed = true
                navigationController?.pushViewController(vc, animated: true)
            }))
        
        sectionModel.addSubItemModel(
            TSBasicItemModel.createItemModel(
            leftTitle: "About us".localized,
            rightViewStyle: 0,
            rightString: appVersion(),
            rightIsHave: false,
            height: 80,
            rectCorner:.allCorners,
            tapBlock: {[weak self] itemModel, index, view  in
//                guard let self = self else { return }
//                let vc = TSAboutUsVC()
//                vc.hidesBottomBarWhenPushed = true
//                navigationController?.pushViewController(vc, animated: true)
            }))
        
        return dataArray
        
    }()
    
    override func createView() {
        
        setViewBgImageNamed(named: "view_main_bg")
        
        navBarContentView.addSubview(navBarView)
        navBarView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        contentView.addSubview(tableView)
        tableView.snp.makeConstraints { make in
            make.leading.equalTo(16)
            make.trailing.equalTo(-16)
            make.top.bottom.equalTo(0)
        }
    }
}

extension TSMineVC {
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return dataArray.count
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if let sectionModel = dataArray.safeObj(At: section){
            return sectionModel.itemsArray.count
        }
        return 0
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        if let sectionModel = dataArray.safeObj(At: indexPath.section),let itemModel = sectionModel.itemsArray.safeObj(At: indexPath.row){
            return itemModel.height
        }
        return 0
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "TSMineCell") as! TSMineCell
        if let sectionModel = dataArray.safeObj(At: indexPath.section),let itemModel = sectionModel.itemsArray.safeObj(At: indexPath.row){
            cell.itemModel = itemModel
        }
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if let sectionModel = dataArray.safeObj(At: indexPath.section),let itemModel = sectionModel.itemsArray.safeObj(At: indexPath.row){
            itemModel.tapBlock?(itemModel,indexPath.row,nil)
        }
    }
}
