//
//  UserNotificationVC.swift
//  WOKA
//
//  Created by MacBook Pro on 13/06/24.
//

import UIKit
import GoogleMobileAds

class UserNotificationVC: UIViewController {

    @IBOutlet weak var tableView: UITableView!
    @IBOutlet weak var noDataStack: UIStackView!
    @IBOutlet weak var adView: UIView!
    @IBOutlet weak var adViewHeight: NSLayoutConstraint!
    
    var vm = UserNotificationVM()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        vm.vc = self
        vm.initView()
        navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
        navigationController?.navigationBar.shadowImage = UIImage()
        tableView.indicatorStyle = .white
    }
    
    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)
        
        // Customize the navigation bar's appearance
        self.navigationController?.setColor(color: .black)
        
        if self.isMovingFromParent {
            // Back button was pressed
            PersistentStorage.shared.addOthersCount()
        }
    }
    
    @IBAction func retryBtnTapped(_ sender: UIButton) {
        PersistentStorage.shared.addOthersCount()
        Utilities.startProgressHUD()
        vm.getUserNotification()
    }
}


// MARK: - TableView DataSource , Delegates

extension UserNotificationVC : TableViewSRC{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return vm.userNotification.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: K.CellIdentifier.Home.userNotificationCell) as! UserNotificationCell
        let data = vm.userNotification[indexPath.row]
        cell.setData(data: data)
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        PersistentStorage.shared.addOthersCount()
        if let type = vm.userNotification[indexPath.row].postType{
            switch type{
            case 1,2,3,4:
                let sb = UIStoryboard(name: K.StoryBoard.webSeries, bundle: nil)
                let vcPush = sb.instantiateViewController(withIdentifier: K.StoryBoardID.WebSeries.webSeriesVC) as! WebSeriesVC
                self.navigationController?.pushViewController(vcPush, animated: true)
            case 6:
                let sb = UIStoryboard(name: K.StoryBoard.Games, bundle: nil)
                let vcPush = sb.instantiateViewController(withIdentifier: K.StoryBoardID.Games.gamesListVC) as! GamesListVC
                self.navigationController?.pushViewController(vcPush, animated: true)
            case 7:
                let sb = UIStoryboard(name: K.StoryBoard.audioBooks, bundle: nil)
                let vcPush = sb.instantiateViewController(withIdentifier: K.StoryBoardID.AudioBooks.audioBookHomeVC) as! AudioBookHomeVC
                self.navigationController?.pushViewController(vcPush, animated: true)
            case 8:
                let sb = UIStoryboard(name: K.StoryBoard.Karaoke, bundle: nil)
                let vcPush = sb.instantiateViewController(withIdentifier: K.StoryBoardID.Karaoke.karaokeListingVC) as! KaraokeListingVC
                self.navigationController?.pushViewController(vcPush, animated: true)
            case 9:
                let sb = UIStoryboard(name: K.StoryBoard.shop, bundle: nil)
                let vcPush = sb.instantiateViewController(withIdentifier: K.StoryBoardID.Shop.shopListingVC) as! ShopListingVC
                self.navigationController?.pushViewController(vcPush, animated: true)
            default:
                break
            }
        }
    }
}

// MARK: - Google Ad Banner Delegate

extension UserNotificationVC : GADBannerViewDelegate{
    
    func bannerViewDidReceiveAd(_ bannerView: GADBannerView) {
        adView.isHidden = false
        bannerView.alpha = 0
        UIView.animate(withDuration: 0.2, animations: {
            bannerView.alpha = 1
        })
        print("bannerViewDidReceiveAd")
    }
    
    func bannerView(_ bannerView: GADBannerView, didFailToReceiveAdWithError error: Error) {
        UIView.animate(withDuration: 0.2, animations: { [weak self] in
            guard let self else{return}
            bannerView.alpha = 0
            self.adView.isHidden = false
        })
        
        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")
    }
}
