import UIKit
import Async

protocol ManageSessionTableItemDelegate {
    func handleDeleteButtonTap(_ sender: UIButton)
}

class ManageSessionTableItem: UITableViewCell {
    
    @IBOutlet weak var bgView: UIView!
    @IBOutlet weak var alphaLabel: UILabel!
    @IBOutlet weak var lastSeenlabel: UILabel!
    @IBOutlet weak var browserLabel: UILabel!
    @IBOutlet weak var phoneLabel: UILabel!
    @IBOutlet weak var deleteButton: UIButton!
    
    var object : SessionModel.Datum?
    var singleCharacter: String?
    var delegate: ManageSessionTableItemDelegate?
    
    override func awakeFromNib() {
        super.awakeFromNib()
        
    }
    
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        
    }
    
    @IBAction func deleteButtonAction(_ sender: UIButton) {
        self.delegate?.handleDeleteButtonTap(sender)
    }
    
    func bind(_ object: SessionModel.Datum) {
        self.object = object
        self.phoneLabel.text = "\(object.platform ?? "")"
        self.browserLabel.text = "Browser : \(object.browser ?? "")"
        self.lastSeenlabel.text = "Last seen : \(object.time ?? "")"
        if object.browser == nil {
            self.alphaLabel.text = self.singleCharacter ?? ""
        } else {
            for (index, value) in (object.browser?.enumerated())! {
                if index == 0 {
                    self.singleCharacter = String(value)
                    break
                }
            }
            self.alphaLabel.text = self.singleCharacter ?? ""
        }
    }
    
}
