//
//  SelectAvatarVC.swift
//  WOKA
//
//  Created by MacBook Pro on 02/05/24.
//

import UIKit

class SelectAvatarVC: UIViewController {

    @IBOutlet weak var collectionView: UICollectionView!
    @IBOutlet weak var nextBtn: LocalisedElementsButton!
    
    var vm = SelectAvatarVM()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        vm.vc = self
        vm.initView()
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        navigationController?.setNavigationBarHidden(false, animated: animated)
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        navigationController?.setNavigationBarHidden(true, animated: animated)
        
        if self.isMovingFromParent {
            // Back button was pressed
            PersistentStorage.shared.addOthersCount()
        }
    }
    
    @IBAction func nextBtnTapped(_ sender: LocalisedElementsButton) {
        PersistentStorage.shared.addOthersCount()
        // Check for avatar.
        if AuthFunc.shareInstance.regData.avtar == nil || AuthFunc.shareInstance.regData.avtar == ""{
            let sb = UIStoryboard(name: K.StoryBoard.customAlerts, bundle: nil)
            let vcPush = sb.instantiateViewController(withIdentifier: K.StoryBoardID.CustomAlerts.alertCustomVC) as! AlertCustomVC
            
            vcPush.contentLabel = K.ConstantString.avatar.localized(loc: AuthFunc.shareInstance.languageSelected.rawValue)
            vcPush.mainTitleText = K.ConstantString.error.localized(loc: AuthFunc.shareInstance.languageSelected.rawValue)
            //                vcPush.onDoneBlock = { isDone in }
            vcPush.modalPresentationStyle = .overCurrentContext
            vcPush.modalTransitionStyle = .crossDissolve
            self.present(vcPush, animated: true)
            return
        }
        vm.registerUser()
    }
}

// MARK: - CollectionView Delegate

extension SelectAvatarVC : UICollectionViewDelegate , UICollectionViewDataSource{
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return vm.avatarData.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: K.CellIdentifier.Authentication.selectAvatarCell, for: indexPath) as! SelectAvatarCell
        cell.setData(data: vm.avatarData[indexPath.row])
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        PersistentStorage.shared.addOthersCount()
        AuthFunc.shareInstance.regData.avtar = vm.avatarData[indexPath.row].avatarName
        collectionView.reloadData()
    }
}

// MARK: - Collection Flow Layout

extension SelectAvatarVC : UICollectionViewDelegateFlowLayout{
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return 10
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        let noOfItems = self.collectionView.frame.width
        let size = ((noOfItems / 3) - 10)
        return CGSize(width: size, height: size)
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
        return UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
    }
}
