//
//  GamesListVC.swift
//  WOKA
//
//  Created by MacBook Pro on 04/07/24.
//

import UIKit

class GamesListVC: UIViewController {
    
    @IBOutlet weak var scrollView: UIScrollView!
    @IBOutlet weak var headerHeight: NSLayoutConstraint!
    @IBOutlet weak var gamesListingTableView: UITableView!
    @IBOutlet weak var tableHeight: NSLayoutConstraint!
    @IBOutlet weak var headerView: ShimmerEffectView!
    @IBOutlet weak var headerTitleHeight: NSLayoutConstraint!
    @IBOutlet weak var headerBtn: LocalisedElementsButton!
    @IBOutlet weak var headerImage: UIImageView!
    @IBOutlet weak var headerTitleLabel: UILabel!
    @IBOutlet weak var gamesLoadingView: ShimmerEffectView!
    
    @IBOutlet weak var noDataView: UIView!
    @IBOutlet weak var loadMoreBtn: LocalisedElementsButton!
    @IBOutlet weak var loadMoreActivityIndicator: UIActivityIndicatorView!
    
    var vm = GamesListVM()
                
    override func viewDidLoad() {
        super.viewDidLoad()
        vm.vc = self
        vm.initView()
        scrollView.delegate = self
        self.title = "GAMES".localized(loc: AuthFunc.shareInstance.languageSelected.rawValue)
        navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
        navigationController?.navigationBar.shadowImage = UIImage()
        

    }

    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) {
        /*
         Check if ads is there to map impressions
         */
        if let adsData = AuthFunc.shareInstance.adsData{
            // check if ads data contains ad for webseries
            if let gamesAd = adsData.result?.filter({$0.forPage == AdsEnum.games.rawValue}).first, let adID = gamesAd.id{
                PersistentStorage.shared.addAdsCount(adID: adID ,impressions: 1)
            }
        }
    }
    
    override func viewDidLayoutSubviews() {
        vm.updateTableHeight()
    }
    
    // MARK: - Tap Handler

    @IBAction func gameBtnTapped(_ sender: LocalisedElementsButton) {
        
        /*
         MAke logic for ads
         */
        if let adsData = AuthFunc.shareInstance.adsData{
            // check if ads data contains ad for webseries
            if let gamesAd = adsData.result?.filter({$0.forPage == AdsEnum.games.rawValue}).first, let adLink = gamesAd.adLink, let adID = gamesAd.id{
                PersistentStorage.shared.addAdsCount(adID: adID,clicks: 1)
                if let url = URL(string: adLink), UIApplication.shared.canOpenURL(url) {
                    UIApplication.shared.open(url)
                }
                return
            }
        }
        
        let sb = UIStoryboard(name: K.StoryBoard.Games, bundle: nil)
        let vcPush = sb.instantiateViewController(withIdentifier: K.StoryBoardID.Games.gamesWebViewVC) as! GamesWebViewVC
        let gameData = vm.gameData[vm.indexToLoad]
        
        if let postID = gameData.id{
            PersistentStorage.shared.addGamesCount(postID: postID)
        }
        
        vcPush.url = gameData.gameURL
        vcPush.orientation = gameData.screenOrientation
        vcPush.modalTransitionStyle = .crossDissolve
        vcPush.modalPresentationStyle = .fullScreen
        self.present(vcPush, animated: true)
    }
    
    @IBAction func loadMoreBtnTapped(_ sender: LocalisedElementsButton) {
        PersistentStorage.shared.addOthersCount()
        
        loadMoreBtn.isHidden = true
        vm.pageNo += 1
        loadMoreActivityIndicator.startAnimating()
        vm.getGamesListing(isBtnClick: true)
    }
    
    @IBAction func retryBtnTapped(_ sender: LocalisedElementsButton) {
        /*
         make page 0 as its lazy loading, hide the no data stack as retry is tapped,
         start the shimmer and reload tableview so that both header and table shimmer matches, get the karaoke listing
         */
        vm.pageNo = 0
        noDataView.isHidden = true
        vm.startShimmer()
        vm.getGamesListing()
    }
}

// MARK: - TableView DataSource , Delegates

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

        if vm.gameData.count == 0{
            cell.showShimmer()
        }else{
            let data = vm.gameData[indexPath.row]
            cell.setGameData(data: data)
            cell.stopShimmer()
        }
        
        cell.btnTapped = { [self] (type) -> Void in
            vm.updateFavLikes(type: type, index: indexPath.row)
        }
        
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        vm.indexToLoad = indexPath.row
        
        /*
         MAke logic for ads
         */
        if let adsData = AuthFunc.shareInstance.adsData, adsData.result?.filter({$0.forPage == AdsEnum.games.rawValue}).first != nil{
            // check if ads data contains ad for webseries
        }else{
            vm.setHeaderData()
        }
        
        let data = vm.gameData[indexPath.row]

        if let postID = data.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.gameIndex = indexPath.row
        vcPush.gameData = data
        vcPush.delegate = self
        self.present(vcPush, animated: true)
    }
}

extension GamesListVC : ReloadSeriesFavLike{
    
    func updateRows(index: Int, type: FavCellCLick, isFav: Bool?, isLike: Bool?, id: Int?) {
        if let isFav{
            switch isFav{
            case true:
                vm.gameData[index].markAsFavourite = true
                gamesListingTableView.reloadRows(at: [IndexPath(row: index, section: 0)],with: .none)
            case false:
                vm.gameData[index].markAsFavourite = false
                gamesListingTableView.reloadRows(at: [IndexPath(row: index, section: 0)],with: .none)
            }
        }
        
        if let isLike{
            switch isLike{
            case true:
                vm.gameData[index].isLiked = true
                vm.gameData[index].likesCount! += 1
                gamesListingTableView.reloadRows(at: [IndexPath(row: index, section: 0)],with: .none)
            case false:
                vm.gameData[index].isLiked = false
                vm.gameData[index].likesCount! -= 1
                gamesListingTableView.reloadRows(at: [IndexPath(row: index, section: 0)],with: .none)
            }
        }
    }
}

// MARK: - Animating scrollView

extension GamesListVC: 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
//        let maxHeaderHeight: CGFloat = 200.0 // Maximum height when fully visible

        // 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()
        }
    }
}
