//
//  FeedbackView.swift
//  SwiftProject
//
//  Created by aaa on 2024/4/12.
//

import Foundation
import UIKit
import StoreKit
import Firebase
import FirebaseRemoteConfig
class FeedbackView: UIView {
    var scenterBgView:UIView?
    var alphaBgView:UIView?
    var praiseCallback:(()->Void?)? = nil
    var negativeCallback:(()->Void?)? = nil
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        alphaBgView = UIView()
        self.addSubview(alphaBgView!)
        alphaBgView!.backgroundColor = UIColor(r: 10, g: 10, b: 10, a: 0.2)
        alphaBgView?.alpha = 0
        alphaBgView?.snp.makeConstraints({ make in
            make.top.left.right.bottom.equalToSuperview()
        })
        
        let tgr = UITapGestureRecognizer(target: self, action: #selector(hiddenSelf))
        self.addGestureRecognizer(tgr)
        
        let centerBgView = UIView()
        scenterBgView = centerBgView
        self.addSubview(centerBgView)
        centerBgView.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.centerY.equalToSuperview().offset(KScreenHeight+200)
            make.width.equalTo(282)
            make.height.equalTo(336)
        }
        
        let centerBg_ImageView = UIImageView()
        centerBgView.addSubview(centerBg_ImageView)
        centerBg_ImageView.image = UIImage(named: "feedback_centerbg")
        centerBg_ImageView.contentMode = .scaleAspectFill
        
        centerBg_ImageView.snp.makeConstraints { make in
            make.top.right.bottom.left.equalToSuperview()
        }
        
        let desLabel = UILabel()
        centerBgView.addSubview(desLabel)
        desLabel.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(21)
            make.right.equalToSuperview().offset(-21)
        }
        
        
        desLabel.textColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.5)
        desLabel.font = UIFont(name: "PingFangSC-Medium", size: 12)
        desLabel.numberOfLines = 0
        desLabel.lineBreakMode = .byWordWrapping
        desLabel.textAlignment = .center
        let paragraphStyle = NSMutableParagraphStyle()
        paragraphStyle.lineHeightMultiple = 1
        paragraphStyle.alignment = .center
        desLabel.attributedText = NSMutableAttributedString(string: NSLocalizedString("v1.5_feedbackdesc", comment: ""), attributes: [NSAttributedString.Key.paragraphStyle: paragraphStyle])
        
        
        
        
        
        let praiseBtn = UIButton()
        centerBgView.addSubview(praiseBtn)
        praiseBtn.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(26)
            make.right.equalToSuperview().offset(-26)
            make.height.equalTo(38)
            make.top.equalTo(desLabel.snp.bottom).offset(13)
        }
        praiseBtn.backgroundColor = UIColor(r: 254, g: 50, b: 101, a: 1)
        praiseBtn.addTarget(self, action: #selector(praiseAction(sender: )), for: .touchUpInside)
        praiseBtn.layer.cornerRadius = 8
        praiseBtn.layer.masksToBounds = true
        praiseBtn.setTitle(NSLocalizedString("v1.5_praise", comment: ""), for: .normal)
        praiseBtn.titleLabel?.font = UIFont(name: "PingFangSC-Heavy", size: 16)
        praiseBtn.setTitleColor(.white, for: .normal)
        let negativeBtn = UIButton()
        centerBgView.addSubview(negativeBtn)
        negativeBtn.addTarget(self, action: #selector(negativeAction(sender: )), for: .touchUpInside)
        negativeBtn.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(26)
            make.right.equalToSuperview().offset(-26)
            make.height.equalTo(20)
            make.top.equalTo(praiseBtn.snp.bottom).offset(16)
            make.bottom.equalToSuperview().offset(-16)
        }
        negativeBtn.setTitle(NSLocalizedString("v1.5_negative", comment: ""), for: .normal)
        negativeBtn.titleLabel?.font = UIFont(name: "PingFangSC-Medium", size: 16)
        negativeBtn.setTitleColor(UIColor(red: 0, green: 0, blue: 0, alpha: 0.6), for: .normal)
        
        let closeBtn = UIButton()
        self.addSubview(closeBtn)
        closeBtn.setImage(UIImage(named: "feedback_close"), for: .normal)
        closeBtn.addTarget(self, action: #selector(closeAction(sender: )), for: .touchUpInside)
        closeBtn.snp.makeConstraints { make in
            make.width.height.equalTo(32)
            make.centerX.equalToSuperview()
            make.top.equalTo(centerBgView.snp.bottom).offset(26)
        }
    }
    
    @objc func closeAction(sender:UIButton) {
        hiddenSelf()
    }
    
    @objc func praiseAction(sender:UIButton) {
        hiddenSelf()
        let config = FirebaseRemoteConfig.RemoteConfig.remoteConfig()
        let menuParams = config.configValue(forKey: "menuParams")
        guard let dicInfo = menuParams.jsonValue as? [String:Any] else {
            return
        }
        guard let isOnline = dicInfo["isOnline"] as? Bool else {
            return
        }
        
        if isOnline == false {
            return
        }
        
        guard let praiseCallback else {
            return
        }
        praiseCallback()
        guard let kw = KWindow else {
            return
        }
        SKStoreReviewController.requestReview(in: kw.windowScene!)
    }
    
    @objc func negativeAction(sender:UIButton) {
        hiddenSelf()
        guard let negativeCallback else {
            return
        }
        negativeCallback()
    }
    
    func show(){
        self.isHidden = false
        UIView.animate(withDuration: 0.25) {[weak self] in
            self?.scenterBgView!.snp.updateConstraints({ make in
                make.centerY.equalToSuperview().offset(0)
            })
            self?.alphaBgView?.alpha = 1
            self?.layoutIfNeeded()
        } completion: {finish in
        }
    }
    
    class func show(praiseCallback:@escaping ()->Void,negativeCallback:@escaping ()->Void) {
        let hasShowFeedback = UserDefaults.standard.bool(forKey: "khasShowFeedbackKey")
        
        if hasShowFeedback {
            return
        }
        
        let fbView = FeedbackView(frame: .zero)
        fbView.praiseCallback = praiseCallback
        fbView.negativeCallback = negativeCallback
        
        fbView.isHidden = true
        KWindow?.addSubview(fbView)
        fbView.snp.makeConstraints { make in
            make.left.top.right.bottom.equalToSuperview()
        }
        KWindow?.layoutIfNeeded()
        fbView.show()
        UserDefaults.standard.set(true, forKey: "khasShowFeedbackKey")
        UserDefaults.standard.synchronize()
    }
    
    @objc func hiddenSelf() {
        UIView.animate(withDuration: 0.25) {[weak self] in
            self?.scenterBgView!.snp.updateConstraints({ make in
                make.centerY.equalToSuperview().offset(KScreenHeight+200)
            })
            self?.alphaBgView?.alpha = 0
            self?.layoutIfNeeded()
        } completion: {[weak self] finish in
            self?.removeFromSuperview()
        }

    }
    
    
}
