//
//  EpisodeDetailsVC.swift
//  WOKA
//
//  Created by MacBook Pro on 25/06/24.
//

import UIKit

class EpisodeDetailsVC: UIViewController {

    @IBOutlet weak var episodeImage: UIImageView!
    @IBOutlet weak var episodeTitle: UILabel!
    @IBOutlet weak var mediaType: UILabel!
    @IBOutlet weak var episodeTimeLine: UILabel!
    @IBOutlet weak var episodeDesc: UITextView!
    @IBOutlet weak var watchBtn: LocalisedElementsButton!
    
    // Completion block to be executed when the alert is dismissed
    var onDoneBlock: (() -> Void)?
    
    var teaserData : TeaserDM.ResultData?
    
    var episodeData : EpisodeListingDM.ResultData?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        episodeDesc.font = FontCustom.shareInstance.customFont(fontName: .Exo2_Medium, size: 16)
        watchBtn.addShadowAndCorner(radius: 25, shadowColor: .black, shadowOpacity: 0.6, shadowOffset: CGSize(width: 0, height: 0.5), shadowRadius: 5)
        if teaserData != nil{
            initTeaser()
        }
        
        if episodeData != nil{
            initEpisode()
        }
    }
    
    func initEpisode(){
        guard let episodeData else{return}
        episodeImage.imageURL(episodeData.thumbnailPath ?? "", color: .black)
        
        if AuthFunc.shareInstance.getDefaultLanguage() == .english{
            guard let englishData = episodeData.contentMoreDetails?.filter({$0.languageMasterID == 1}).first else{return}
            episodeTitle.text = englishData.title
            if let desc = englishData.description?.replacingOccurrences(of: "<br>", with: "").htmlToAttributedString{
                let sizeText = NSMutableAttributedString(attributedString: desc)
                sizeText.setFontFace(font: FontCustom.shareInstance.customFont(fontName: .Exo2_Regular, size: 15),color: UIColor.appColor(.TextDarkBlue)!)
                self.episodeDesc.attributedText = sizeText
            }
        }else{
            guard let hindiData = episodeData.contentMoreDetails?.filter({$0.languageMasterID == 2}).first else{return}
            episodeTitle.text = hindiData.title
            if let desc = hindiData.description?.replacingOccurrences(of: "<br>", with: "").htmlToAttributedString{
                let sizeText = NSMutableAttributedString(attributedString: desc)
                sizeText.setFontFace(font: FontCustom.shareInstance.customFont(fontName: .Exo2_Regular, size: 15),color: UIColor.appColor(.TextDarkBlue)!)
                self.episodeDesc.attributedText = sizeText
            }
        }
        
        mediaType.text = "Episode".localized(loc: AuthFunc.shareInstance.languageSelected.rawValue) + " " + (episodeData.episodeNumber?.toString() ?? "0")
        episodeTimeLine.text = episodeData.episodeDuration
    }
    
    func initTeaser(){
        guard let teaserData else{return}
        episodeImage.imageURL(teaserData.thumbnailPath ?? "", color: .black)
        
        if AuthFunc.shareInstance.getDefaultLanguage() == .english{
            guard let englishData = teaserData.contentMoreDetails?.filter({$0.languageMasterID == 1}).first else{return}
            episodeTitle.text = englishData.title
            if let desc = englishData.description?.replacingOccurrences(of: "<br>", with: "").htmlToAttributedString{
                let sizeText = NSMutableAttributedString(attributedString: desc)
                sizeText.setFontFace(font: FontCustom.shareInstance.customFont(fontName: .Exo2_Regular, size: 15),color: UIColor.appColor(.TextDarkBlue)!)
                self.episodeDesc.attributedText = sizeText
            }
        }else{
            guard let hindiData = teaserData.contentMoreDetails?.filter({$0.languageMasterID == 2}).first else{return}
            episodeTitle.text = hindiData.title
            if let desc = hindiData.description?.replacingOccurrences(of: "<br>", with: "").htmlToAttributedString{
                let sizeText = NSMutableAttributedString(attributedString: desc)
                sizeText.setFontFace(font: FontCustom.shareInstance.customFont(fontName: .Exo2_Regular, size: 15),color: UIColor.appColor(.TextDarkBlue)!)
                self.episodeDesc.attributedText = sizeText
            }
        }
        
        mediaType.text = "Teaser".localized(loc: AuthFunc.shareInstance.languageSelected.rawValue) + " " + (teaserData.serialNumber?.toString() ?? "0")
        episodeTimeLine.text = teaserData.teaserDuration
    }
    
    @IBAction func closeBtnTapped(_ sender: UIButton) {
        self.dismiss(animated: true)
        PersistentStorage.shared.addOthersCount()
    }
    
    @IBAction func watchBtnTapped(_ sender: LocalisedElementsButton) {
        if let episodeData = episodeData, let postID = episodeData.id, let categoryID = (AuthFunc.shareInstance.languageSelected == .english ? 1 : 18){
            PersistentStorage.shared.addWebSeries(catID: categoryID, postID: postID, postType: .episode)
        }
        self.dismiss(animated: true) {
            self.onDoneBlock?()
        }
    }
}
