//
//  LWLandingVC.swift


import UIKit
import SwiftUI

struct Launch {
    var img: String?
    var title: String?
    var subtitle: String?
    
    init(img: String?, title: String?, subtitle: String?) {
        self.img = img
        self.title = title
        self.subtitle = subtitle
    }
}

class LWLandingVC: BaseViewController {
    
    //MARK:- Outlet
    @IBOutlet weak var viewGetSatrtedBtn: LWBtnView!
    @IBOutlet weak var btnGetStarted: LWButton!
    @IBOutlet weak var btnGetStartedPlay: UIButton!
    @IBOutlet weak var cvView: UICollectionView!
    @IBOutlet weak var lblTitle: UILabel!
    @IBOutlet weak var lblSubTitle: UILabel!
    @IBOutlet weak var viewBtnHeight: NSLayoutConstraint!
    @IBOutlet weak var playBtnHeight: NSLayoutConstraint!
    
    //MARK:- Variable
    var arrLunch = [Launch]()
    var lastVisibleIndex = 0
    var tTime: Timer!
    let userdefaultManager = UserDefaultManager.shared

    //MARK:- Life Cycle
    override func viewDidLoad() {
        super.viewDidLoad()
        tTime = Timer.scheduledTimer(timeInterval: 2, target: self, selector: #selector(changePage), userInfo: nil, repeats: true)
        initWithData()
        setNavigationBar()
        
        if DeviceType.ISPAD {
            viewGetSatrtedBtn.layer.cornerRadius = viewBtnHeight.constant / 2
            btnGetStartedPlay.layer.cornerRadius = playBtnHeight.constant / 2
            btnGetStarted.titleLabel?.font = UIFont(name: "SF Pro Display Bold", size: 46)
        }
    }
    override func viewWillAppear(_ animated: Bool) {
        
    }
    
    //MARK:- Init Function
    
    @objc func changePage() {
        UIView.animate(withDuration: 4.0) {
            if self.cvView.contentOffset.x == 0{
                self.lblTitle.text = FIRRemoteManagement.shared.introTitle2
                self.lblTitle.font = UIFont(name: "SF Pro Display Bold", size: DeviceType.ISPAD ? (FIRRemoteManagement.shared.introTitle2FontSize + 3 ) : FIRRemoteManagement.shared.introTitle2FontSize)
                self.lblTitle.textColor = UIColor.hexStringToUIColor(hex: FIRRemoteManagement.shared.introTitle2FontColor)
                
                self.lblSubTitle.text = FIRRemoteManagement.shared.introsubTitle2
                self.lblSubTitle.font = UIFont(name: "SF Pro Display Regular", size:DeviceType.ISPAD ? (FIRRemoteManagement.shared.introTitle2FontSize + 3 ) : FIRRemoteManagement.shared.introsubTitle2FontSize)
                self.lblSubTitle.textColor = UIColor.hexStringToUIColor(hex: FIRRemoteManagement.shared.introsubTitle2FontColor)

                self.cvView.scrollRectToVisible(CGRect(x: UIScreen.main.bounds.width, y: self.cvView.contentOffset.y, width: self.cvView.frame.width, height: self.cvView.frame.height), animated: true)
            }
            else{
                self.lblTitle.text = FIRRemoteManagement.shared.introTitle1
                self.lblTitle.font = UIFont(name: "SF Pro Display Bold", size: DeviceType.ISPAD ? (FIRRemoteManagement.shared.introTitle1FontSize + 3 ) : FIRRemoteManagement.shared.introTitle1FontSize)
                self.lblTitle.textColor = UIColor.hexStringToUIColor(hex: FIRRemoteManagement.shared.introTitle1FontColor)
                
                self.lblSubTitle.text = FIRRemoteManagement.shared.introsubTitle1
                self.lblSubTitle.font = UIFont(name: "SF Pro Display Regular", size: DeviceType.ISPAD ? (FIRRemoteManagement.shared.introTitle1FontSize + 3 ) : FIRRemoteManagement.shared.introsubTitle1FontSize)
                self.lblSubTitle.textColor = UIColor.hexStringToUIColor(hex: FIRRemoteManagement.shared.introsubTitle1FontColor)
                
                self.cvView.scrollRectToVisible(CGRect(x: 0, y: self.cvView.contentOffset.y, width: self.cvView.frame.width, height: self.cvView.frame.height), animated: true)
            }
            
        }
    }
    
    func initWithData(){
        self.lblTitle.text = FIRRemoteManagement.shared.introTitle1
        self.lblTitle.font = UIFont(name: "SF Pro Display Bold", size: DeviceType.ISPAD ? (FIRRemoteManagement.shared.introTitle1FontSize + 3 ) : FIRRemoteManagement.shared.introTitle1FontSize)
        self.lblTitle.textColor = UIColor.hexStringToUIColor(hex: FIRRemoteManagement.shared.introTitle1FontColor)
        
        
        self.lblSubTitle.text = FIRRemoteManagement.shared.introsubTitle1
        self.lblSubTitle.font = UIFont(name: "SF Pro Display Regular", size: DeviceType.ISPAD ? (FIRRemoteManagement.shared.introTitle1FontSize + 3 ) : FIRRemoteManagement.shared.introsubTitle1FontSize)
        self.lblSubTitle.textColor = UIColor.hexStringToUIColor(hex: FIRRemoteManagement.shared.introsubTitle1FontColor)
        
        arrLunch.append(Launch.init(img: "ic_launch1", title: FIRRemoteManagement.shared.introTitle1, subtitle: FIRRemoteManagement.shared.introsubTitle1))
        arrLunch.append(Launch.init(img: "ic_launch2", title: FIRRemoteManagement.shared.introTitle2, subtitle: FIRRemoteManagement.shared.introsubTitle2))
        cvView.reloadData()
    }
    func setNavigationBar(){
        
    }
    
    //MARK:- Action
    @IBAction func btnGetStartedTapped(_ sender: UIButton){
        
        if self.userdefaultManager.loginUserData.isEmpty{
            ScreenManagerShared.moveToLoginVC()
        }
        else{
            ScreenManagerShared.moveToHomeVC()
        }
        
    }
    @IBAction func btnGetStartedPlayTapped(_ sender: UIButton){
        ScreenManagerShared.moveToLoginVC()
    }
}

//MARK:- UICollectionViewDelegateDataSource
extension LWLandingVC: UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return arrLunch.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: LWLandingCCell.identifier, for: indexPath) as! LWLandingCCell
        let obj = arrLunch[indexPath.item]
//        cell.lblTitle.text = obj.title ?? ""
//        cell.lblSubTitle.text = obj.subtitle ?? ""
        cell.imgBackground.image = UIImage.init(named: obj.img ?? "")
        return cell
    }
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize.init(width: self.cvView.frame.width, height: self.cvView.frame.height)
    }
    func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
        lastVisibleIndex = indexPath.item
    }
}
