//
//  BroadcastInfoVC.swift
//  WoWonder
//
//  Created by iMac on 21/05/24.
//  Copyright © 2024 ScriptSun. All rights reserved.
//

import UIKit
import Async
import Toast_Swift
import WowonderMessengerSDK
import SDWebImage

class BroadcastInfoVC: BaseVC {
    
    // MARK: - IBOutlets
    
    @IBOutlet weak var broadcastImage: UIImageView!
    @IBOutlet weak var broadcastNameLabel: UILabel!
    @IBOutlet weak var tableView: UITableView!
    @IBOutlet weak var tableViewHeightConstraint: NSLayoutConstraint!
    
    // MARK: - Properties
    
    var broadcastData: BroadcastData?
    
    // MARK: - View Life Cycles
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // Do any additional setup after loading the view.
        
        self.initialConfig()
    }
    
    // MARK: - Selectors
    
    // Back Button Action
    @IBAction func backButtonAction(_ sender: UIButton) {
        self.view.endEditing(true)
        self.navigationController?.popViewController(animated: true)
    }
    
    // MARK: - Helper Functions
    
    // Initial Config
    func initialConfig() {
        self.tableViewSetup()
        self.setData()
    }
    
    // Set Data
    func setData() {
        self.broadcastNameLabel.text = self.broadcastData?.name ?? ""
        let url = URL(string: self.broadcastData?.image ?? "")
        let indicator = SDWebImageActivityIndicator.medium
        self.broadcastImage.sd_imageIndicator = indicator
        DispatchQueue.global(qos: .userInteractive).async {
            self.broadcastImage.sd_setImage(with: url, placeholderImage: UIImage(named: ""))
        }
    }
    
    // TableView Setup
    func tableViewSetup() {
        self.tableView.dataSource = self
        self.tableView.delegate = self
        self.tableView.register(UINib(resource: R.nib.groupAddParticipantsCell), forCellReuseIdentifier: R.reuseIdentifier.groupAddParticipantsCell.identifier)
        self.tableViewHeightConstraint.constant = CGFloat(68 * (self.broadcastData?.users?.count ?? 0))
    }
    
}

// MARK: - Extensions

// MARK: TableView Setup
extension BroadcastInfoVC: UITableViewDelegate, UITableViewDataSource {
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.broadcastData?.users?.count ?? 0
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: R.reuseIdentifier.groupAddParticipantsCell.identifier, for: indexPath) as! GroupAddParticipantsCell
        if let userData = self.broadcastData?.users?[indexPath.row] {
            cell.cancelButton.isHidden = true
            cell.setData(object: userData)
        }
        return cell
    }
    
}
