//
//  MyListVC.swift
//
//  Created by Bilal on 19/05/2024.
//

import UIKit

class MyListVC: UIViewController{
    
    @IBOutlet weak var webSeriesCV: UICollectionView!
    @IBOutlet weak var webSeriesEnglishStack: UIStackView!
    
    @IBOutlet weak var webSeriesHindiCV: UICollectionView!
    @IBOutlet weak var webSeriesHindiStack: UIStackView!
    
    @IBOutlet weak var audioBooksCV: UICollectionView!
    @IBOutlet weak var audioBooksStack: UIStackView!
    
    @IBOutlet weak var karaokeCV: UICollectionView!
    @IBOutlet weak var karaokeStack: UIStackView!
    
    @IBOutlet weak var gamesCV: UICollectionView!
    @IBOutlet weak var gamesStack: UIStackView!
    
    @IBOutlet weak var noDataStack: UIStackView!
    
    @IBOutlet weak var scrollView: UIScrollView!
    
    @IBOutlet weak var gradientView: UIView!
    @IBOutlet weak var guestLoginView: UIView!
    @IBOutlet weak var guestLoginStack: UIStackView!
    
    @IBOutlet weak var webSeriesBtn: LocalisedElementsButton!
    @IBOutlet weak var webSeriesHindiBtn: LocalisedElementsButton!
    @IBOutlet weak var audioBooksBtn: LocalisedElementsButton!
    @IBOutlet weak var gamesBtn: LocalisedElementsButton!
    @IBOutlet weak var karaokeViewBtn: LocalisedElementsButton!
    
    @IBOutlet weak var shimmerStack: UIStackView!
    @IBOutlet var shimmerView: [ShimmerEffectView]!
    
    
    var vm = MyListVM()
    
    deinit{
        NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: K.NotificationCenterReloads.myListPush), object: nil)
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        vm.vc = self
        vm.initView()
        
        vm.refreshControl.attributedTitle = NSAttributedString(string: "Refreshing...",attributes: [.foregroundColor: UIColor.appColor(.TextDarkBlue)!])
        vm.refreshControl.tintColor = UIColor.appColor(.TextDarkBlue)!
        vm.refreshControl.addTarget(self, action: #selector(self.refresh(_:)), for: .valueChanged)
        scrollView.addSubview(vm.refreshControl)
    }
    
    @objc func refresh(_ sender: AnyObject) {
        Timer.scheduledTimer(withTimeInterval: 1, repeats: false) { [unowned self] _ in
            vm.getFavouriteListing()
        }
    }
    
    override func viewWillAppear(_ animated: Bool) {
        
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        
    }
    
    override func viewDidLayoutSubviews() {
        vm.addGradient()
    }
    
    override func viewDidAppear(_ animated: Bool) {
        K.GVar.topView = .myList
        
        if K.GVar.reloadMyList{
            K.GVar.reloadMyList = false
            K.GVar.myListSoftReload = false
            Utilities.startProgressHUD()
            vm.getFavouriteListing()
        }
        
        if K.GVar.myListSoftReload{
            vm.reloadCollections()
            vm.checkNil()
            K.GVar.myListSoftReload = false
        }
        
        if MyListDataTemp.shareInstance.isDatafetched == false{
            K.GVar.reloadMyListAudioBooks = false
            K.GVar.reloadKaraoke = false
            K.GVar.reloadGames = false
            K.GVar.reloadHindiWebseries = false
            K.GVar.reloadEnglishWebseries = false
            return
        }
        
        if K.GVar.reloadMyListAudioBooks{
            MyListDataTemp.shareInstance.updateAudioBooks() { [weak self] isDone in
                guard let self else{return}
                if isDone{
                    self.audioBooksCV.reloadData()
                    vm.checkNil()
                    vm.checkViewAll()
                }
            }
            K.GVar.reloadMyListAudioBooks = false
        }
        
        if K.GVar.reloadKaraoke{
            MyListDataTemp.shareInstance.updateKaraoke() { [weak self] isDone in
                guard let self else{return}
                if isDone{
                    self.karaokeCV.reloadData()
                    vm.checkNil()
                    vm.checkViewAll()
                }
            }
            K.GVar.reloadKaraoke = false
        }
        
        if K.GVar.reloadGames{
            MyListDataTemp.shareInstance.updateGames() { [weak self] isDone in
                guard let self else{return}
                if isDone{
                    self.gamesCV.reloadData()
                    vm.checkNil()
                    vm.checkViewAll()
                }
            }
            K.GVar.reloadGames = false
        }
        
        if K.GVar.reloadEnglishWebseries{
            MyListDataTemp.shareInstance.updateWebSeries(catID: 1){ [weak self] isDone in
                guard let self else{return}
                if isDone{
                    webSeriesCV.reloadData()
                    vm.checkNil()
                    vm.checkViewAll()
                }
            }
            K.GVar.reloadEnglishWebseries = false
        }
        
        if K.GVar.reloadHindiWebseries{
            MyListDataTemp.shareInstance.updateWebSeries(catID: 18) { [weak self] isDone in
                guard let self else{return}
                if isDone{
                    webSeriesHindiCV.reloadData()
                    vm.checkNil()
                    vm.checkViewAll()
                }
            }
            K.GVar.reloadHindiWebseries = false
        }
    
    }
    
    @IBAction func sideBarBtnTapped(_ sender: UIButton) {
        self.sideMenuController?.revealMenu()
    }
    
    @IBAction func backBtntapped(_ sender: UIButton) {
        self.tabBarController?.selectedIndex = 0
    }
    
    @IBAction func freeSignIn(_ sender: LocalisedElementsButton) {
        UIApplication.setRootView(LoginNavVC.instantiate(from: .AuthenticationSB))
    }
    
    @IBAction func viewAllBtnTapped(_ sender: UIButton) {
        PersistentStorage.shared.addOthersCount()
        switch sender{
        case webSeriesHindiBtn:
            let sb = UIStoryboard(name: K.StoryBoard.home, bundle: nil)
            let vcPush = sb.instantiateViewController(withIdentifier: K.StoryBoardID.Home.MyListViewAllVC) as! MyListViewAllVC
            vcPush.vm.postType = 1
            vcPush.vm.catID = "18"
            self.navigationController?.pushViewController(vcPush, animated: true)
        case webSeriesBtn:
            let sb = UIStoryboard(name: K.StoryBoard.home, bundle: nil)
            let vcPush = sb.instantiateViewController(withIdentifier: K.StoryBoardID.Home.MyListViewAllVC) as! MyListViewAllVC
            vcPush.vm.postType = 1
            vcPush.vm.catID = "1"
            self.navigationController?.pushViewController(vcPush, animated: true)
        case karaokeViewBtn:
            let sb = UIStoryboard(name: K.StoryBoard.home, bundle: nil)
            let vcPush = sb.instantiateViewController(withIdentifier: K.StoryBoardID.Home.MyListViewAllVC) as! MyListViewAllVC
            vcPush.vm.postType = 8
            self.navigationController?.pushViewController(vcPush, animated: true)
        case audioBooksBtn:
            let sb = UIStoryboard(name: K.StoryBoard.home, bundle: nil)
            let vcPush = sb.instantiateViewController(withIdentifier: K.StoryBoardID.Home.MyListViewAllVC) as! MyListViewAllVC
            vcPush.vm.postType = 7
            self.navigationController?.pushViewController(vcPush, animated: true)
        case gamesBtn:
            let sb = UIStoryboard(name: K.StoryBoard.home, bundle: nil)
            let vcPush = sb.instantiateViewController(withIdentifier: K.StoryBoardID.Home.MyListViewAllVC) as! MyListViewAllVC
            vcPush.vm.postType = 6
            self.navigationController?.pushViewController(vcPush, animated: true)
        default:
            break
        }
    }
}

// MARK: - CollectionView Delegate

extension MyListVC : CollectionViewSRC{
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        switch collectionView{
        case webSeriesCV:
            if MyListDataTemp.shareInstance.favListingData?.showData?.english?.count == 0 || MyListDataTemp.shareInstance.favListingData == nil{
                webSeriesEnglishStack.isHidden = true
            }else{
                webSeriesEnglishStack.isHidden = false
            }
            return MyListDataTemp.shareInstance.favListingData?.showData?.english?.count ?? 0
        case webSeriesHindiCV:
            if MyListDataTemp.shareInstance.favListingData?.showData?.hindi?.count == 0 || MyListDataTemp.shareInstance.favListingData == nil{
                webSeriesHindiStack.isHidden = true
            }else{
                webSeriesHindiStack.isHidden = false
            }
            return MyListDataTemp.shareInstance.favListingData?.showData?.hindi?.count ?? 0
        case audioBooksCV:
            if MyListDataTemp.shareInstance.favListingData?.audioData?.count == 0 || MyListDataTemp.shareInstance.favListingData == nil{
                audioBooksStack.isHidden = true
            }else{
                audioBooksStack.isHidden = false
            }
            return MyListDataTemp.shareInstance.favListingData?.audioData?.count ?? 0
        case karaokeCV:
            if MyListDataTemp.shareInstance.favListingData?.singKaraokeData?.count == 0 || MyListDataTemp.shareInstance.favListingData == nil{
                karaokeStack.isHidden = true
            }else{
                karaokeStack.isHidden = false
            }
            return MyListDataTemp.shareInstance.favListingData?.singKaraokeData?.count ?? 0
        case gamesCV:
            if MyListDataTemp.shareInstance.favListingData?.gameData?.count == 0 || MyListDataTemp.shareInstance.favListingData == nil{
                gamesStack.isHidden = true
            }else{
                gamesStack.isHidden = false
            }
            return MyListDataTemp.shareInstance.favListingData?.gameData?.count ?? 0
        default:
            return 0
        }
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: K.CellIdentifier.Home.favouriteCell, for: indexPath) as! FavouriteCell
        
        switch collectionView{
        case webSeriesCV:
            if let data = MyListDataTemp.shareInstance.favListingData?.showData?.english?[indexPath.row]{
                cell.setData(data: data)
            }
        case webSeriesHindiCV:
            if let data = MyListDataTemp.shareInstance.favListingData?.showData?.hindi?[indexPath.row]{
                cell.setData(data: data)
            }
        case audioBooksCV:
            if let data = MyListDataTemp.shareInstance.favListingData?.audioData?[indexPath.row]{
                cell.setAudioData(data: data)
            }
        case karaokeCV:
            if let data = MyListDataTemp.shareInstance.favListingData?.singKaraokeData?[indexPath.row]{
                cell.setKaraokeData(data: data)
            }
        case gamesCV:
            if let data = MyListDataTemp.shareInstance.favListingData?.gameData?[indexPath.row]{
                cell.setGameData(data: data)
            }
        default:
            break
//            if let data = MyListDataTemp.shareInstance.favListingData?.showData?[indexPath.row]{
//                cell.setData(data: data)
//            }
        }
        
//        cell.btnTapped = { [self] (type) -> Void in
//            updateFavLikes(cv: collectionView, type: type, index: indexPath.row)
//        }
        
        return cell
    }
    
    // Updates from Cell Clicks
//    func updateFavLikes(cv : UICollectionView , type : FavCellCLick, index : Int){
//        switch cv{
//        case webSeriesCV:
//            guard let data = MyListDataTemp.shareInstance.favListingData?.showData?[index] else{return}
//            switch type {
//            case .favourite:
//                guard let isFav = data.markAsFavourite ,let postID = data.id,let postType = data.contentMoreDetails?.first?.postType,let categoryID = data.categoryMasterID else{return}
//                if isFav{
//                    vm.removeFavourite(postID: postID, postType: postType, categoryID: categoryID, index: index) { [unowned self] isDone in
//                        if isDone{
//                            MyListDataTemp.shareInstance.favListingData?.showData?.remove(at: index)
//                            webSeriesCV.reloadData()
//                            vm.checkNil()
//                        }
//                    }
//                }
//            case .liked:
//                guard let isLiked = data.isLiked ,let postID = data.id,let postType = data.contentMoreDetails?.first?.postType else{return}
//                if isLiked{
//                    vm.unlikePost(postID: postID, postType: postType, index: index) { [unowned self] isDone in
//                        if isDone{
//                            MyListDataTemp.shareInstance.favListingData?.showData?[index].isLiked = false
//                            MyListDataTemp.shareInstance.favListingData?.showData?[index].likesCount! -= 1
//                            webSeriesCV.reloadItems(at: [IndexPath(row: index, section: 0)])
//
//                            for (i,element) in MyListDataTemp.shareInstance.webSeriesHindi.enumerated(){
//                                if element.id == MyListDataTemp.shareInstance.favListingData?.showData?[index].id{
//                                    MyListDataTemp.shareInstance.webSeriesHindi[i].isLiked = false
//                                    MyListDataTemp.shareInstance.webSeriesHindi[i].likesCount! -= 1
//                                    webSeriesHindiCV.reloadItems(at: [IndexPath(row: i, section: 0)])
//                                }
//                            }
//                        }
//                    }
//                }else{
//                    vm.likePost(postID: postID, postType: postType, index: index){ [unowned self] isDone in
//                        if isDone{
//                            MyListDataTemp.shareInstance.favListingData?.showData?[index].isLiked = true
//                            MyListDataTemp.shareInstance.favListingData?.showData?[index].likesCount! += 1
//                            webSeriesCV.reloadItems(at: [IndexPath(row: index, section: 0)])
//                            
//                            for (i,element) in MyListDataTemp.shareInstance.webSeriesHindi.enumerated(){
//                                if element.id == MyListDataTemp.shareInstance.favListingData?.showData?[index].id{
//                                    MyListDataTemp.shareInstance.webSeriesHindi[i].isLiked = true
//                                    MyListDataTemp.shareInstance.webSeriesHindi[i].likesCount! += 1
//                                    webSeriesHindiCV.reloadItems(at: [IndexPath(row: i, section: 0)])
//                                }
//                            }
//                        }
//                    }
//                }
//            }
//        case webSeriesHindiCV:
//            let data = MyListDataTemp.shareInstance.webSeriesHindi[index]
//            switch type {
//            case .favourite:
//                guard let isFav = data.markAsFavourite ,let postID = data.id,let postType = data.contentMoreDetails?.first?.postType,let categoryID = data.categoryMasterID else{return}
//                if isFav{
//                    vm.removeFavourite(postID: postID, postType: postType, categoryID: categoryID, index: index) { [unowned self] isDone in
//                        if isDone{
//                            MyListDataTemp.shareInstance.webSeriesHindi.remove(at: index)
//                            webSeriesHindiCV.reloadData()
//                            vm.checkNil()
//                        }
//                    }
//                }
//            case .liked: // Hindi Data
//                guard let isLiked = data.isLiked ,let postID = data.id,let postType = data.contentMoreDetails?.first?.postType else{return}
//                if isLiked{
//                    vm.unlikePost(postID: postID, postType: postType, index: index) { [unowned self] isDone in
//                        if isDone{
//                            MyListDataTemp.shareInstance.webSeriesHindi[index].isLiked = false
//                            MyListDataTemp.shareInstance.webSeriesHindi[index].likesCount! -= 1
//                            webSeriesHindiCV.reloadItems(at: [IndexPath(row: index, section: 0)])
//                            vm.checkNil()
//                            
//                            guard let showData = MyListDataTemp.shareInstance.favListingData?.showData else{return}
//                            
//                            for (i,element) in showData.enumerated(){
//                                if element.id == MyListDataTemp.shareInstance.webSeriesHindi[index].id{
//                                    MyListDataTemp.shareInstance.favListingData?.showData?[i].isLiked = false
//                                    MyListDataTemp.shareInstance.favListingData?.showData?[i].likesCount! -= 1
//                                    webSeriesCV.reloadItems(at: [IndexPath(row: i, section: 0)])
//                                }
//                            }
//                            
//                        }
//                    }
//                }else{
//                    vm.likePost(postID: postID, postType: postType, index: index){ [unowned self] isDone in
//                        if isDone{
//                            MyListDataTemp.shareInstance.webSeriesHindi[index].isLiked = true
//                            MyListDataTemp.shareInstance.webSeriesHindi[index].likesCount! += 1
//                            webSeriesHindiCV.reloadItems(at: [IndexPath(row: index, section: 0)])
//                            vm.checkNil()
//                            
//                            guard let showData = MyListDataTemp.shareInstance.favListingData?.showData else{return}
//                            
//                            for (i,element) in showData.enumerated(){
//                                if element.id == MyListDataTemp.shareInstance.webSeriesHindi[index].id{
//                                    MyListDataTemp.shareInstance.favListingData?.showData?[i].isLiked = true
//                                    MyListDataTemp.shareInstance.favListingData?.showData?[i].likesCount! += 1
//                                    webSeriesCV.reloadItems(at: [IndexPath(row: i, section: 0)])
//                                }
//                            }
//                        }
//                    }
//                }
//            }
//        case audioBooksCV:
//            guard let data = MyListDataTemp.shareInstance.favListingData?.audioData?[index] else{return}
//            switch type {
//            case .favourite:
//                guard let isFav = data.markAsFavourite ,let postID = data.id,let postType = data.contentMoreDetails?.first?.postType else{return}
//                if isFav{
//                    vm.removeFavourite(postID: postID, postType: postType, categoryID: "", index: index) { [unowned self] isDone in
//                        if isDone{
//                            MyListDataTemp.shareInstance.favListingData?.audioData?.remove(at: index)
//                            audioBooksCV.reloadData()
//                            vm.checkNil()
//                        }
//                    }
//                }
//            case .liked:
//                guard let isLiked = data.isLiked ,let postID = data.id,let postType = data.contentMoreDetails?.first?.postType else{return}
//                if isLiked{
//                    vm.unlikePost(postID: postID, postType: postType, index: index) { [unowned self] isDone in
//                        if isDone{
//                            MyListDataTemp.shareInstance.favListingData?.audioData?[index].isLiked = false
//                            MyListDataTemp.shareInstance.favListingData?.audioData?[index].likesCount! -= 1
//                            audioBooksCV.reloadItems(at: [IndexPath(row: index, section: 0)])
//                            vm.checkNil()
//                        }
//                    }
//                }else{
//                    vm.likePost(postID: postID, postType: postType, index: index){ [unowned self] isDone in
//                        if isDone{
//                            MyListDataTemp.shareInstance.favListingData?.audioData?[index].isLiked = true
//                            MyListDataTemp.shareInstance.favListingData?.audioData?[index].likesCount! += 1
//                            audioBooksCV.reloadItems(at: [IndexPath(row: index, section: 0)])
//                            vm.checkNil()
//                        }
//                    }
//                }
//            }
//        case karaokeCV:
//            guard let data = MyListDataTemp.shareInstance.favListingData?.singKaraokeData?[index] else{return}
//
//            switch type {
//            case .favourite:
//                guard let isFav = data.markAsFavourite ,let postID = data.id,let postType = data.contentMoreDetails?.first?.postType else{return}
//                if isFav{
//                    vm.removeFavourite(postID: postID, postType: postType, categoryID: "", index: index) { [unowned self] isDone in
//                        if isDone{
//                            MyListDataTemp.shareInstance.favListingData?.singKaraokeData?.remove(at: index)
//                            karaokeCV.reloadData()
//                            vm.checkNil()
//                        }
//                    }
//                }
//            case .liked:
//                guard let isLiked = data.isLiked ,let postID = data.id,let postType = data.contentMoreDetails?.first?.postType else{return}
//                if isLiked{
//                    vm.unlikePost(postID: postID, postType: postType, index: index) { [unowned self] isDone in
//                        if isDone{
//                            MyListDataTemp.shareInstance.favListingData?.singKaraokeData?[index].isLiked = false
//                            MyListDataTemp.shareInstance.favListingData?.singKaraokeData?[index].likesCount! -= 1
//                            karaokeCV.reloadItems(at: [IndexPath(row: index, section: 0)])
//                            vm.checkNil()
//                        }
//                    }
//                }else{
//                    vm.likePost(postID: postID, postType: postType, index: index){ [unowned self] isDone in
//                        if isDone{
//                            MyListDataTemp.shareInstance.favListingData?.singKaraokeData?[index].isLiked = true
//                            MyListDataTemp.shareInstance.favListingData?.singKaraokeData?[index].likesCount! += 1
//                            karaokeCV.reloadItems(at: [IndexPath(row: index, section: 0)])
//                            vm.checkNil()
//                        }
//                    }
//                }
//            }
//        case gamesCV:
//            guard let data = MyListDataTemp.shareInstance.favListingData?.gameData?[index] else{return}
//
//            switch type {
//            case .favourite:
//                guard let isFav = data.markAsFavourite ,let postID = data.id,let postType = data.contentMoreDetails?.first?.postType else{return}
//                if isFav{
//                    vm.removeFavourite(postID: postID, postType: postType, categoryID: "", index: index) { [unowned self] isDone in
//                        if isDone{
//                            MyListDataTemp.shareInstance.favListingData?.gameData?.remove(at: index)
//                            gamesCV.reloadData()
//                            vm.checkNil()
//                        }
//                    }
//                }
//            case .liked:
//                guard let isLiked = data.isLiked ,let postID = data.id,let postType = data.contentMoreDetails?.first?.postType else{return}
//                if isLiked{
//                    vm.unlikePost(postID: postID, postType: postType, index: index) { [unowned self] isDone in
//                        if isDone{
//                            MyListDataTemp.shareInstance.favListingData?.gameData?[index].isLiked = false
//                            MyListDataTemp.shareInstance.favListingData?.gameData?[index].likesCount! -= 1
//                            gamesCV.reloadItems(at: [IndexPath(row: index, section: 0)])
//                            vm.checkNil()
//                        }
//                    }
//                }else{
//                    vm.likePost(postID: postID, postType: postType, index: index){ [unowned self] isDone in
//                        if isDone{
//                            MyListDataTemp.shareInstance.favListingData?.gameData?[index].isLiked = true
//                            MyListDataTemp.shareInstance.favListingData?.gameData?[index].likesCount! += 1
//                            gamesCV.reloadItems(at: [IndexPath(row: index, section: 0)])
//                            vm.checkNil()
//                        }
//                    }
//                }
//            }
//        default:
//            break
//        }
//    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        switch collectionView{
        case webSeriesCV:
            // 18- hindi , 1- english
            
            guard let showData = MyListDataTemp.shareInstance.favListingData?.showData?.english?[indexPath.row] else{return}
//            PersistentStorage.shared.addWebSeries(catID: 1, postID: showData.id ?? 0)

            if let showID = showData.id{
                PersistentStorage.shared.addWebSeries(catID: 1, postID: showID, postType: .series)
            }
            
            let showDataModified = WebSeriesShowListDM.ShowDatum(id: showData.id, title: showData.title, description: showData.description, thumbnailPath: showData.thumbnailPath, showType: showData.showType, totalSeasons: showData.totalSeasons, totalEpisodes: showData.totalEpisodes, categoryMasterID: nil, ageRangeMasterID: nil, genderMasterID: showData.genderMasterID, contentMoreDetails: showData.contentMoreDetails, seasonData: nil, categoryData: nil, ageRangeData: nil, genderData: nil, markAsFavourite: showData.markAsFavourite, isLiked: showData.isLiked, likedCategoryIDS: nil, favouriteCategoryIDS: ValueWrapper.stringValue(showData.bookmarkCategoryIDS ?? "1") , likesCount: showData.likesCount, viewsCount: showData.viewsCount, bookmarkCount: showData.bookmarkCount)
            
            let sb = UIStoryboard(name: K.StoryBoard.webSeries, bundle: nil)
            let vcPush = sb.instantiateViewController(withIdentifier: K.StoryBoardID.WebSeries.webSeriesSeasonVC) as! WebSeriesSeasonVC
            vcPush.vm.showData = showDataModified
            vcPush.vm.indexSelected = indexPath.row
//            vcPush.likeFavDelegate = self
            vcPush.vm.categoryID = 1
            self.navigationController?.pushViewController(vcPush, animated: true)
            self.vm.selectedCollection = .webSeriesCV
        case webSeriesHindiCV:
            guard let showData = MyListDataTemp.shareInstance.favListingData?.showData?.hindi?[indexPath.row] else{return}
            if let showID = showData.id{
                PersistentStorage.shared.addWebSeries(catID: 18, postID: showID, postType: .series)
            }
            
            let showDataModified = WebSeriesShowListDM.ShowDatum(id: showData.id, title: showData.title, description: showData.description, thumbnailPath: showData.thumbnailPath, showType: showData.showType, totalSeasons: showData.totalSeasons, totalEpisodes: showData.totalEpisodes, categoryMasterID: nil, ageRangeMasterID: nil, genderMasterID: showData.genderMasterID, contentMoreDetails: showData.contentMoreDetails, seasonData: nil, categoryData: nil, ageRangeData: nil, genderData: nil, markAsFavourite: showData.markAsFavourite, isLiked: showData.isLiked, likedCategoryIDS: nil, favouriteCategoryIDS: ValueWrapper.stringValue(showData.bookmarkCategoryIDS ?? "1") , likesCount: showData.likesCount, viewsCount: showData.viewsCount, bookmarkCount: showData.bookmarkCount)
            
            let sb = UIStoryboard(name: K.StoryBoard.webSeries, bundle: nil)
            let vcPush = sb.instantiateViewController(withIdentifier: K.StoryBoardID.WebSeries.webSeriesSeasonVC) as! WebSeriesSeasonVC
            vcPush.vm.showData = showDataModified
            vcPush.vm.indexSelected = indexPath.row
//            vcPush.likeFavDelegate = self
            vcPush.vm.categoryID = 18
            self.navigationController?.pushViewController(vcPush, animated: true)
            self.vm.selectedCollection = .webSeriesHindiCV
        case audioBooksCV:
            guard let audioData = MyListDataTemp.shareInstance.favListingData?.audioData?[indexPath.row] else{return}
            if let postID = audioData.id{
                PersistentStorage.shared.addAudioCount(postID: postID)
            }
            let sb = UIStoryboard(name: K.StoryBoard.audioBooks, bundle: nil)
            let vcPush = sb.instantiateViewController(withIdentifier: K.StoryBoardID.AudioBooks.audioBookDetailsVC) as! AudioBookDetailsVC
            vcPush.modalPresentationStyle = .overCurrentContext
            vcPush.modalTransitionStyle = .crossDissolve
            vcPush.audioData = audioData
//            vcPush.delegate = self
            vcPush.btnTapped = { (type) -> Void in
                MyListDataTemp.shareInstance.updateAudioBooks() { [weak self] isDone in
                    guard let self else{return}
                    if isDone{
                        self.audioBooksCV.reloadData()
                        vm.checkNil()
                        vm.checkViewAll()
                    }
                }
            }
            self.vm.selectedCollection = .audioBooksCV
            self.present(vcPush, animated: true)
        case karaokeCV:
            guard let karaokeData = MyListDataTemp.shareInstance.favListingData?.singKaraokeData?[indexPath.row] else{return}
            if let postID = karaokeData.id{
                PersistentStorage.shared.addKaraokeCount(postID: postID)
            }
            let sb = UIStoryboard(name: K.StoryBoard.Karaoke, bundle: nil)
            let vcPush = sb.instantiateViewController(withIdentifier: K.StoryBoardID.Karaoke.karaokeDetailsVC) as! KaraokeDetailsVC
            vcPush.modalPresentationStyle = .fullScreen
            vcPush.modalTransitionStyle = .crossDissolve
            vcPush.karaokeData = karaokeData
            vcPush.karaokeIndex = indexPath.row
//            vcPush.delegate = self
            vcPush.btnTapped = { (type) -> Void in
                MyListDataTemp.shareInstance.updateKaraoke() { [weak self] isDone in
                    guard let self else{return}
                    if isDone{
                        self.karaokeCV.reloadData()
                        vm.checkNil()
                        vm.checkViewAll()
                    }
                }
            }
            self.vm.selectedCollection = .karaokeCV
            self.present(vcPush, animated: true)
        case gamesCV:
            guard let gameData = MyListDataTemp.shareInstance.favListingData?.gameData?[indexPath.row] else{return}
            if let postID = gameData.id{
                PersistentStorage.shared.addGamesCount(postID: postID)
            }
            let sb = UIStoryboard(name: K.StoryBoard.Games, bundle: nil)
            let vcPush = sb.instantiateViewController(withIdentifier: K.StoryBoardID.Games.gamesDetailVC) as! GamesDetailVC
            vcPush.modalPresentationStyle = .overCurrentContext
            vcPush.modalTransitionStyle = .crossDissolve
            vcPush.gameData = gameData
            vcPush.gameIndex = indexPath.row
//            vcPush.delegate = self
            vcPush.btnTapped = { (type) -> Void in
                MyListDataTemp.shareInstance.updateGames() { [weak self] isDone in
                    guard let self else{return}
                    if isDone{
                        self.gamesCV.reloadData()
                        vm.checkNil()
                        vm.checkViewAll()
                    }
                }
            }
            self.vm.selectedCollection = .gamesCV
            self.present(vcPush, animated: true)
        default:
            print(indexPath.row)
        }
        
    }
}
// MARK: - Delegate for reload

//extension MyListVC : ReloadSeriesFavLike{
    //Updates from details
//    func updateRows(index: Int, type: FavCellCLick, isFav: Bool?, isLike: Bool?, id: Int?) {
//        switch vm.selectedCollection {
//        case .webSeriesCV:
//            
//            if let isLike{
//                switch isLike{
//                case true:
//                    MyListDataTemp.shareInstance.favListingData?.showData?.english?[index].isLiked = true
//                    MyListDataTemp.shareInstance.favListingData?.showData?.english?[index].likesCount! += 1
//                    webSeriesCV.reloadItems(at: [IndexPath(row: index, section: 0)])
//                    
//                    for (i,element) in MyListDataTemp.shareInstance.webSeriesHindi.enumerated(){
//                        if element.id == MyListDataTemp.shareInstance.favListingData?.showData?[index].id{
//                            MyListDataTemp.shareInstance.webSeriesHindi[i].isLiked = true
//                            MyListDataTemp.shareInstance.webSeriesHindi[i].likesCount! += 1
//                            webSeriesHindiCV.reloadItems(at: [IndexPath(row: i, section: 0)])
//                        }
//                    }
//                case false:
//                    MyListDataTemp.shareInstance.favListingData?.showData?[index].isLiked = false
//                    MyListDataTemp.shareInstance.favListingData?.showData?[index].likesCount! -= 1
//                    webSeriesCV.reloadItems(at: [IndexPath(row: index, section: 0)])
//
//                    for (i,element) in MyListDataTemp.shareInstance.webSeriesHindi.enumerated(){
//                        if element.id == MyListDataTemp.shareInstance.favListingData?.showData?[index].id{
//                            MyListDataTemp.shareInstance.webSeriesHindi[i].isLiked = false
//                            MyListDataTemp.shareInstance.webSeriesHindi[i].likesCount! -= 1
//                            webSeriesHindiCV.reloadItems(at: [IndexPath(row: i, section: 0)])
//                        }
//                    }
//                }
//            }
//        case .webSeriesHindiCV:
//            if let isLike{
//                switch isLike{
//                case true:
//                    MyListDataTemp.shareInstance.webSeriesHindi[index].isLiked = true
//                    MyListDataTemp.shareInstance.webSeriesHindi[index].likesCount! += 1
//                    webSeriesHindiCV.reloadItems(at: [IndexPath(row: index, section: 0)])
//                    vm.checkNil()
//                    
//                    guard let showData = MyListDataTemp.shareInstance.favListingData?.showData else{return}
//                    
//                    for (i,element) in showData.enumerated(){
//                        if element.id == MyListDataTemp.shareInstance.webSeriesHindi[index].id{
//                            MyListDataTemp.shareInstance.favListingData?.showData?[i].isLiked = true
//                            MyListDataTemp.shareInstance.favListingData?.showData?[i].likesCount! += 1
//                            webSeriesCV.reloadItems(at: [IndexPath(row: i, section: 0)])
//                        }
//                    }
//                case false:
//                    MyListDataTemp.shareInstance.webSeriesHindi[index].isLiked = false
//                    MyListDataTemp.shareInstance.webSeriesHindi[index].likesCount! -= 1
//                    webSeriesHindiCV.reloadItems(at: [IndexPath(row: index, section: 0)])
//                    vm.checkNil()
//                    
//                    guard let showData = MyListDataTemp.shareInstance.favListingData?.showData else{return}
//                    
//                    for (i,element) in showData.enumerated(){
//                        if element.id == MyListDataTemp.shareInstance.webSeriesHindi[index].id{
//                            MyListDataTemp.shareInstance.favListingData?.showData?[i].isLiked = false
//                            MyListDataTemp.shareInstance.favListingData?.showData?[i].likesCount! -= 1
//                            webSeriesCV.reloadItems(at: [IndexPath(row: i, section: 0)])
//                        }
//                    }
//                    
//                }
//            }
//        case .gamesCV:
//            if let isLike{
//                switch isLike{
//                case true:
//                    MyListDataTemp.shareInstance.favListingData?.gameData?[index].isLiked = true
//                    MyListDataTemp.shareInstance.favListingData?.gameData?[index].likesCount! += 1
//                    gamesCV.reloadItems(at: [IndexPath(row: index, section: 0)])
//                case false:
//                    MyListDataTemp.shareInstance.favListingData?.gameData?[index].isLiked = false
//                    MyListDataTemp.shareInstance.favListingData?.gameData?[index].likesCount! -= 1
//                    gamesCV.reloadItems(at: [IndexPath(row: index, section: 0)])                    
//                }
//            }
//            
//            if let isFav{
//                switch isFav{
//                case true:
//                    self.gamesCV.reloadData()
//                    self.vm.checkNil()
//                case false:
//                    self.gamesCV.reloadData()
//                    self.vm.checkNil()
//                }
//            }
//            
//        case .karaokeCV:
//            if let isLike{
//                switch isLike{
//                case true:
//                    karaokeCV.reloadItems(at: [IndexPath(row: index, section: 0)])
//                case false:
//                    karaokeCV.reloadItems(at: [IndexPath(row: index, section: 0)])
//                }
//            }
//            
//            if let isFav{
//                switch isFav{
//                case true:
//                    self.karaokeCV.reloadData()
//                    self.vm.checkNil()
//                case false:
//                    self.karaokeCV.reloadData()
//                    self.vm.checkNil()
//                }
//            }
//        case .audioBooksCV:
//            if let isLike{
//                switch isLike{
//                case true:
//                    audioBooksCV.reloadItems(at: [IndexPath(row: index, section: 0)])
//                case false:
//                    audioBooksCV.reloadItems(at: [IndexPath(row: index, section: 0)])
//                }
//            }
//            
//            if let isFav{
//                switch isFav{
//                case true:
//                    self.audioBooksCV.reloadData()
//                    self.vm.checkNil()
//                case false:
//                    self.audioBooksCV.reloadData()
//                    self.vm.checkNil()
//                }
//            }
//        default:
//            break
//            
//        }
//    }
//}

// MARK: - Collection Flow Layout

extension MyListVC : UICollectionViewDelegateFlowLayout{

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
        return 5
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
        let inset: CGFloat = 10
        return UIEdgeInsets(top: 0, left: inset, bottom: 0, right: inset)
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return 0 // Space between cells
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        let widthPerItem = collectionView.frame.width - 30 // Adjust to your desired width
        return CGSize(width: widthPerItem, height: 230)
    }
}
