//
//  CallLogCell.swift
//  WoWonder
//
//  Created by iMac on 10/10/23.
//  Copyright © 2023 ScriptSun. All rights reserved.
//

import UIKit
import SDWebImage

protocol CallLogCellDelegate {
    func handleCallButtonTap(indexPath: IndexPath)
}

class CallLogCell: UITableViewCell {
    
    @IBOutlet weak var profileImage: UIImageView!
    @IBOutlet weak var userNameLabel: UILabel!
    @IBOutlet weak var callLogImageView: UIImageView!
    @IBOutlet weak var callLogLabel: UILabel!
    @IBOutlet weak var callButton: UIButton!
    
    var delegate: CallLogCellDelegate?
    var indexPath = IndexPath()

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
    
    // Set Data
    func setData(object: CallLogModel) {
        let imageUrl = URL.init(string: object.avatar ?? "")
        let indicator = SDWebImageActivityIndicator.medium
        self.profileImage.sd_imageIndicator = indicator
        DispatchQueue.global(qos: .userInteractive).async {
            self.profileImage.sd_setImage(with: imageUrl, placeholderImage: UIImage(named: ""))
        }
        self.userNameLabel.text = object.name ?? ""
        self.callLogLabel.text = (object.status ?? "") + " ● " + (object.time?.getLastSeenString(replace: false) ?? "")
        switch object.call_type {
        case "audio":
            self.callButton.setImage(UIImage(named: "bold_call"), for: .normal)
            switch object.status {
            case "Outgoing":
                self.callLogImageView.image = UIImage(named: "icon_call_phone_outgoing_vector")
            case "Incoming":
                self.callLogImageView.image = UIImage(named: "icon_call_phone_incoming_vector")
            case "Missing":
                self.callLogImageView.image = UIImage(named: "icon_call_phone_missed_vector")
            default:
                break
            }
        case "video":
            self.callButton.setImage(UIImage(named: "bold_video"), for: .normal)
            switch object.status {
            case "Outgoing":
                self.callLogImageView.image = UIImage(named: "icon_call_video_outgoing_vector")
            case "Incoming":
                self.callLogImageView.image = UIImage(named: "icon_call_video_incoming_vector")
            case "Missing":
                self.callLogImageView.image = UIImage(named: "icon_call_video_missed_vector")
            default:
                break
            }
        default:
            break
        }
    }
    
    // Call Button Action
    @IBAction func callButtonAction(_ sender: UIButton) {
        self.delegate?.handleCallButtonTap(indexPath: self.indexPath)
    }
    
}
