CNLiveIntegralAlertTool.swift 14.4 KB
//
//  CNLiveIntegralAlertTool.swift
//  metaCode
//
//  Created by zx on 2024/1/11.
//  积分支付弹窗

import Foundation
import QMUIKit
import SnapKit

///积分支付状态类型
enum CNLiveIntegralPayStatus {
    ///积分充足
    case integralEnough
    ///积分不足
    case integralNotEnough
    ///支付中
    case paying
    ///支付成功
    case paySuccess
    ///支付失败
    case payFailed
    
}

class CNLiveIntegralAlertTool:NSObject{
    static let shared = CNLiveIntegralAlertTool()
    
    var homePageNeedReloadActionBlock:homePageNeedReloadBlockActionBlock?

    ///当前积分支付状态
    var currentIntegralPayStatus: CNLiveIntegralPayStatus = .integralEnough

    let delegate = UIApplication.shared.delegate as! AppDelegate
    /// 专辑名称
    var title:String = ""
    
    /// 专辑积分值
    var integralCount:String = "0"
    /// 支付的商品id
    var productId:String = ""
    /// 用户积分值
    var point:Int = 0
    /// 用户积分model
    var checkInModel:MinePointCheckInModel?

    lazy var bgBtnView: UIButton = {
        let bgBtnView = UIButton(frame: self.delegate.window!.bounds)
        bgBtnView.backgroundColor = HexColor("#000000")
        bgBtnView.alpha = 0.6
        bgBtnView.addTarget(self, action: #selector(bgBtnViewAction), for: .touchUpInside)
        return bgBtnView
    }()

    lazy var bottomBgView :UIView = {
        let bottomBgView = UIView(frame: CGRect(x: 0, y: KSreenHeight-267, width: KSreenWidth, height: 267))
        bottomBgView.backgroundColor = HexColor("#FFFFFF")
        bottomBgView.layer.cornerRadius = 9
        bottomBgView.layer.maskedCorners = [.layerMinXMinYCorner, .layerMaxXMinYCorner]
        bottomBgView.tag = 10000
        return bottomBgView
    }()
    
    ///专辑标题
    lazy var albumTitleLabel: UILabel = {
        let titleLabel = UILabel.init(frame: CGRectMake(15, 15, KSreenWidth-15-60, 20))
        titleLabel.textColor = HexColor("#333333")
        titleLabel.textAlignment = .left
        titleLabel.font = Font_16
        titleLabel.text = "剧集名称45集"
        return titleLabel
    }()
    
    lazy var closeBtn: UIButton = {
        let  closeBtn = UIButton(type: .custom)
        closeBtn.frame = CGRect(x: self.albumTitleLabel.right, y: 0, width: 50, height: 50)
        closeBtn.setImage(UIImage(named: "point_alert_close"), for: .normal)
        closeBtn.addTarget(self, action: #selector(closeBtnAction), for: .touchUpInside)
        return closeBtn
    }()

    lazy var bottomLineView :UIView = {
        let bottomLineView = UIView(frame: CGRect(x: 0, y: self.closeBtn.bottom+2, width: KSreenWidth, height: 1))
        bottomLineView.backgroundColor = HexColor("#E4E4E4")
        return bottomLineView
    }()
    
    ///积分数量
    lazy var integralCountLabel: UILabel = {
        let titleLabel = UILabel.init(frame: CGRectMake(0, self.bottomLineView.bottom+30, 100, 66))
        titleLabel.textColor = HexColor("#333333")
        titleLabel.textAlignment = .left
        titleLabel.font = UIFont.boldSystemFont(ofSize: 55)
        titleLabel.text = "8000"
        return titleLabel
    }()
    ///余额XXX积分
    lazy var balanceIntegralLabel: UILabel = {
        let titleLabel = UILabel.init(frame: CGRectMake(0, 0, 100, 20))
        titleLabel.textColor = HexColor("#696969")
        titleLabel.textAlignment = .left
        titleLabel.font = Font_14
        return titleLabel
    }()
    
    lazy var payBtn: UIButton = {
        let  payBtn = UIButton(type: .custom)
        payBtn.frame = CGRect(x: 15, y: self.bottomBgView.height-15-44, width: KSreenWidth-30, height: 44)
        payBtn.setTitleColor(HexColor("#FFFFFF"), for: .normal)
        // fillCode
        let bgLayer1 = CAGradientLayer()
        bgLayer1.colors = [UIColor(red: 0.4, green: 0.77, blue: 1, alpha: 1).cgColor, UIColor(red: 0.38, green: 0.6, blue: 1, alpha: 1).cgColor]
        bgLayer1.locations = [0, 1]
        bgLayer1.frame = payBtn.bounds
        bgLayer1.startPoint = CGPoint(x: 0, y: 0.5)
        bgLayer1.endPoint = CGPoint(x: 0.5, y: 0.5)
        payBtn.layer.addSublayer(bgLayer1)
        payBtn.addTarget(self, action: #selector(payBtnAction), for: .touchUpInside)
        return payBtn
    }()
    
    ///支付中img
    lazy var payingImgView :UIImageView = {
        let payingImgView = UIImageView(frame: CGRect(x: (KSreenWidth-45)/2.0, y: self.bottomLineView.bottom+65, width: 45, height: 43))
        payingImgView.image = UIImage(named: "jifen_paying")
        return payingImgView
    }()
    ///支付成功img
    lazy var paySuccessImgView :UIImageView = {
        let paySuccessImgView = UIImageView(frame: CGRect(x: (KSreenWidth-78)/2.0, y: 60, width: 78, height: 78))
        paySuccessImgView.image = UIImage(named: "jifen_paySuccess")
        return paySuccessImgView
    }()

    ///支付中
    lazy var payingLabel: UILabel = {
        let titleLabel = UILabel.init(frame: CGRectMake(0, self.payingImgView.bottom+15, KSreenWidth-40, 20))
        titleLabel.centerX = self.payingImgView.centerX
        titleLabel.textColor = HexColor("#333333")
        titleLabel.textAlignment = .center
        titleLabel.font = Font_14
        return titleLabel
    }()
    
    // MARK: - show
    ///弹出积分购买弹窗
    func showIntegralAlert(title:String,integralCount:String,productId:String){
        self.title = title
        self.integralCount = integralCount
        self.productId = productId
        
        delegate.window?.addSubview(self.bgBtnView)
        delegate.window?.addSubview(self.bottomBgView)

        //专辑所需积分值
        let intIntegralCount = Int(integralCount)!
        //用户积分值
        self.getCheckIn { result, status in
            let checkInModel = result as! MinePointCheckInModel
            self.checkInModel = checkInModel
            let pointStr = checkInModel.integral ?? "0"
            let intPoint = Int(pointStr)!
            self.point = intPoint
            if intPoint > intIntegralCount{
                //可以支付积分
                self.currentIntegralPayStatus = .integralEnough
                
            }else{
                //积分不足
                self.currentIntegralPayStatus = .integralNotEnough
            }
            self.initUI()

        }
    }
    
    //隐藏积分购买弹窗
    func hiddenIntegralAlert(){
        delegate.window?.subviews.forEach({ view in
            if view.tag == 10000{
                UIView.animate(withDuration: 0.25) {
                    view.removeFromSuperview()
                    self.bgBtnView.removeFromSuperview()
                }
            }
        })
    }
    
    func initUI(){
        self.bottomBgView.subviews.forEach { view in
            view.removeFromSuperview()
        }

        if self.currentIntegralPayStatus == .integralEnough{
            //积分充足
            self.bottomBgView.addSubview(self.albumTitleLabel)
            self.albumTitleLabel.text = self.title
            self.bottomBgView.addSubview(self.closeBtn)
            self.bottomBgView.addSubview(self.bottomLineView)
            self.bottomBgView.addSubview(self.integralCountLabel)
            let integralCountStr = "\(self.integralCount)积分"
            let priceAttri = NSMutableAttributedString.init(string: integralCountStr)
            priceAttri.addAttribute(.font, value: regular_adapt_font(pointSize: 55), range: NSMakeRange(0, integralCountStr.count-2))
            priceAttri.addAttribute(.foregroundColor, value: HexColor("#333333")!, range: NSMakeRange(0, integralCountStr.count-2))
            priceAttri.addAttribute(.font, value: regular_adapt_font(pointSize: 16), range: NSMakeRange(integralCountStr.count-2, 2))
            priceAttri.addAttribute(.foregroundColor, value: HexColor("#333333")!, range: NSMakeRange(integralCountStr.count-2, 2))
            self.integralCountLabel.attributedText = priceAttri

            self.bottomBgView.addSubview(self.balanceIntegralLabel)
            
            self.balanceIntegralLabel.text = String(format: "余额%d积分", self.point)
            
            self.bottomBgView.addSubview(self.payBtn)
            self.payBtn.setTitle("确认支付", for: .normal)
            
            self.integralCountLabel.snp.makeConstraints { make in
                make.centerX.equalTo(self.bottomBgView.snp.centerX)
                make.top.equalTo(self.bottomLineView.snp.bottom).offset(30)
                make.width.lessThanOrEqualToSuperview().offset(-10)
            }
            self.balanceIntegralLabel.snp.makeConstraints { make in
                make.centerX.equalTo(self.bottomBgView.snp.centerX)
                make.top.equalTo(self.integralCountLabel.snp.bottom).offset(5)
                make.width.lessThanOrEqualToSuperview()
            }
            
        }else if self.currentIntegralPayStatus == .integralNotEnough{
            //积分不足
            self.bottomBgView.addSubview(self.albumTitleLabel)
            self.albumTitleLabel.text = self.title
            self.bottomBgView.addSubview(self.closeBtn)
            self.bottomBgView.addSubview(self.bottomLineView)
            self.bottomBgView.addSubview(self.integralCountLabel)
            let integralCountStr = "\(self.integralCount)积分"
            let priceAttri = NSMutableAttributedString.init(string: integralCountStr)
            priceAttri.addAttribute(.font, value: regular_adapt_font(pointSize: 55), range: NSMakeRange(0, integralCountStr.count-2))
            priceAttri.addAttribute(.foregroundColor, value: HexColor("#333333")!, range: NSMakeRange(0, integralCountStr.count-2))
            priceAttri.addAttribute(.font, value: regular_adapt_font(pointSize: 16), range: NSMakeRange(integralCountStr.count-2, 2))
            priceAttri.addAttribute(.foregroundColor, value: HexColor("#333333")!, range: NSMakeRange(integralCountStr.count-2, 2))
            self.integralCountLabel.attributedText = priceAttri

            self.bottomBgView.addSubview(self.balanceIntegralLabel)
            self.balanceIntegralLabel.text = String(format: "余额%d积分   积分不足", self.point)

            self.bottomBgView.addSubview(self.payBtn)
            self.payBtn.setTitle("去赚积分", for: .normal)

            self.integralCountLabel.snp.makeConstraints { make in
                make.centerX.equalTo(self.bottomBgView.snp.centerX)
                make.top.equalTo(self.bottomLineView.snp.bottom).offset(30)
                make.width.lessThanOrEqualToSuperview().offset(-10)
            }
            self.balanceIntegralLabel.snp.makeConstraints { make in
                make.centerX.equalTo(self.bottomBgView.snp.centerX)
                make.top.equalTo(self.integralCountLabel.snp.bottom).offset(5)
                make.width.lessThanOrEqualToSuperview()
            }
        }else if self.currentIntegralPayStatus == .paying{
            //支付中
            self.bottomBgView.addSubview(self.albumTitleLabel)
            self.albumTitleLabel.text = self.title
            self.bottomBgView.addSubview(self.closeBtn)
            self.bottomBgView.addSubview(self.bottomLineView)
            self.bottomBgView.addSubview(self.payingImgView)
            self.payingImgView = UIImageView(frame: CGRect(x: (KSreenWidth-45)/2.0, y: self.bottomLineView.bottom+65, width: 45, height: 43))
            self.payingImgView.image = UIImage(named: "jifen_paying")
            
            self.bottomBgView.addSubview(self.payingLabel)
            self.payingLabel.text = "支付中..."
            
        }else if self.currentIntegralPayStatus == .paySuccess{
            //支付成功
            self.bottomBgView.addSubview(self.paySuccessImgView)
            self.paySuccessImgView = UIImageView(frame: CGRect(x: (KSreenWidth-78)/2.0, y: 60, width: 78, height: 78))
            self.paySuccessImgView.image = UIImage(named: "jifen_paySuccess")

            self.bottomBgView.addSubview(self.payingLabel)
            self.payingLabel.text = "支付成功\(self.integralCount)积分"
            
            self.bottomBgView.addSubview(self.payBtn)
            self.payBtn.setTitle("完成", for: .normal)
            
        }else if self.currentIntegralPayStatus == .payFailed{
            //支付失败
        }else{
            //其他
        }
    }
    
    //获取用户积分
    func getCheckIn(resultBlock: @escaping downSlideResultBlock){
        CNLiveMineViewModel.requestWithCheckIn { result, respStatue in
            if respStatue == .success {
                let checkModel = result as! MinePointCheckInModel
                resultBlock(checkModel, .success)
            } else {
                resultBlock(NSNull(), .failed)
            }
        }
    }
    
    // MARK: - BtnAction
    @objc func bgBtnViewAction(){
        self.hiddenIntegralAlert()
    }
    @objc func closeBtnAction(){
        self.hiddenIntegralAlert()
    }
    @objc func payBtnAction(){
        if self.currentIntegralPayStatus == .integralEnough{
            self.postOrderVideoByPoint()
        }else if self.currentIntegralPayStatus == .integralNotEnough{
            self.hiddenIntegralAlert()
            //跳转任务中心
            navigationViewController().pushViewController(CNLiveMineQuestViewController(checkModel: self.checkInModel!), animated: pushAnimated)

        }else if self.currentIntegralPayStatus == .paying{
            
        }else if self.currentIntegralPayStatus == .paySuccess{
            self.hiddenIntegralAlert()
        }else if self.currentIntegralPayStatus == .payFailed{
            self.currentIntegralPayStatus = .integralEnough
            self.initUI()
        }else{
            
        }
    }
    // MARK: - 积分付费接口
    func postOrderVideoByPoint(){
        self.currentIntegralPayStatus = .paying
        self.initUI()
        //付费接口
        CNLiveHomePageViewModel.orderVideoByPoint(prdId: self.productId) { result, status in
            if status == .success{
                //购买成功
                self.currentIntegralPayStatus = .paySuccess
                self.initUI()
                //刷新短剧首页数据
                self.homePageNeedReloadActionBlock!()
            }else{
                //购买失败
                if self.point > Int(self.integralCount)!{
                    //可以支付积分
                    self.currentIntegralPayStatus = .integralEnough
                }else{
                    //积分不足
                    self.currentIntegralPayStatus = .integralNotEnough
                }
                self.initUI()
                let errMsg = result as! String
                showMessage(message: errMsg)
            }
        }
        
    }

    
}