//
//  TSSettingVC.swift
//  Girly
//
//  Created by 100Years on 2025/1/7.
//

class TSSettingVC: 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 simpleTableView:TSSimpleTableView = {
        let simpleTableView = TSSimpleTableView()
        simpleTableView.reuseClass = ["TSSettingCell"]
        return simpleTableView
    }()
    
    lazy var dataArray:[TSBasicSectionModel] = {
        var dataArray = [TSBasicSectionModel]()
        let sectionModel = TSBasicSectionModel()
        dataArray.append(sectionModel)
        
        let cellH = 64.0
        
        sectionModel.addSubItemModel(
            createItemModel(
            leftTitle: "Share us".localized,
            rightViewStyle: 0,
            rightString: "",
            rightIsHave: true,
            bottomIsHave: true,
            height: cellH,
            rectCorner:[.topLeft,.topRight],
            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 transforming my selfies with Girly! Not only do I have quality lighting, but I also have a small mirror for me to touch up my makeup whenever I want, come and try them out 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(
            createItemModel(
            leftTitle: "Rate us".localized,
            rightViewStyle: 0,
            rightString: "",
            rightIsHave: true,
            bottomIsHave: true,
            height: cellH,
            rectCorner:nil,
            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(
            createItemModel(
            leftTitle: "User Agreement".localized,
            rightViewStyle: 0,
            rightString: "",
            rightIsHave: true,
            bottomIsHave: true,
            height: cellH,
            rectCorner:nil,
            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(
            createItemModel(
            leftTitle: "Privacy Policy".localized,
            rightViewStyle: 0,
            rightString: "",
            rightIsHave: true,
            bottomIsHave: true,
            height: cellH,
            rectCorner:nil,
            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(
            createItemModel(
            leftTitle: "About us".localized,
            rightViewStyle: 0,
            rightString: appVersion(),
            rightIsHave: false,
            bottomIsHave: false,
            height: cellH,
            rectCorner:[.bottomLeft,.bottomRight],
            tapBlock: {[weak self] itemModel, index, view  in

            }))
        
        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.bottom.equalTo(10)
        }

        simpleTableView.dataArray = dataArray
    }
    
    
    func createItemModel(leftTitle: String,
                    rightViewStyle: Int,
                       rightString: String,
                       rightIsHave: Bool,
                      bottomIsHave: Bool,
                            height: CGFloat,
                        rectCorner: UIRectCorner?,
                          tapBlock: @escaping ((TSBasicItemModel, Int, Any?) -> Void)) -> TSBasicItemModel {
        let model = TSBasicItemModel()
        model.leftTitle = leftTitle
        model.rightViewStyle = rightViewStyle
        model.rightString = rightString
        model.rightIsHave = rightIsHave
        model.bottomIsHave = bottomIsHave
        model.height = height
        model.rectCorner = rectCorner
        model.tapBlock = tapBlock
        return model
    }
}
