//
//  HomeMeetGuideController.swift

//
//  Created by Leon on 2025/9/26.
//

import Lottie
import UIKit

class HomeMeetGuideController: BaseMaskPopDialogController {
    var upGuideContainer: UIView!
    var upLottie: LottieAnimationView!
    var upDescLabel: UILabel!

    var rightLikeLottie: LottieAnimationView!
    var leftLikeLottie: LottieAnimationView!
    
    weak var playingLottie: LottieAnimationView?

    var stepIndex = 0

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        setupViews()
        setupDatas()
        setupEvents()
    }

    private func setupDatas() {
        stepIndex = 0
        refreshViews()
    }

    private func setupEvents() {
        let tapGesture = UITapGestureRecognizer(target: self, action: #selector(handleTapGesture(_:)))
        tapGesture.numberOfTapsRequired = 1
        tapGesture.numberOfTouchesRequired = 1
        block.addGestureRecognizer(tapGesture)
    }

    @objc private func handleTapGesture(_ tap: UITapGestureRecognizer) {
        // dlog("点击停止")
        playingLottie?.stop()
    }

    private func refreshViews() {
        if stepIndex == 0 {
            upGuideContainer.isHidden = false
            upLottie.isHidden = false
            playingLottie = upLottie
            upLottie.play {[weak self] completed in
                self?.continueGuide()
            }
        } else if stepIndex == 1 {
            rightLikeLottie.isHidden = false
            playingLottie = rightLikeLottie
            rightLikeLottie.play{[weak self] completed in
                self?.continueGuide()
            }
        } else if stepIndex == 2 {
            leftLikeLottie.isHidden = false
            playingLottie = leftLikeLottie
            leftLikeLottie.play{[weak self] completed in
                self?.continueGuide()
            }
        }
    }
    
    private func continueGuide(){
        upGuideContainer.isHidden = true
        rightLikeLottie.isHidden = true
        leftLikeLottie.isHidden = true

        stepIndex += 1

        if stepIndex > 2 { // 解锁
            dismiss()
            AppCache.cache(key: CacheKey.meetGuideSeen.rawValue, value: true)
            return
        }
        
        refreshViews()
    }
    
    // MARK: - UI

    private func setupViews() {
        navigationView.isHidden = true
        closeButton.isHidden = true
        block.backgroundColor = .clear
        block.snp.remakeConstraints { make in
            make.edges.equalToSuperview()
        }

        do {
            upGuideContainer = {
                let v = UIView()
                block.addSubview(v)
                v.snp.makeConstraints { make in
                    make.edges.equalToSuperview()
                }
                return v
            }()

            upLottie = {
                let animation = LottieAnimation.named("meet_up_swipe_see_more")
                let v = LottieAnimationView(animation: animation)

                v.contentMode = .scaleAspectFit
                v.loopMode = .loop
                v.backgroundBehavior = .pauseAndRestore
                v.loopMode = .playOnce
                v.backgroundColor = .clear
                upGuideContainer.addSubview(v)
                let size = CGSize(width: 136, height: 136)
                v.size = size
                v.snp.makeConstraints { make in
                    make.centerX.equalToSuperview()
                    make.centerY.equalToSuperview()
                    make.size.equalTo(size)
                }
                v.isHidden = true
                return v
            }()

            upDescLabel = {
                let v = UILabel()
                v.font = .t.ttm
                v.textColor = .text
                upGuideContainer.addSubview(v)
                v.snp.makeConstraints { make in
                    make.centerX.equalToSuperview()
                    make.top.equalTo(upLottie.snp.bottom).offset(16)
                }
                return v
            }()

            upDescLabel.text = "Swipe to see more"
        }

        rightLikeLottie = {
            let animation = LottieAnimation.named("meet_right_swipe_like")
            let v = LottieAnimationView(animation: animation)

            v.contentMode = .scaleAspectFit
            v.loopMode = .loop
            v.backgroundBehavior = .pauseAndRestore
            v.loopMode = .playOnce
            v.backgroundColor = .clear
            block.addSubview(v)
            let size = CGSize(width: 240, height: 240)
            v.size = size
            v.snp.makeConstraints { make in
                make.centerX.equalToSuperview()
                make.bottom.equalToSuperview().offset(-120)
                make.size.equalTo(size)
            }
            v.isHidden = true
            return v
        }()

        leftLikeLottie = {
            let animation = LottieAnimation.named("meet_left_swipe_dislike")
            let v = LottieAnimationView(animation: animation)

            v.contentMode = .scaleAspectFit
            v.loopMode = .loop
            v.backgroundBehavior = .pauseAndRestore
            v.loopMode = .playOnce
            v.backgroundColor = .clear
            block.addSubview(v)
            let size = CGSize(width: 240, height: 240)
            v.size = size
            v.snp.makeConstraints { make in
                make.centerX.equalToSuperview()
                make.bottom.equalToSuperview().offset(-120)
                make.size.equalTo(size)
            }
            v.isHidden = true
            return v
        }()
    }
}
