//
//  WebSeriesShowListingCell.swift
//  WOKA
//
//  Created by MacBook Pro on 19/06/24.
//

import UIKit

class WebSeriesShowListingCell: UITableViewCell {
    
    @IBOutlet weak var showThumbnail: UIImageView!
    @IBOutlet weak var showTitle: UILabel!
    @IBOutlet weak var totalLikes: UILabel!
    @IBOutlet weak var likeBtn: UIButton!
    @IBOutlet weak var favBtn: UIButton!
    @IBOutlet weak var shimmerView: ShimmerEffectView!
    
    typealias btnTappedBlock = ( _ from : FavCellCLick) -> Void  // 0 - plus 1 - minus
    var btnTapped : btnTappedBlock!
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }
    
    func showShimmer(){
        shimmerView.startShimmer()
    }
    
    func stopShimmer(){
        shimmerView.stopShimmer()
    }
    
    func setData(data : WebSeriesShowListDM.ShowDatum, selectedCategory : Int){
        //heart.fill , heart , hand.thumbsup.fill , hand.thumbsup
        if AuthFunc.shareInstance.getDefaultLanguage() == .english{
            showTitle.text = data.contentMoreDetails?.filter({$0.languageMasterID == 1}).first?.title
        }else{
            showTitle.text = data.contentMoreDetails?.filter({$0.languageMasterID == 2}).first?.title
        }
        totalLikes.text = data.likesCount?.toString() ?? "0"
        
        if let url = data.thumbnailPath{
            showThumbnail.imageURL(url)
        }
        
        if let like = data.isLiked{
            switch like{
            case true:
                likeBtn.setImage(UIImage(named: "LikeAdd"), for: .normal)
            case false:
                likeBtn.setImage(UIImage(named: "LikeRemove"), for: .normal)
            }
        }
        
        if let favourite = data.markAsFavourite{
            if let categoryIds = data.favouriteCategoryIDS?.rawValue { // if string, it means category is selected for multiple language
                let components = categoryIds.components(separatedBy: ",")
                if favourite == true && (components.first == selectedCategory.toString() || components.last == selectedCategory.toString()){
                    favBtn.setImage(UIImage(named: "FavouriteAdd"), for: .normal)
                }else{
                    favBtn.setImage(UIImage(named: "FavouriteRemove"), for: .normal)
                }
                return
            }
            
            if favourite == true && data.favouriteCategoryIDS?.intValue == selectedCategory{
                favBtn.setImage(UIImage(named: "FavouriteAdd"), for: .normal)
            }else{
                favBtn.setImage(UIImage(named: "FavouriteRemove"), for: .normal)
            }
        }
    }
    
    func setAudioData(data: ListenAudioListingDM.AudioDatum){
        //heart.fill , heart , hand.thumbsup.fill , hand.thumbsup
        if AuthFunc.shareInstance.getDefaultLanguage() == .english{
            showTitle.text = data.contentMoreDetails?.filter({$0.languageMasterID == 1}).first?.title
        }else{
            showTitle.text = data.contentMoreDetails?.filter({$0.languageMasterID == 2}).first?.title
        }
        totalLikes.text = data.likesCount?.toString() ?? "0"
        
        if let url = data.thumbnailPath{
            showThumbnail.imageURL(url)
        }
        
        if let like = data.isLiked{
            switch like{
            case true:
                likeBtn.setImage(UIImage(named: "LikeAdd"), for: .normal)
            case false:
                likeBtn.setImage(UIImage(named: "LikeRemove"), for: .normal)
            }
        }
        
        if let favourite = data.markAsFavourite{
            if favourite == true{
                favBtn.setImage(UIImage(named: "FavouriteAdd"), for: .normal)
            }else{
                favBtn.setImage(UIImage(named: "FavouriteRemove"), for: .normal)
            }
        }
    }
    
    func setGameData(data: GamesListDM.GameDatum){
        //heart.fill , heart , hand.thumbsup.fill , hand.thumbsup
        if AuthFunc.shareInstance.getDefaultLanguage() == .english{
            showTitle.text = data.contentMoreDetails?.filter({$0.languageMasterID == 1}).first?.title
        }else{
            showTitle.text = data.contentMoreDetails?.filter({$0.languageMasterID == 2}).first?.title
        }
        totalLikes.text = data.likesCount?.toString() ?? "0"
        
        if let url = data.thumbnailPath{
            showThumbnail.imageURL(url)
        }
        
        if let like = data.isLiked{
            switch like{
            case true:
                likeBtn.setImage(UIImage(named: "LikeAdd"), for: .normal)
            case false:
                likeBtn.setImage(UIImage(named: "LikeRemove"), for: .normal)
            }
        }
        
        if let favourite = data.markAsFavourite{
            if favourite == true{
                favBtn.setImage(UIImage(named: "FavouriteAdd"), for: .normal)
            }else{
                favBtn.setImage(UIImage(named: "FavouriteRemove"), for: .normal)
            }
        }
    }
    
    func setKaraokeData(data: KaraokeListingDM.KaraokeDatum){
        //heart.fill , heart , hand.thumbsup.fill , hand.thumbsup
        if AuthFunc.shareInstance.getDefaultLanguage() == .english{
            showTitle.text = data.contentMoreDetails?.filter({$0.languageMasterID == 1}).first?.title
        }else{
            showTitle.text = data.contentMoreDetails?.filter({$0.languageMasterID == 2}).first?.title
        }
        totalLikes.text = data.likesCount?.toString() ?? "0"
        
        if let url = data.thumbnailPath{
            showThumbnail.imageURL(url)
        }
        
        if let like = data.isLiked{
            switch like{
            case true:
                likeBtn.setImage(UIImage(named: "LikeAdd"), for: .normal)
            case false:
                likeBtn.setImage(UIImage(named: "LikeRemove"), for: .normal)
            }
        }
        
        if let favourite = data.markAsFavourite{
            if favourite == true{
                favBtn.setImage(UIImage(named: "FavouriteAdd"), for: .normal)
            }else{
                favBtn.setImage(UIImage(named: "FavouriteRemove"), for: .normal)
            }
        }
    }
    
    @IBAction func btnTapped(_ sender: UIButton) {
        if AuthFunc.shareInstance.guestUserLoginPopUp() { return}
        
        switch sender{
        case likeBtn:
            if btnTapped != nil {
                btnTapped( .liked)
            }
        case favBtn:
            if btnTapped != nil {
                btnTapped( .favourite)
            }
        default:
            break
        }
    }
}
