//
//  QSLGuideController.swift
//  QuickSearchLocation
//
//  Created by Destiny on 2024/12/9.
//

import UIKit
import GKCycleScrollView

class QSLGuideController: QSLBaseController {
    
//    var currentPage
    
    lazy var cycleScrollView: GKCycleScrollView = {
      
        let cycleScrollView = GKCycleScrollView()
        cycleScrollView.dataSource = self
        cycleScrollView.delegate = self
        cycleScrollView.isAutoScroll = false
        cycleScrollView.isInfiniteLoop = false
        cycleScrollView.isChangeAlpha = false
        cycleScrollView.pageControl = pageControl
        return cycleScrollView
    }()
    
    lazy var pageControl: GKPageControl = {
      
        let pageControl = GKPageControl()
        pageControl.style = .rectangle
        pageControl.dotHeight = 4.rpx
        pageControl.dotWidth = 20.rpx
        pageControl.dotMargin = 4.rpx
        pageControl.pageIndicatorTintColor = .hexStringColor(hexString: "#EEEEEE")
        pageControl.currentPageIndicatorTintColor = QSLColor.themeMainColor
        return pageControl
    }()
    
    lazy var nextButton: UIButton = {
       
        let btn = UIButton()
        btn.gradientBackgroundColor(color1: .hexStringColor(hexString: "#15CBA1"), color2: .hexStringColor(hexString: "#1FE0BA"), width: 280.rpx, height: 44.rpx, direction: .horizontal)
        btn.title("下一步")
        btn.mediumFont(16)
        btn.textColor(.white)
        btn.addRadius(radius: 22.rpx)
        btn.addTarget(self, action: #selector(nextBtnAction), for: .touchUpInside)
        return btn
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.view.addSubview(cycleScrollView)
        cycleScrollView.snp.makeConstraints { make in
            make.left.equalTo(40.rpx)
            make.right.equalTo(-40.rpx)
            make.height.equalTo(368.rpx)
            make.top.equalTo(QSLConst.qsl_kStatusBarFrameH + 72.rpx)
        }
        
        self.view.addSubview(pageControl)
        pageControl.snp.makeConstraints { make in
            make.size.equalTo(CGSize(width: 68.rpx, height: 4.rpx))
            make.centerX.equalToSuperview()
            make.top.equalTo(cycleScrollView.snp.bottom).offset(17.rpx)
        }
        
        self.view.addSubview(nextButton)
        nextButton.snp.makeConstraints { make in
            make.size.equalTo(CGSize(width: 280.rpx, height: 44.rpx))
            make.centerX.equalToSuperview()
            make.top.equalTo(pageControl.snp.bottom).offset(50.rpx)
        }
        
        cycleScrollView.reloadData()
    }
    
    // 点击下一步按钮
    @objc func nextBtnAction() {
        
        switch cycleScrollView.currentSelectIndex {
        case 0:
            gravityInstance?.track(QSLGravityConst.guide_first_click)
            break
        case 1:
            gravityInstance?.track(QSLGravityConst.guide_second_click)
            break
        case 2:
            gravityInstance?.track(QSLGravityConst.guide_third_click)
            break
        default:
            break
        }
        
        if cycleScrollView.currentSelectIndex == 2 {
            if let sceneDelegate = UIApplication.shared.connectedScenes.first?.delegate as? SceneDelegate {
                sceneDelegate.window?.rootViewController = CustomTabBarController()
                sceneDelegate.window?.makeKeyAndVisible()
            }
        } else {
            cycleScrollView.scrollToCell(at: cycleScrollView.currentSelectIndex + 1, animated: true)
        }
    }
}

extension QSLGuideController: GKCycleScrollViewDataSource, GKCycleScrollViewDelegate {
    
    func numberOfCells(in cycleScrollView: GKCycleScrollView!) -> Int {
        return 3
    }
    
    func cycleScrollView(_ cycleScrollView: GKCycleScrollView!, cellForViewAt index: Int) -> GKCycleScrollViewCell! {
        if let cell = cycleScrollView.dequeueReusableCell() {
            cell.imageView.image = UIImage(named: "guide_pic_\(index + 1)")
            return cell
        }
        
        let cell = GKCycleScrollViewCell()
        cell.imageView.image = UIImage(named: "guide_pic_\(index + 1)")
        return cell
    }
    
    func cycleScrollView(_ cycleScrollView: GKCycleScrollView!, didScrollCellTo index: Int) {
        switch index {
        case 0:
            gravityInstance?.track(QSLGravityConst.guide_first_show)
            break
        case 1:
            gravityInstance?.track(QSLGravityConst.guide_second_show)
            break
        case 2:
            gravityInstance?.track(QSLGravityConst.guide_third_show)
            break
        default:
            break
        }
    }
}
