//
//  WebSeriesVC.swift
//  WOKA
//
//  Created by MacBook Pro on 18/06/24.
//

import UIKit
import GoogleMobileAds

protocol ReloadSeriesFavLike{
    func updateRows(index : Int, type : FavCellCLick, isFav : Bool? , isLike : Bool?, id : Int?)
}

class WebSeriesVC: UIViewController {

    @IBOutlet weak var headerView: ShimmerEffectView!
    @IBOutlet weak var headerViewLabel: UILabel!
    @IBOutlet weak var headerViewLabelHeight: NSLayoutConstraint!
    @IBOutlet weak var headerViewImage: UIImageView!
    @IBOutlet weak var headerBtn: LocalisedElementsButton!
    @IBOutlet weak var masilaTrailerView: ShimmerEffectView!
    
    @IBOutlet weak var videoLanguageView: ShimmerEffectView!
    
    @IBOutlet weak var topLabel: UILabel!
    @IBOutlet weak var scrollView: UIScrollView!
    @IBOutlet weak var headerViewTopConstraint: NSLayoutConstraint!
    
    @IBOutlet weak var languageLabel: UILabel!
    @IBOutlet weak var languageStack: UIStackView!
    @IBOutlet weak var expandBtn: UIButton!
    
    @IBOutlet weak var headerHeight: NSLayoutConstraint!
    @IBOutlet weak var continueWatchingCV: UICollectionView!
    @IBOutlet weak var showListingTableView: UITableView!
    @IBOutlet weak var tableHeight: NSLayoutConstraint!
    
    @IBOutlet weak var continueWatchingStack: UIStackView!
    @IBOutlet weak var noDataView: UIView!
    
    @IBOutlet weak var loadMoreBtn: LocalisedElementsButton!
    @IBOutlet weak var loadMoreActivityIndicator: UIActivityIndicatorView!
    
    var vm = WebSeriesVM()
    
    private var isFirstLoad = true
    
    override func viewDidLoad() {
        super.viewDidLoad()
        vm.vc = self
        scrollView.delegate = self
        vm.initView()
        navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
        navigationController?.navigationBar.shadowImage = UIImage()
        
        isFirstLoad = false
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        navigationController?.setNavigationBarHidden(false, animated: animated)
        self.navigationController?.setColor(color: .white)
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        self.navigationController?.setNavigationBarHidden(true, animated: animated)
        
        if self.isMovingFromParent {
            // Back button was pressed
            PersistentStorage.shared.addOthersCount()
        }
    }
    
    override func viewDidDisappear(_ animated: Bool) {
        super.viewDidDisappear(animated)
        
        // Customize the navigation bar's appearance
        self.navigationController?.setColor(color: .black)
    }
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        
        if !isFirstLoad{
            if K.GVar.reloadContinueWebSeries{
                K.GVar.reloadContinueWebSeries = false
                vm.getContinueWatching()
            }
        }else{
            K.GVar.reloadContinueWebSeries = false
        }
        
        /*
         Check if ads is there to map impressions
         */
        if let adsData = AuthFunc.shareInstance.adsData{
            // check if ads data contains ad for webseries
            if let webSeriesAd = adsData.result?.filter({$0.forPage == AdsEnum.web_series.rawValue}).first, let adID = webSeriesAd.id{
                PersistentStorage.shared.addAdsCount(adID: adID ,impressions: 1)
            }
        }
    }
    
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        vm.updateTableHeight()
        
    }
    
    // MARK: - Tap Handler
    @IBAction func playTrailer(_ sender: LocalisedElementsButton) {
        
        //If its ads then nav user to webview
        if let adsData = AuthFunc.shareInstance.adsData, let webSeriesAd = adsData.result?.filter({$0.forPage == AdsEnum.web_series.rawValue}).first{
            // check if ads data contains ad for webseries
            if let adLink = webSeriesAd.adLink,let adID = webSeriesAd.id{
                PersistentStorage.shared.addAdsCount(adID: adID ,clicks: 1)
                if let url = URL(string: adLink), UIApplication.shared.canOpenURL(url) {
                    UIApplication.shared.open(url)
                }
                return
            }
        }
        
        let item = JwPlayerItemCreate(url: APIEndPoints.StaticURLs.masilaUrl, poster: nil, titles: "Masila")
        JWPlayerManager.shared.presentPlayer(from: self, playerItems: [item], contentType: .trailer, videoIDs: [0])
        PersistentStorage.shared.addTrailerCount()
    }
    
    @IBAction func expandLanguageTapped(_ sender: UIButton) {
        vm.dropDownModule.show()
        PersistentStorage.shared.addOthersCount()
        self.expandBtn.setImage(UIImage(named: "SupportUpArrow"), for: .normal)
    }
    
    @IBAction func loadMoreBtnTapped(_ sender: LocalisedElementsButton) {
        PersistentStorage.shared.addOthersCount()
        loadMoreBtn.isHidden = true
        vm.pageNo += 1
        loadMoreActivityIndicator.startAnimating()
        if let index = vm.dropDownModule.indexForSelectedRow, let categoryIndex = vm.categoryListingData[index].id{
            vm.getShowListing(categoryID: categoryIndex, isBtnClick: true)
        }
        PersistentStorage.shared.addOthersCount()
    }
    
    @IBAction func retryBtnTapped(_ sender: LocalisedElementsButton) {
        PersistentStorage.shared.addOthersCount()
        noDataView.isHidden = true
        // for the first load always send hindi as it is default category
        headerViewLabel.isHidden = false
        headerBtn.isHidden = false
        
        vm.startShimmer()
        vm.checkAds()
//        showListingTableView.reloadData()
        vm.getShowListing(categoryID: 18)
        self.vm.getCategoryListing()
    }
}

// MARK: - TableView DataSource , Delegates

extension WebSeriesVC : TableViewSRC{
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return vm.showData.count == 0 ? 2 : vm.showData.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: K.CellIdentifier.WebSeries.webSeriesShowListingCell) as! WebSeriesShowListingCell

        if vm.showData.count == 0{
            cell.showShimmer()
        }else{
            cell.stopShimmer()
            let data = vm.showData[indexPath.row]
            let categoryID = vm.categoryListingData[vm.dropDownModule.indexForSelectedRow ?? 0].id ?? 0
            cell.setData(data: data,selectedCategory: categoryID)
        }
        
        cell.btnTapped = { [self] (type) -> Void in
            updateFavLikes(type: type, index: indexPath.row)
        }
        
        return cell
    }
    
    func updateFavLikes(type : FavCellCLick, index : Int){
        let data = vm.showData[index]
        if let postID = data.id, let categoryID = vm.categoryListingData[vm.dropDownModule.indexForSelectedRow ?? 0].id{
            PersistentStorage.shared.addWebSeries(catID: categoryID, postID: postID, postType: .series)
        }
        
        let categoryID = vm.categoryListingData[vm.dropDownModule.indexForSelectedRow ?? 0].id ?? 0
        switch type {
        case .favourite:
            guard let isFav = data.markAsFavourite ,let postID = data.id,let postType = data.contentMoreDetails?.first?.postType else{return}
            if let categoryIds = data.favouriteCategoryIDS?.rawValue {
                let components = categoryIds.components(separatedBy: ",")
                if isFav == true && (components.first == categoryID.toString() || components.last == categoryID.toString()){
                    LikeFavCommonFunc.shareInstance.removeFavourite(postID: postID, postType: postType, categoryID: categoryID, vc: self) { [unowned self] isDone in
                        if isDone{
                            vm.showData[index].markAsFavourite = false
                            vm.showData[index].favouriteCategoryIDS = ValueWrapper.stringValue("")
                            showListingTableView.reloadRows(at: [IndexPath(row: index, section: 0)],with: .none)
                            if categoryID == 18{ // hindi
                                K.GVar.reloadHindiWebseries = true
                            }else{ // english
                                K.GVar.reloadEnglishWebseries = true
                            }

                            // Update for mylist
//                            if let id = vm.categoryListingData[vm.dropDownModule.indexForSelectedRow ?? 0].id{
//                                if id == 1{ // english
//                                    if let index = MyListDataTemp.shareInstance.favListingData?.showData?.english?.firstIndex(where: {$0.id == postID}){
//                                        MyListDataTemp.shareInstance.favListingData?.showData?.english?.remove(at: index)
//                                        K.GVar.myListSoftReload = true
//                                    }
//                                }else{ // hindi
//                                    if let index = MyListDataTemp.shareInstance.favListingData?.showData?.hindi?.firstIndex(where: {$0.id == postID}){
//                                        MyListDataTemp.shareInstance.favListingData?.showData?.hindi?.remove(at: index)
//                                        K.GVar.myListSoftReload = true
//                                    }
//                                }
//                            }
                        }
                    }
                }else{
                    LikeFavCommonFunc.shareInstance.addFavourite(postID: postID, postType: postType, categoryID: categoryID, vc: self) { [unowned self] isDone in
                        if isDone{
                            vm.showData[index].markAsFavourite = true
                            vm.showData[index].favouriteCategoryIDS = ValueWrapper.stringValue(categoryID.toString())
                            showListingTableView.reloadRows(at: [IndexPath(row: index, section: 0)],with: .none)
                            if categoryID == 18{ // hindi
                                K.GVar.reloadHindiWebseries = true
                            }else{ // english
                                K.GVar.reloadEnglishWebseries = true
                            }
                            // Update for mylist
//                            if let id = vm.categoryListingData[vm.dropDownModule.indexForSelectedRow ?? 0].id{
//                                if id == 1{ // english
//                                    if !MyListDataTemp.shareInstance.isDatafetched {return}
//                                    let showData = vm.showData[index]
//                                    MyListDataTemp.shareInstance.favListingData?.showData?.append(FavouriteListingDM.ResultData.ShowDatum(id: showData.id, title: showData.title, description: showData.description, thumbnailPath: showData.thumbnailPath, showType: showData.showType, totalSeasons: showData.totalSeasons, totalEpisodes: showData.totalEpisodes, ageRangeMasterID: showData.ageRangeMasterID, genderMasterID: showData.genderMasterID, categoryMasterID: "1", contentMoreDetails: showData.contentMoreDetails, markAsFavourite: showData.markAsFavourite, isLiked: showData.isLiked, likesCount: showData.likesCount, viewsCount: showData.viewsCount, bookmarkCount: showData.bookmarkCount, bookmarkCategoryIDS: "1"))
//                                    K.GVar.myListSoftReload = true
//                                }else{ // hindi
//                                    // return if the data is not fetched. IT means user has not loaded mylist yet
//                                    if !MyListDataTemp.shareInstance.isDatafetched {return}
//                                    let showData = vm.showData[index]
//                                    MyListDataTemp.shareInstance.webSeriesHindi.append(FavouriteListingDM.ResultData.ShowDatum(id: showData.id, title: showData.title, description: showData.description, thumbnailPath: showData.thumbnailPath, showType: showData.showType, totalSeasons: showData.totalSeasons, totalEpisodes: showData.totalEpisodes, ageRangeMasterID: showData.ageRangeMasterID, genderMasterID: showData.genderMasterID, categoryMasterID: "18", contentMoreDetails: showData.contentMoreDetails, markAsFavourite: showData.markAsFavourite, isLiked: showData.isLiked, likesCount: showData.likesCount, viewsCount: showData.viewsCount, bookmarkCount: showData.bookmarkCount, bookmarkCategoryIDS: "18"))
//                                    K.GVar.myListSoftReload = true
//                                }
//                            }
                            
                        }
                    }
                }
                return
            }
        case .liked:
            guard let isLiked = data.isLiked ,let postID = data.id,let postType = data.contentMoreDetails?.first?.postType else{return}
            if isLiked{
                LikeFavCommonFunc.shareInstance.unlikePost(postID: postID, postType: postType, vc: self) { [unowned self] isDone in
                    if isDone{
                        vm.showData[index].isLiked = false
                        vm.showData[index].likesCount! -= 1
                        showListingTableView.reloadRows(at: [IndexPath(row: index, section: 0)],with: .none)
                        
                        K.GVar.myListSoftReload = true
                        
                        if let index = MyListDataTemp.shareInstance.favListingData?.showData?.english?.firstIndex(where: {$0.id == postID}){
                            MyListDataTemp.shareInstance.favListingData?.showData?.english?[index].isLiked = false
                            MyListDataTemp.shareInstance.favListingData?.showData?.english?[index].likesCount! -= 1
                        }
                        
                        if let index = MyListDataTemp.shareInstance.favListingData?.showData?.hindi?.firstIndex(where: {$0.id == postID}){
                            MyListDataTemp.shareInstance.favListingData?.showData?.hindi?[index].isLiked = false
                            MyListDataTemp.shareInstance.favListingData?.showData?.hindi?[index].likesCount! -= 1
                        }
                    }
                }
            }else{
                LikeFavCommonFunc.shareInstance.likePost(postID: postID, postType: postType, vc: self){ [unowned self] isDone in
                    if isDone{
                        vm.showData[index].isLiked = true
                        vm.showData[index].likesCount! += 1
                        showListingTableView.reloadRows(at: [IndexPath(row: index, section: 0)],with: .none)
                        
                        // Soft reload for webseries english
                        K.GVar.myListSoftReload = true
                        
                        if let index = MyListDataTemp.shareInstance.favListingData?.showData?.english?.firstIndex(where: {$0.id == postID}){
                            MyListDataTemp.shareInstance.favListingData?.showData?.english?[index].isLiked = true
                            MyListDataTemp.shareInstance.favListingData?.showData?.english?[index].likesCount! += 1
                        }
                        
                        if let index = MyListDataTemp.shareInstance.favListingData?.showData?.english?.firstIndex(where: {$0.id == postID}){
                            MyListDataTemp.shareInstance.favListingData?.showData?.english?[index].isLiked = true
                            MyListDataTemp.shareInstance.favListingData?.showData?.english?[index].likesCount! += 1
                        }
                    }
                }
            }
//            K.GVar.reloadMyList = true
        }
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if vm.showData.count == 0 {return}
        let showData = vm.showData[indexPath.row]
        
        if let postID = showData.id, let categoryID = vm.categoryListingData[vm.dropDownModule.indexForSelectedRow ?? 0].id{
            PersistentStorage.shared.addWebSeries(catID: categoryID, postID: postID, postType: .series)
        }
        
        let sb = UIStoryboard(name: K.StoryBoard.webSeries, bundle: nil)
        let vcPush = sb.instantiateViewController(withIdentifier: K.StoryBoardID.WebSeries.webSeriesSeasonVC) as! WebSeriesSeasonVC
        vcPush.vm.showData = showData
        vcPush.vm.indexSelected = indexPath.row
        vcPush.likeFavDelegate = self
        if let selectedIndex = vm.dropDownModule.indexForSelectedRow{
            let categoryID = vm.categoryListingData[selectedIndex]
            vcPush.vm.categoryID = categoryID.id
        }
        self.navigationController?.pushViewController(vcPush, animated: true)
    }
}

extension WebSeriesVC : ReloadSeriesFavLike{
    
    // if like or fav is done in webseriesseason vc, dont need to do api call in this screen.
    func updateRows(index: Int, type: FavCellCLick, isFav: Bool?, isLike: Bool?, id: Int?) {
        if let isFav{
            let categoryID = vm.categoryListingData[vm.dropDownModule.indexForSelectedRow ?? 0].id ?? 0
            switch isFav{
            case true:
                vm.showData[index].markAsFavourite = true
                vm.showData[index].favouriteCategoryIDS = ValueWrapper.stringValue(categoryID.toString())
                showListingTableView.reloadRows(at: [IndexPath(row: index, section: 0)],with: .none)
            case false:
                vm.showData[index].markAsFavourite = false
                vm.showData[index].favouriteCategoryIDS = ValueWrapper.stringValue("")
                showListingTableView.reloadRows(at: [IndexPath(row: index, section: 0)],with: .none)
            }
        }
        
        if let isLike{
            switch isLike{
            case true:
                vm.showData[index].isLiked = true
                vm.showData[index].likesCount! += 1
                showListingTableView.reloadRows(at: [IndexPath(row: index, section: 0)],with: .none)
            case false:
                vm.showData[index].isLiked = false
                vm.showData[index].likesCount! -= 1
                showListingTableView.reloadRows(at: [IndexPath(row: index, section: 0)],with: .none)
            }
        }
    }
}

// MARK: - CollectionView Delegate and Data Source

extension WebSeriesVC : CollectionViewSRC{
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return vm.continueWatchingData.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: K.CellIdentifier.WebSeries.webSeriesCell, for: indexPath) as! WebSeriesCell
        let data = vm.continueWatchingData[indexPath.row]
        cell.setData(data: data)
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let data = vm.continueWatchingData[indexPath.row]
        
        if let postID = data.id, let categoryID = vm.categoryListingData[vm.dropDownModule.indexForSelectedRow ?? 0].id{
            PersistentStorage.shared.addWebSeries(catID: categoryID, postID: postID, postType: .series)
        }
        
        let sb = UIStoryboard(name: K.StoryBoard.webSeries, bundle: nil)
        let vcPush = sb.instantiateViewController(withIdentifier: K.StoryBoardID.WebSeries.continueWatchingVC) as! ContinueWatchingVC
        vcPush.modalPresentationStyle = .overCurrentContext
        vcPush.modalTransitionStyle = .crossDissolve
        vcPush.watchData = data
        if let selectedIndex = vm.dropDownModule.indexForSelectedRow{
            let categoryID = vm.categoryListingData[selectedIndex]
            vcPush.categoryID = categoryID.id
        }
        self.present(vcPush, animated: true)

    }
    
}

// MARK: - Collection Flow Layout

extension WebSeriesVC : 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)
    }
}


// MARK: - Google Ad Banner Delegate

extension WebSeriesVC : GADBannerViewDelegate{
    func bannerViewDidReceiveAd(_ bannerView: GADBannerView) {
        switch bannerView{
        case vm.headerBannerView:
            headerView.stopShimmer()
        case vm.masilaBannerView:
            headerBtn.alpha = 0
            headerBtn.isEnabled = false
            masilaTrailerView.stopShimmer()
        default:
            break
        }
        print("bannerViewDidReceiveAd")
        bannerView.backgroundColor = #colorLiteral(red: 0.01960784314, green: 0, blue: 0.2196078431, alpha: 1)
    }
    
    func bannerView(_ bannerView: GADBannerView, didFailToReceiveAdWithError error: Error) {
        switch bannerView{
        case vm.headerBannerView:
            headerView.stopShimmer()
        case vm.masilaBannerView:
            headerViewLabel.isHidden = true
            headerBtn.isHidden = true
            masilaTrailerView.stopShimmer()
        default:
            break
        }
        print("bannerView:didFailToReceiveAdWithError: \(error.localizedDescription)")
    }
    
    func bannerViewDidRecordImpression(_ bannerView: GADBannerView) {
        print("bannerViewDidRecordImpression")
    }
    
    func bannerViewWillPresentScreen(_ bannerView: GADBannerView) {
        print("bannerViewWillPresentScreen")
    }
    
    func bannerViewWillDismissScreen(_ bannerView: GADBannerView) {
        print("bannerViewWillDIsmissScreen")
    }
    
    func bannerViewDidDismissScreen(_ bannerView: GADBannerView) {
        print("bannerViewDidDismissScreen")
    }
}


// MARK: - Animating scrollView

extension WebSeriesVC: UIScrollViewDelegate {
        
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        
        // Get the current vertical offset of the scroll view
        let y = scrollView.contentOffset.y

        // Define the height range for the header view
        let minHeaderHeight: CGFloat = 0.0 // Height at which the header becomes invisible
        
        // Calculate the new height for the header view based on the scroll position
        let newHeaderHeight: CGFloat
        if y < 0 {
            // When scrolling up beyond the top, ensure the header view is fully expanded
            newHeaderHeight = vm.maxHeaderHeight
        } else {
            // Calculate the new height for the header view, ensuring it doesn't go below the minimum height
            newHeaderHeight = max(minHeaderHeight, vm.maxHeaderHeight - y)
        }

        // Update the header view's height constraint with the new height
        headerHeight.constant = newHeaderHeight

        // Animate the layout changes to smoothly transition the header height
        UIView.animate(withDuration: 0.3) {
            self.view.layoutIfNeeded()
        }
    }
    
}

