//
//  ContinueWatchingVC.swift
//  WOKA
//
//  Created by MacBook Pro on 26/06/24.
//

import UIKit

class ContinueWatchingVC: UIViewController {

    @IBOutlet weak var watchingImage: UIImageView!
    @IBOutlet weak var watchingTitle: UILabel!
    @IBOutlet weak var watchingDesc: UITextView!
    @IBOutlet weak var outerView: UIView!
    @IBOutlet weak var contentView: UIView!
    
    var watchData : ContinueWatchingDM.ResultData?
    
    // This ID is from webseries main screen, where user selectes
    var categoryID : Int?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        if watchData != nil{
            setData()
        }
        let tapGesture = UITapGestureRecognizer(target: self, action: #selector(handleTap(_:)))
        outerView.addGestureRecognizer(tapGesture)
    }
    
    @objc func handleTap(_ gesture: UITapGestureRecognizer) {
        let location = gesture.location(in: outerView)
        if !contentView.frame.contains(location) {
            PersistentStorage.shared.addOthersCount()
            self.dismiss(animated: true)
        }
    }
    
    func setData(){
        guard let watchData else{return}
        
        if let url = watchData.thumbnailPath{
            self.watchingImage.imageURL(url, color: .black)
        }
        
        if AuthFunc.shareInstance.getDefaultLanguage() == .english{
            guard let englishData = watchData.contentMoreDetails?.filter({$0.languageMasterID == 1}).first else{return}
            watchingTitle.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.watchingDesc.attributedText = sizeText
            }
        }else{
            guard let hindiData = watchData.contentMoreDetails?.filter({$0.languageMasterID == 2}).first else{return}
            watchingTitle.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.watchingDesc.attributedText = sizeText
            }
        }
    }
    
    @IBAction func watchBtnTapped(_ sender: LocalisedElementsButton) {
        guard let watchData ,let showID = watchData.id else{return}
        if let catID = self.categoryID{
            PersistentStorage.shared.addWebSeries(catID: catID, postID: showID, postType: .episode)
        }
        
        var playerItem = JwPlayerItemCreate(url: "", poster: "", titles: "")
        playerItem.poster = watchData.thumbnailPath
        if AuthFunc.shareInstance.getDefaultLanguage() == .english{
            guard let englishData = watchData.contentMoreDetails?.filter({$0.languageMasterID == 1}).first else{return}
            playerItem.titles = englishData.title
        }else{
            guard let hindiData = watchData.contentMoreDetails?.filter({$0.languageMasterID == 2}).first else{return}
            playerItem.titles = hindiData.title
        }
        
        if categoryID == 1{
            guard let englishData = watchData.contentMoreDetails?.filter({$0.languageMasterID == 1}).first, let url = englishData.contentHDURL else{return}
            playerItem.url = url

        }else if categoryID == 18{
            guard let hindiData = watchData.contentMoreDetails?.filter({$0.languageMasterID == 2}).first, let url = hindiData.contentHDURL else{return}
            playerItem.url = url
        }
        
        JWPlayerManager.shared.presentPlayer(from: self, playerItems: [playerItem], startIndex: 0, contentType: .continueWatching,videoIDs: [showID], catID: categoryID)

    }

    @IBAction func closeBtnTapped(_ sender: LocalisedElementsButton) {
        PersistentStorage.shared.addOthersCount()
        self.dismiss(animated: true)
    }
}
