//
//  TSBootPageVC.swift
//  Girly
//
//  Created by 100Years on 2025/1/14.
//

class TSBootPageVC: TSBaseVC {
    
    var onComplete:(()->Void)
    
    init(onComplete: @escaping () -> Void) {
        self.onComplete = onComplete
        super.init()
    }
    
    @MainActor required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    var index:Int = 0
    let titleStrings = ["Create Ringtones with AI".localized,"Thousands of Ringtones".localized,"Calling Theme Surprise You".localized]
    let imageStrings = ["bootPage_0","bootPage_1","bootPage_2"]
    
    
    lazy var titleLabel: UILabel = {
        let titleLabel = UILabel.createLabel(text: titleStrings.safeString(At: index),font: .font(size: 20,weight: .semibold),textColor: "#FBDAFF".uiColor,textAlignment:.center,numberOfLines: 0)
        return titleLabel
    }()
    
    lazy var continueBtn: UIButton = {
        let continueBtn = kCreateNormalSubmitBtn(title: "Continue".localized, frame: CGRectMake(0, 0, 343, 50)) { [weak self]  in
            guard let self = self else { return }
            clickContinue()
        }
        continueBtn.titleLabel?.font = .font(size: 18,weight: .medium)
        
        let imageView = UIImageView.createImageView(imageName: "launch_rightArrow")
        continueBtn.addSubview(imageView)
        imageView.snp.makeConstraints { make in
            make.width.height.equalTo(24)
            make.centerY.equalToSuperview()
            make.trailing.equalTo(-24)
        }
        
        return continueBtn
    }()
    
//    lazy var scrollView: UIScrollView = {
//        let scrollView = UIScrollView()
//        scrollView.isScrollEnabled = false
//        scrollView.frame = self.view.bounds
//
//        let imageView0 = UIImageView.createImageView(imageName: "bootPage_0",contentMode: .scaleAspectFill)
//        imageView0.frame = CGRectMake(0, 0, k_ScreenWidth, k_ScreenHeight)
//        scrollView.addSubview(imageView0)
//        
//        let imageView1 = UIImageView.createImageView(imageName: "bootPage_1",contentMode: .scaleAspectFill)
//        imageView1.frame = CGRectMake(k_ScreenWidth, 0, k_ScreenWidth, k_ScreenHeight)
//        scrollView.addSubview(imageView1)
//        
//        let imageView2 = UIImageView.createImageView(imageName: "bootPage_2",contentMode: .scaleAspectFill)
//        imageView2.frame = CGRectMake(k_ScreenWidth*2, 0, k_ScreenWidth, k_ScreenHeight)
//        scrollView.addSubview(imageView2)
//        
//        let btnH = k_ScreenHeight/5
//        let btnTop = btnH*4
//        
//        let button0 = UIButton(frame: CGRectMake(0, btnTop, k_ScreenWidth, btnH))
//        button0.tag = 1
//        button0.addTarget(self, action: #selector(clickBtn(_:)), for: .touchUpInside)
//        scrollView.addSubview(button0)
//        
//        let button1 = UIButton(frame: CGRectMake(k_ScreenWidth, btnTop, k_ScreenWidth, btnH))
//        button1.tag = 2
//        button1.addTarget(self, action: #selector(clickBtn(_:)), for: .touchUpInside)
//        scrollView.addSubview(button1)
//        
//        let button2 = UIButton(frame: CGRectMake(k_ScreenWidth*2, btnTop, k_ScreenWidth, btnH))
//        button2.tag = 3
//        button2.addTarget(self, action: #selector(clickBtn(_:)), for: .touchUpInside)
//        scrollView.addSubview(button2)
//        
//        scrollView.addSubview(titleLabel)
//        scrollView.addSubview(continueBtn)
//        continueBtn.snp.makeConstraints { make in
//            make.width.equalTo(continueBtn.width)
//            make.height.equalTo(continueBtn.height)
//            make.bottom.equalTo(-61)
//            make.centerY.equalToSuperview()
//        }
//        
//        titleLabel.snp.makeConstraints { make in
//            make.leading.equalTo(16)
//            make.trailing.equalTo(-16)
//            make.bottom.equalTo(continueBtn.snp.top).offset(-36)
//        }
//        return scrollView
//    }()
//    
//    override func createView() {
//        setNavBarViewHidden(true)
//        self.view.backgroundColor = .black
//        self.view.addSubview(scrollView)
//        titleLabel.text = titleStrings.safeString(At: index)
//    }
    
    
//    @objc func clickBtn(_ btn:UIButton){
//        if btn.tag <= 2{
//            scrollView.contentOffset = CGPointMake(k_ScreenWidth*CGFloat(btn.tag), 0)
//        }else{
//            onComplete()
//            self.navBarClickLeftAction()
//        }
//    }
    
    @objc func clickBtn(_ btn:UIButton){
        if btn.tag <= 2{
            scrollView.contentOffset = CGPointMake(k_ScreenWidth*CGFloat(btn.tag), 0)
        }else{
            let vc = TSPurchaseVC()
            vc.closePageBlock = { [weak self]  in
                guard let self = self else { return }
                onComplete()
            }
            self.navigationController?.pushViewController(vc, animated: true)
        }
    }
    
    
    lazy var scrollView: UIScrollView = {
        let scrollView = UIScrollView()
        scrollView.isScrollEnabled = false
        scrollView.frame = self.view.bounds

        var idx:Int = 0
        for string in imageStrings {
            let imageView = UIImageView.createImageView(imageName: string,contentMode: .scaleAspectFill)
            imageView.frame = CGRectMake(k_ScreenWidth * CGFloat(idx), 0, k_ScreenWidth, k_ScreenHeight)
            scrollView.addSubview(imageView)
            idx+=1
        }

        return scrollView
    }()
    
    override func createView() {
        setNavBarViewHidden(true)
        self.view.backgroundColor = .black
        self.view.addSubview(scrollView)
        
        view.addSubview(titleLabel)
        view.addSubview(continueBtn)
        continueBtn.snp.makeConstraints { make in
            make.width.equalTo(continueBtn.width)
            make.height.equalTo(continueBtn.height)
            make.bottom.equalTo(-61)
            make.centerX.equalToSuperview()
        }
        
        titleLabel.snp.makeConstraints { make in
            make.leading.equalTo(16)
            make.trailing.equalTo(-16)
            make.bottom.equalTo(continueBtn.snp.top).offset(-36)
        }
        

    }
    
    func clickContinue(){
        index+=1
        titleLabel.text = titleStrings.safeString(At: index)
        if index <= 2{
            scrollView.contentOffset = CGPointMake(k_ScreenWidth*CGFloat(index), 0)
        }else{
            let vc = TSPurchaseVC()
            vc.closePageBlock = { [weak self]  in
                guard let self = self else { return }
                onComplete()
            }
            self.navigationController?.pushViewController(vc, animated: true)
        }
        
    }
    
    func indexAdd(){
        index+=1
        if index > 2 {
            index = 2
        }
    }
}
