import UIKit
import SDWebImage

protocol ContactCellDelegate {
    func handleCallButtonTap(indexPath: IndexPath, type: String)
}

class ContactCell: UITableViewCell {
    
    @IBOutlet weak var profileImage: UIImageView!
    @IBOutlet weak var userNameLabel: UILabel!
    @IBOutlet weak var verifiedImage: UIImageView!
    @IBOutlet weak var lastSeenLabel: UILabel!
    
    var delegate: ContactCellDelegate?
    var indexPath = IndexPath()
    
    override func awakeFromNib() {
        super.awakeFromNib()
        
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
    
    }
    
    // Set Data
    func setData(object: UserData) {
        self.userNameLabel.text = object.name ?? ""
        self.lastSeenLabel.text = "Last seen " + (object.lastseen_time_text ?? "")
        self.verifiedImage.isHidden = object.is_verified == 0
        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: ""))
        }
    }
    
    // Video Call Button Action
    @IBAction func videoCallButtonAction(_ sender: UIButton) {
        self.delegate?.handleCallButtonTap(indexPath: self.indexPath, type: "video")
    }
    
    // Audio Call Button Action
    @IBAction func audioCallButtonAction(_ sender: UIButton) {
        self.delegate?.handleCallButtonTap(indexPath: self.indexPath, type: "audio")
    }
    
}
