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

class TSMineVC: TSBaseVC {

    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 vipBgImageView: UIImageView = {
        let vipBgImageView = UIImageView.createImageView(imageName: "noVip_big_bg",contentMode:.scaleToFill)
        vipBgImageView.isUserInteractionEnabled = true
        return vipBgImageView
    }()
    
    
    lazy var vipTimeLabel: UILabel = {
        let vipTimeLabel = UILabel.createLabel(text: "Limited Time Discount",font: .font(size: 14),textColor: .lesserText)
        return vipTimeLabel
    }()
    
    lazy var upgradeBtn: UIButton = {
        let upgradeBtn = TSViewTool.createNormalSubmitBtn(title: "Upgrade".localized) { [weak self]  in
            guard let self = self else { return }
            TSPurchaseVC.show(target: self) {[weak self]  in
                guard let self = self else { return }
                updateVipView()
            }
        }
        upgradeBtn.cornerRadius = 13
        upgradeBtn.titleLabel?.font = .font(size: 14)
        return upgradeBtn
    }()
    
    lazy var headerView: UIView = {
        let headerView = UIView()
        headerView.frame = CGRect(x: 0, y: 0, width: k_ScreenWidth, height: 88)
        headerView.addSubview(vipBgImageView)
        vipBgImageView.snp.makeConstraints { make in
            make.top.equalTo(0)
            make.leading.trailing.bottom.equalTo(0)
        }
        
        let liveImageView = UIImageView.createImageView(imageName: "livelive")
        vipBgImageView.addSubview(liveImageView)
        liveImageView.snp.makeConstraints { make in
            make.leading.equalTo(16)
            make.top.equalTo(19)
        }
        
        vipBgImageView.addSubview(vipTimeLabel)
        vipTimeLabel.snp.makeConstraints { make in
            make.leading.equalTo(16)
            make.bottom.equalTo(-19)
            make.height.equalTo(14.0)
        }
        
        vipBgImageView.addSubview(upgradeBtn)
        upgradeBtn.snp.makeConstraints { make in
            make.width.equalTo(86)
            make.height.equalTo(26)
            make.trailing.equalTo(-29)
            make.centerY.equalToSuperview()
        }
        

        return headerView
    }()
    
    
    lazy var simpleTableView:TSSimpleTableView = {
        let simpleTableView = TSSimpleTableView()
        simpleTableView.reuseClass = ["TSMineCell"]
        simpleTableView.dataArray = dataArray
        simpleTableView.tableView.tableHeaderView = headerView
        return simpleTableView
    }()
    
    
    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 = ""
                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 }

#if DEBUG
                let vc = TSPurchaseVC()
                vc.hidesBottomBarWhenPushed = true
                kPresentModalVC(target: self, modelVC: vc)
#endif
                
            }))
        
        return dataArray
        
    }()
    
    override func createView() {
        
        setViewBgImageNamed(named: "view_main_bg")
        
        navBarContentView.addSubview(navBarView)
        navBarView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        contentView.addSubview(simpleTableView.tableView)
        simpleTableView.tableView.snp.makeConstraints { make in
            make.leading.equalTo(16)
            make.trailing.equalTo(-16)
            make.top.equalTo(16)
            make.bottom.equalTo(0)
        }
    }
    
    override func viewWillAppear(_ animated: Bool) {
        updateVipView()
    }
    
    func updateVipView(){
        vipTimeLabel.text = PurchaseManager.default.expiredDateString
        vipBgImageView.image =  UIImage(named: PurchaseManager.default.isVip ? "vip_big_bg" : "noVip_big_bg")
        
        if PurchaseManager.default.isVip {
            vipTimeLabel.text = "Due Date: \(PurchaseManager.default.expiredDateString)"
            vipBgImageView.image =  UIImage(named: "vip_big_bg")
            upgradeBtn.isHidden = true
        }else{
            vipTimeLabel.text = "Limited Time Discount"
            vipBgImageView.image =  UIImage(named:"noVip_big_bg")
            upgradeBtn.isHidden = false
        }
    }
}
