//
//  MembershipVCConfigureView.swift
//  SwiftProject
//
//  Created by aaa on 2024/4/1.
//

import Foundation
import UIKit
extension MembershipVC {
    
    
    func configureUI() {
        
        
        self.view.backgroundColor = .black
        self.navLine?.isHidden = true
        self.setLeftOneBtnImg(imgStr: "membership_close")
        self.leftBtn1?.addTarget(self, action: #selector(dismissVC(sender: )), for: .touchUpInside)
//        self.setRightOneBtnImg(imgStr: "Icon_PopUp_Icon_Folder")
//        self.rightBtn1?.addTarget(self, action: #selector(rebuyMembership(sender: )), for: .touchUpInside)
        self.setRightOneBtnTitle(string: NSLocalizedString("restorevip", comment: "") as NSString)
//        self.rightBtn1?.isHidden = UserInfo.sharedInstance.isMemberShip
//        self.rightBtn1?.isEnabled = true
        
        rightBtn1 = UIButton.init(type: UIButton.ButtonType.custom)
        rightBtn1?.isHidden = UserInfo.sharedInstance.isMemberShip
        navtionBar?.addSubview(rightBtn1!)
        rightBtn1?.tag = 10
        rightBtn1?.isSelected = false
        rightBtn1?.addTarget(self, action: #selector(rebuyMembership(sender: )), for: .touchUpInside)
        rightBtn1?.setTitle(NSLocalizedString("restorevip", comment: ""), for: .normal)
        rightBtn1?.frame = CGRect(x: 2, y: 10, width: 76, height: 56)
        rightBtn1!.centerY = StatuBar_Height + NavBar_Height * 0.5
        rightBtn1!.right = SCREEN_Width - 6
        
        
        self.view.addSubview(mTopImgView)
        
        let scrollView = UIScrollView()
        self.view.addSubview(scrollView)
        scrollView.snp.makeConstraints { make in
            make.left.right.bottom.equalTo(0)
            make.top.equalToSuperview().offset(100)
        }
        scrollView.contentSize = CGSize(width: KScreenWidth, height: KScreenHeight+100)
        scrollView.showsVerticalScrollIndicator = false
        
        let bgView = UIView(frame: CGRectMake(0, 0, KScreenWidth, KScreenHeight))
        scrollView.addSubview(bgView)
        
        let topView = self.topView()
        bgView.addSubview(topView)
        topView.snp.makeConstraints { make in
            make.left.right.equalTo(0)
            make.top.equalToSuperview().offset(0)
            make.height.equalTo(254)
        }
        
        
        self.view.bringSubviewToFront(self.navtionBar!)
        self.setNavgationBarColorImg(color: .clear)
        var acView = topView
        if UserInfo.sharedInstance.isMemberShip == false {
            let centerView = self.centerView()
            bgView.addSubview(centerView)
            centerView.snp.makeConstraints { make in
                make.left.equalToSuperview().offset(20)
                make.right.equalToSuperview().offset(-20)
                make.top.equalTo(topView.snp.bottom).offset(20)
                make.height.equalTo(268)
            }
            acView = centerView
        }
        else {
            scrollView.contentSize = CGSize(width: KScreenWidth, height: KScreenHeight - 168)
        }
        
        
        let bottomView = self.bottomView()
        bgView.addSubview(bottomView)
        bottomView.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(20)
            make.right.equalToSuperview().offset(-20)
            make.top.equalTo(acView.snp.bottom).offset(20)
            make.bottom.equalToSuperview()
        }
        
//        scrollView.contentOffset = CGPoint(x: 0, y: 0)
//        scrollView.scrollRectToVisible(CGRect(x: 0, y: 0, width: KScreenWidth, height: scrollView.frame.size.height), animated: false)
    }
    
    
    
    
    @objc func dismissVC(sender:UIButton) {
        self.dismiss(animated: true)
        
    }
    
    //MARK: - 购买会员
    @objc func buyMembership(sender:UIButton) {
        SKUPay.sharedInstance.pay(productIdentifier: (self.selectedItem?.productIdentifier!)!)
    }
    
    //恢复购买
    @objc func rebuyMembership(sender:UIButton) {
        print("......click 恢复购买")
        SKUPay.sharedInstance.restoreProducts()
    }
    
    func topView() -> UIView {
        let bgView = UIView()
        let imgView = UIImageView()
        imgView.image = UIImage(named: NSLocalizedString("vip_topImg", comment: ""))
        bgView.addSubview(imgView)
        imgView.snp.makeConstraints { make in
            make.top.right.bottom.top.equalToSuperview()
        }
//        bgView.backgroundColor = .white
//        let imgView_1 = UIImageView(image: UIImage(named: "logo"))
//        bgView.addSubview(imgView_1)
//        imgView_1.snp.makeConstraints { make in
//            make.height.width.equalTo(30)
//        }
//        
//        let titleLabel = UILabel()
//        bgView.addSubview(titleLabel)
//        titleLabel.text = NSLocalizedString("VPcamera", comment: "")
//        titleLabel.snp.makeConstraints { make in
//            make.centerX.equalToSuperview()
//            make.top.equalToSuperview().offset(8)
//            make.centerY.equalTo(imgView_1.snp.centerY)
//            make.left.equalTo(imgView_1.snp.right).offset(4)
//        }
//        
//        let imgView_2 = UIImageView(image: UIImage(named: "logo"))
//        bgView.addSubview(imgView_2)
//        imgView_2.snp.makeConstraints { make in
//            make.centerY.equalTo(titleLabel.snp.centerY)
//            make.height.width.equalTo(30)
//            make.left.equalTo(titleLabel.snp.right).offset(4)
//        }
//        
//        let siView_1 = shipItemView("logo", NSLocalizedString("membershipdesc_1", comment: ""))
//        bgView.addSubview(siView_1)
//        let siView_2 = shipItemView("logo", NSLocalizedString("membershipdesc_2", comment: ""))
//        bgView.addSubview(siView_2)
//        let siView_3 = shipItemView("logo", NSLocalizedString("membershipdesc_3", comment: ""))
//        bgView.addSubview(siView_3)
//        let siView_4 = shipItemView("logo", NSLocalizedString("membershipdesc_4", comment: ""))
//        bgView.addSubview(siView_4)
//        
//        siView_1.snp.makeConstraints { make in
//            make.centerX.equalToSuperview()
//            make.top.equalTo(titleLabel.snp.bottom).offset(20)
//        }
//        siView_2.snp.makeConstraints { make in
//            make.left.equalTo(siView_1.snp.left)
//            make.top.equalTo(siView_1.snp.bottom).offset(4)
//        }
//        
//        siView_3.snp.makeConstraints { make in
//            make.left.equalTo(siView_2.snp.left)
//            make.top.equalTo(siView_2.snp.bottom).offset(4)
//        }
//        
//        siView_4.snp.makeConstraints { make in
//            make.left.equalTo(siView_3.snp.left)
//            make.top.equalTo(siView_3.snp.bottom).offset(4)
//        }
        
        return bgView
    }
    
    func shipItemView(_ imageName:String,_ desc:String) -> UIView {
        let bgView = UIView()
        let imgView = UIImageView()
        imgView.image = UIImage(named: imageName)
        bgView.addSubview(imgView)
        let descLabel = UILabel()
        descLabel.text = desc
        bgView.addSubview(descLabel)
        
        imgView.snp.makeConstraints { make in
            make.height.width.equalTo(20)
            make.left.equalToSuperview()
            make.top.bottom.equalToSuperview()
        }
        
        descLabel.snp.makeConstraints { make in
            make.right.equalToSuperview()
            make.top.bottom.equalToSuperview()
            make.left.equalTo(imgView.snp.right).offset(3)
        }
        return bgView
    }
    
    func centerView() -> UIView {
        let bgView = UIView()
        pcenterView = bgView
        bgView.backgroundColor = .clear
        pvView = MembershipProductView(frame: .zero)
        bgView.addSubview(pvView!)
        pvView?.snp.makeConstraints { make in
            make.left.right.top.bottom.equalToSuperview()
        }
        pvView?.selectCallback = {[weak self] item in
            print("选择....\(item)")
            self?.selectedItem = item
        }
        return bgView
    }
    
    //测试数据
    func configureData(){
        var arr:[MemberShipModelItem] = []
        let item1 = MemberShipModelItem(title: "$0.99/周", desc: "一周", rightDesc: "", isSelected: false, imageName_selected: NSLocalizedString("vip_product_week", comment: ""), imageName_unSelected: NSLocalizedString("vip_product_unselect_week", comment: ""), productIdentifier: MemberShipModelItem.getProductsIDs()[0])
        arr.append(item1)
        
        let item2 = MemberShipModelItem(title: "$1.99/月", desc: "一月", rightDesc: "90%的人选择", isSelected: true, imageName_selected: NSLocalizedString("vip_product_month", comment: ""), imageName_unSelected: NSLocalizedString("vip_product_unselect_month", comment: ""), productIdentifier: MemberShipModelItem.getProductsIDs()[1])
        arr.append(item2)
        
        let item3 = MemberShipModelItem(title: "$9.99/年", desc: "一年", rightDesc: "50%的人选择", isSelected: false, imageName_selected: NSLocalizedString("vip_product_year", comment: ""), imageName_unSelected: NSLocalizedString("vip_product_unselect_year", comment: ""), productIdentifier: MemberShipModelItem.getProductsIDs()[2])
        arr.append(item3)
        
//        for i in 0 ..< 3 {
//            let item = MemberShipModelItem()
//            item.title = "$9.9/周"
//            item.desc = "低至$0.14/天"
//            item.rightDesc = "90%人选择"
//            item.isSelected = false
//            item.imageName_selected = ""
//            item.imageName_unSelected = ""
//            arr.append(item)
//        }
        let item = arr[1]
        item.isSelected = true
        selectedItem = item
        pvView?.setData(arr,selectedItem:selectedItem)
    }
    
    func bottomView()->UIView {
        let bgView = UIView()
        bgView.backgroundColor = .clear
        
        let subscribeBtn = UIButton()
        psubscribeBtn = subscribeBtn
        bgView.addSubview(subscribeBtn)
        subscribeBtn.addTarget(self, action: #selector(subscribeAction(sender: )), for: .touchUpInside)
        subscribeBtn.titleLabel?.font = UIFont.boldSystemFont(ofSize: 16)
        subscribeBtn.setTitleColor(.white, for: .normal)
//        subscribeBtn.setTitle(UserInfo.sharedInstance.isMemberShip ?NSLocalizedString("Membership activated", comment: "") : NSLocalizedString("Free Trail 3 Days", comment: ""), for: .normal)
        if UserInfo.sharedInstance.isMemberShip {
            subscribeBtn.setBackgroundImage(UIImage(named: NSLocalizedString("vip_alreadybuybtn", comment: "")), for: .normal)
            subscribeBtn.isEnabled = false
        }
        else {
            subscribeBtn.setBackgroundImage(UIImage(named: NSLocalizedString("vip_buybtn", comment: "")), for: .normal)
            subscribeBtn.isEnabled = true
        }
        
        subscribeBtn.backgroundColor = UserInfo.sharedInstance.isMemberShip ? .lightGray : .darkText
        subscribeBtn.layer.masksToBounds = true
        subscribeBtn.layer.cornerRadius = 16
        subscribeBtn.isEnabled = !UserInfo.sharedInstance.isMemberShip
        subscribeBtn.snp.makeConstraints { make in
            make.top.equalToSuperview()
            make.centerX.equalToSuperview()
            make.height.equalTo(54)
            make.left.equalToSuperview().offset(4)
            make.right.equalToSuperview().offset(-4)
        }
        
        subscribeBtn.addTarget(self, action: #selector(buyMembership(sender: )), for: .touchUpInside)
        
        
        let btn_privateBtn = UIButton()
        bgView.addSubview(btn_privateBtn)
        btn_privateBtn.addTarget(self, action: #selector(goWeb(sender: )), for: .touchUpInside)
        btn_privateBtn.setTitle(NSLocalizedString("隐私政策", comment: ""), for: .normal)
        btn_privateBtn.titleLabel?.font = UIFont.systemFont(ofSize: 12)
        btn_privateBtn.setTitleColor(.white, for: .normal)
        btn_privateBtn.tag = 1
        btn_privateBtn.snp.makeConstraints { make in
            make.top.equalTo(subscribeBtn.snp.bottom).offset(16)
            
        }
        
        
        let line = UIView()
        bgView.addSubview(line)
        line.backgroundColor = .white
        line.snp.makeConstraints { make in
            make.width.equalTo(1)
            make.height.equalTo(10)
            make.centerX.equalToSuperview()
            make.centerY.equalTo(btn_privateBtn.snp.centerY)
            make.left.equalTo(btn_privateBtn.snp.right).offset(19)
        }
        
        let btn_userBtn = UIButton()
        bgView.addSubview(btn_userBtn)
        btn_userBtn.addTarget(self, action: #selector(goWeb(sender: )), for: .touchUpInside)
        btn_userBtn.setTitle(NSLocalizedString("用户协议", comment: ""), for: .normal)
        btn_userBtn.titleLabel?.font = UIFont.systemFont(ofSize: 12)
        btn_userBtn.setTitleColor(.white, for: .normal)
        btn_userBtn.tag = 2
        btn_userBtn.snp.makeConstraints { make in
            make.centerY.equalTo(btn_privateBtn.snp.centerY)
            make.left.equalTo(line.snp.right).offset(19)
        }
        
        
        
        let descLabel = UILabel()
        bgView.addSubview(descLabel)
        descLabel.numberOfLines = 0
        descLabel.textAlignment = .center
        descLabel.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(4)
            make.right.equalToSuperview().offset(-4)
            make.top.equalTo(btn_userBtn.snp.bottom).offset(12)
        }
        

        descLabel.textColor = UIColor(red: 0.804, green: 0.804, blue: 0.804, alpha: 1)
        descLabel.font = UIFont(name: "PingFangSC-Regular", size: 10)
        descLabel.lineBreakMode = .byWordWrapping
        let paragraphStyle = NSMutableParagraphStyle()
        paragraphStyle.lineHeightMultiple = 1.4
        paragraphStyle.alignment = .center
        descLabel.attributedText = NSMutableAttributedString(string: NSLocalizedString("subscribDesc", comment: ""), attributes: [NSAttributedString.Key.paragraphStyle: paragraphStyle])

        return bgView
    }
    
    //MARK: - 跳转web
   @objc func goWeb(sender:UIButton) {
        if sender.tag == 1 {//隐私政策
            let vc:CCWebController = CCWebController()
            vc.titleValue =  NSLocalizedString("隐私政策", comment: "")
            let sizeUrl = NSLocalizedString("siteUrl", comment: "隐私政策url")
            vc.urlString = sizeUrl
            self.navigationController?.pushViewController(vc, animated: true)
        }
        else if sender.tag == 2{//用户协议
            let vc:CCWebController = CCWebController()
            vc.titleValue =  NSLocalizedString("用户协议", comment: "")
            let sizeUrl = NSLocalizedString("userSiteUrl", comment: "用户协议url")
            vc.urlString = sizeUrl
            self.navigationController?.pushViewController(vc, animated: true)
            
        }
    }
    
    //MARK: - 订阅
    @objc func subscribeAction(sender:UIButton) {
        
    }
    
}
