import UIKit
import Async
import WowonderMessengerSDK
import AVFoundation
import AVKit
import SDWebImage

class MessageInfoViewController: BaseVC {
    
    // MARK: - IBOutlets
    
    @IBOutlet weak var tableView: UITableView!
    
    // MARK: - Properties
    
    var object: Messages?
    var isColor = ""
    
    // MARK: - View Life Cycles
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.initialConfig()
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        AudioPlayerHelper.shared.delegate = self
    }
    
    override func viewDidDisappear(_ animated: Bool) {
        AudioPlayerHelper.shared.stopAudioPlayer()
    }
    
    // MARK: - Selectors
    
    // Back Button Action
    @IBAction func backButtonAction(_ sender: UIButton) {
        self.navigationController?.popViewController(animated: true)
    }
    
    // MARK: - Helper Functions
    
    // Initial Config
    func initialConfig() {
        let objColor = UserDefaults.standard.value(forKey: "ColorTheme")
        self.isColor = objColor as? String ?? ""
        AudioPlayerHelper.shared.delegate = self
        self.tableViewSetup()
    }
    
    private func tableViewSetup() {
        self.tableView.separatorStyle = .none
        self.tableView.dataSource = self
        self.tableView.delegate = self
        self.tableView.register(UINib(resource: R.nib.rightTextTableViewCell), forCellReuseIdentifier: R.reuseIdentifier.rightTextTableViewCell.identifier)
        self.tableView.register(UINib(resource: R.nib.rightImageTableViewCell), forCellReuseIdentifier: R.reuseIdentifier.rightImageTableViewCell.identifier)
        self.tableView.register(UINib(resource: R.nib.rightDocumentTableViewCell), forCellReuseIdentifier: R.reuseIdentifier.rightDocumentTableViewCell.identifier)
        self.tableView.register(UINib(resource: R.nib.rightVideoTableViewCell), forCellReuseIdentifier: R.reuseIdentifier.rightVideoTableViewCell.identifier)
        self.tableView.register(UINib(resource: R.nib.rightGifTableViewCell), forCellReuseIdentifier: R.reuseIdentifier.rightGifTableViewCell.identifier)
        self.tableView.register(UINib(resource: R.nib.rightLocationTableViewCell), forCellReuseIdentifier: R.reuseIdentifier.rightLocationTableViewCell.identifier)
        self.tableView.register(UINib(resource: R.nib.rightAudioTableViewCell), forCellReuseIdentifier: R.reuseIdentifier.rightAudioTableViewCell.identifier)
        self.tableView.register(UINib(resource: R.nib.rightContactTableViewCell), forCellReuseIdentifier: R.reuseIdentifier.rightContactTableViewCell.identifier)
        self.tableView.register(UINib(resource: R.nib.rightStickerCell), forCellReuseIdentifier: R.reuseIdentifier.rightStickerCell.identifier)
        self.tableView.register( UINib(resource: R.nib.chatInfoTableItem), forCellReuseIdentifier: R.reuseIdentifier.chatInfoTableItem.identifier)
    }
    
    func convertDate(dateValue: Double) -> String {
        let date = Date(timeIntervalSince1970: dateValue)
        let dateFormatter = DateFormatter()
        dateFormatter.timeStyle = DateFormatter.Style.short //Set time style
        dateFormatter.dateStyle = DateFormatter.Style.full //Set date style
        dateFormatter.timeZone = .current
        let localDate = dateFormatter.string(from: date)
        return localDate
    }
    
}

extension MessageInfoViewController: UITableViewDelegate, UITableViewDataSource {
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 2
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        guard let objChatList = self.object else { return UITableViewCell() }
        switch indexPath.row {
        case 0:
            switch objChatList.type {
            case "right_text":
                let lat = Double(self.object?.lat ?? "0.0") ?? 0.0
                let long = Double(self.object?.lng ?? "0.0") ?? 0.0
                if lat > 0.0 || long > 0.0 {
                    let cell = self.tableView.dequeueReusableCell(withIdentifier: R.reuseIdentifier.rightLocationTableViewCell.identifier, for: indexPath) as! RightLocationTableViewCell
                    self.setRightLocationData(lat: lat, long: long, cell: cell, objChatList: objChatList)
                    return cell
                } else {
                    let cell = self.tableView.dequeueReusableCell(withIdentifier: R.reuseIdentifier.rightTextTableViewCell.identifier, for: indexPath) as! RightTextTableViewCell
                    self.setRightTextData(cell: cell, objChatList: objChatList)
                    return cell
                }
            case "right_image":
                let cell = self.tableView.dequeueReusableCell(withIdentifier: R.reuseIdentifier.rightImageTableViewCell.identifier, for: indexPath) as! RightImageTableViewCell
                self.setRightImageData(cell: cell, objChatList: objChatList)
                return cell
            case "right_video":
                let cell = self.tableView.dequeueReusableCell(withIdentifier: R.reuseIdentifier.rightVideoTableViewCell.identifier, for: indexPath) as! RightVideoTableViewCell
                self.setRightVideoData(cell: cell, objChatList: objChatList)
                return cell
            case "right_gif":
                let cell = self.tableView.dequeueReusableCell(withIdentifier: R.reuseIdentifier.rightGifTableViewCell.identifier, for: indexPath) as! RightGifTableViewCell
                self.setRightGifData(cell: cell, objChatList: objChatList)
                return cell
            case "right_contact":
                let cell = self.tableView.dequeueReusableCell(withIdentifier: R.reuseIdentifier.rightContactTableViewCell.identifier, for: indexPath) as! RightContactTableViewCell
                self.setRightContactData(cell: cell, objChatList: objChatList)
                return cell
            case "right_file":
                let cell = self.tableView.dequeueReusableCell(withIdentifier: R.reuseIdentifier.rightDocumentTableViewCell.identifier, for: indexPath) as! RightDocumentTableViewCell
                self.setRightDocumentData(cell: cell, objChatList: objChatList)
                return cell
            case "right_audio":
                let cell = self.tableView.dequeueReusableCell(withIdentifier: R.reuseIdentifier.rightAudioTableViewCell.identifier, for: indexPath) as! RightAudioTableViewCell
                self.setRightAudioData(cell: cell, objChatList: objChatList)
                return cell
            case "right_sticker":
                let cell = self.tableView.dequeueReusableCell(withIdentifier: R.reuseIdentifier.rightStickerCell.identifier, for: indexPath) as! RightStickerCell
                self.setRightStickerData(cell: cell, objChatList: objChatList)
                return cell
            default:
                return UITableViewCell()
            }
        case 1:
            let cell = self.tableView.dequeueReusableCell(withIdentifier: R.reuseIdentifier.chatInfoTableItem.identifier, for: indexPath) as! ChatInfoTableItem
            if objChatList.seen == "0" {
                cell.readLabel.text = "---"
            } else {
                let value = Double(objChatList.seen ?? "")
                cell.readLabel.text = convertDate(dateValue: value ?? 0.0)
            }
            if objChatList.time == "0" {
                cell.deliverLabel.text = "---"
            } else {
                let value = Double(objChatList.time ?? "")
                cell.deliverLabel.text = convertDate(dateValue: value ?? 0.0)
            }
            return cell
        default:
            return UITableViewCell()
        }
    }
    
    // Set Right Text Data
    private func setRightTextData(cell: RightTextTableViewCell, objChatList: Messages) {
        cell.swipeRecognizer.isEnabled = false
        cell.forwardedView.isHidden = objChatList.forward == "0"
        if objChatList.story != nil {
            cell.replyStoryView.isHidden = false
            cell.replyStoryTitleLabel.text = "Story"
            if objChatList.story?.video == nil {
                let imageUrl = URL(string: objChatList.story?.thumbnail ?? "")
                let indicator = SDWebImageActivityIndicator.medium
                cell.replyStoryImageView.sd_imageIndicator = indicator
                DispatchQueue.global(qos: .userInteractive).async {
                    cell.replyStoryImageView.sd_setImage(with: imageUrl, placeholderImage: UIImage(named: ""))
                }
                cell.replyStoryMessageLabel.text = "Image"
            } else {
                if let videoURL = URL(string: objChatList.story?.video?.filename ?? "") {
                    cell.replyStoryImageView.image = UIImage(named: "")
                    DispatchQueue.global(qos: .userInteractive).async {
                        self.generateThumbnail(from: videoURL, completion: { image in
                            DispatchQueue.main.async {
                                if let image = image {
                                    cell.replyStoryImageView.image = image
                                } else {
                                    cell.replyStoryImageView.image = UIImage(named: "")
                                }
                            }
                        })
                    }
                }
                cell.replyStoryMessageLabel.text = "Video"
            }
        } else {
            cell.replyStoryView.isHidden = true
            cell.replyStoryTitleLabel.text = ""
            cell.replyStoryMessageLabel.text = ""
        }
        if objChatList.reply != nil {
            cell.replyMessageView.isHidden = false
            if objChatList.reply?.from_id == AppInstance.instance._userId {
                cell.replyUserLabel.text = "You"
            } else {
                cell.replyUserLabel.text = AppInstance.instance.userProfile?.name
            }
            if objChatList.reply?._type == "right_text" || objChatList.reply?._type == "left_text" {
                if (Double(objChatList.reply?.lat ?? "0.0") ?? 0.0) > 0.0 || Double(objChatList.reply?.lng ?? "0.0") ?? 0.0 > 0.0 {
                    cell.replyMessageLabel.text = "Location"
                } else {
                    cell.replyMessageLabel.text = self.decryptionAESModeECB(messageData: objChatList.reply?._text ?? "", key: objChatList.reply?._time ?? "") ?? ""
                }
            } else if objChatList.reply?._type == "right_image" || objChatList.reply?._type == "left_image" {
                cell.replyMessageLabel.text = "Image"
            } else if objChatList.reply?._type == "right_audio" || objChatList.reply?._type == "left_audio" {
                cell.replyMessageLabel.text = "Audio"
            } else if objChatList.reply?._type == "right_video" || objChatList.reply?._type == "left_video" {
                cell.replyMessageLabel.text = "Video"
            } else if objChatList.reply?._type == "right_file" || objChatList.reply?._type == "left_file" {
                cell.replyMessageLabel.text = "File"
            } else if objChatList.reply?._type == "right_contact" || objChatList.reply?._type == "left_contact" {
                cell.replyMessageLabel.text = "Contact"
            } else if objChatList.reply?._type == "right_sticker" || objChatList.reply?._type == "left_sticker" {
                cell.replyMessageLabel.text = "Sticker"
            } else if objChatList.reply?._type == "right_gif" || objChatList.reply?._type == "left_gif" {
                cell.replyMessageLabel.text = "Gif"
            }
        } else {
            cell.replyMessageView.isHidden = true
        }
        cell.hideAnimation()
        let messageString = "\(self.decryptionAESModeECB(messageData: objChatList._text, key: objChatList._time) ?? objChatList._text)"
        cell.messaageTextLabel.text = "\(messageString)"
        cell.messageTimeLabel.text = objChatList.time?.timeStampStringToTime()
        if objChatList._reaction == "" {
            cell.reactionImageView.isHidden = true
        } else {
            cell.reactionImageView.isHidden = false
            cell.reactionImageView.image = self.setReactionEmoji(type: objChatList._reaction)
        }
        if objChatList.fav == "yes" {
            cell.starImageVIew.isHidden = false
        } else {
            cell.starImageVIew.isHidden = true
        }
        if isColor != "" {
            cell.messageView.backgroundColor = UIColor(hex: isColor)
        } else {
            cell.messageView.backgroundColor = UIColor(hex: "#C83747")
        }
        if objChatList.seen == "0" {
            cell.lastSeenImage.image = UIImage(named: "ic_seenCheck")?.withRenderingMode(.alwaysTemplate)
            if isColor != "" {
                cell.lastSeenImage.tintColor = UIColor(hex: isColor)
            } else {
                cell.lastSeenImage.tintColor = UIColor(hex: "C83747")
            }
        } else {
            cell.lastSeenImage.image = UIImage(named: "ic_seen")?.withRenderingMode(.alwaysTemplate)
            if isColor != "" {
                cell.lastSeenImage.tintColor = UIColor(hex: isColor)
            } else {
                cell.lastSeenImage.tintColor = UIColor(hex: "C83747")
            }
        }
    }
    
    // Set Right Location Data
    private func setRightLocationData(lat: Double, long: Double, cell: RightLocationTableViewCell, objChatList: Messages) {
        cell.swipeRecognizer.isEnabled = false
        cell.forwardedView.isHidden = objChatList.forward == "0"
        if objChatList.story != nil {
            cell.replyStoryView.isHidden = false
            cell.replyStoryTitleLabel.text = "Story"
            if objChatList.story?.video == nil {
                let imageUrl = URL(string: objChatList.story?.thumbnail ?? "")
                let indicator = SDWebImageActivityIndicator.medium
                cell.replyStoryImageView.sd_imageIndicator = indicator
                DispatchQueue.global(qos: .userInteractive).async {
                    cell.replyStoryImageView.sd_setImage(with: imageUrl, placeholderImage: UIImage(named: ""))
                }
                cell.replyStoryMessageLabel.text = "Image"
            } else {
                if let videoURL = URL(string: objChatList.story?.video?.filename ?? "") {
                    cell.replyStoryImageView.image = UIImage(named: "")
                    DispatchQueue.global(qos: .userInteractive).async {
                        self.generateThumbnail(from: videoURL, completion: { image in
                            DispatchQueue.main.async {
                                if let image = image {
                                    cell.replyStoryImageView.image = image
                                } else {
                                    cell.replyStoryImageView.image = UIImage(named: "")
                                }
                            }
                        })
                    }
                }
                cell.replyStoryMessageLabel.text = "Video"
            }
        } else {
            cell.replyStoryView.isHidden = true
            cell.replyStoryTitleLabel.text = ""
            cell.replyStoryMessageLabel.text = ""
        }
        if objChatList.reply != nil {
            cell.replyMessageView.isHidden = false
            if objChatList.reply?.from_id == AppInstance.instance._userId {
                cell.replyUserLabel.text = "You"
            } else {
                cell.replyUserLabel.text = AppInstance.instance.userProfile?.name
            }
            if objChatList.reply?._type == "right_text" || objChatList.reply?._type == "left_text" {
                if (Double(objChatList.reply?.lat ?? "0.0") ?? 0.0) > 0.0 || Double(objChatList.reply?.lng ?? "0.0") ?? 0.0 > 0.0 {
                    cell.replyMessageLabel.text = "Location"
                } else {
                    cell.replyMessageLabel.text = self.decryptionAESModeECB(messageData: objChatList.reply?._text ?? "", key: objChatList.reply?._time ?? "") ?? ""
                }
            } else if objChatList.reply?._type == "right_image" || objChatList.reply?._type == "left_image" {
                cell.replyMessageLabel.text = "Image"
            } else if objChatList.reply?._type == "right_audio" || objChatList.reply?._type == "left_audio" {
                cell.replyMessageLabel.text = "Audio"
            } else if objChatList.reply?._type == "right_video" || objChatList.reply?._type == "left_video" {
                cell.replyMessageLabel.text = "Video"
            } else if objChatList.reply?._type == "right_file" || objChatList.reply?._type == "left_file" {
                cell.replyMessageLabel.text = "File"
            } else if objChatList.reply?._type == "right_contact" || objChatList.reply?._type == "left_contact" {
                cell.replyMessageLabel.text = "Contact"
            } else if objChatList.reply?._type == "right_sticker" || objChatList.reply?._type == "left_sticker" {
                cell.replyMessageLabel.text = "Sticker"
            } else if objChatList.reply?._type == "right_gif" || objChatList.reply?._type == "left_gif" {
                cell.replyMessageLabel.text = "Gif"
            }
        } else {
            cell.replyMessageView.isHidden = true
        }
        cell.messageTimeLabel.text = objChatList.time?.timeStampStringToTime()
        cell.configureMapView(lat: lat, long: long)
        if objChatList._reaction == "" {
            cell.reactionImageView.isHidden = true
        } else {
            cell.reactionImageView.isHidden = false
            cell.reactionImageView.image = self.setReactionEmoji(type: objChatList._reaction)
        }
        if objChatList.fav == "yes" {
            cell.starImageView.isHidden = false
        } else {
            cell.starImageView.isHidden = true
        }
        if isColor != "" {
            cell.messageView.backgroundColor = UIColor(hex: isColor)
        } else {
            cell.messageView.backgroundColor = UIColor(hex: "#C83747")
        }
        if objChatList.seen == "0" {
            cell.lastSeenImage.image = UIImage(named: "ic_seenCheck")?.withRenderingMode(.alwaysTemplate)
            if isColor != "" {
                cell.lastSeenImage.tintColor = UIColor(hex: isColor)
            } else {
                cell.lastSeenImage.tintColor = UIColor(hex: "C83747")
            }
        } else {
            cell.lastSeenImage.image = UIImage(named: "ic_seen")?.withRenderingMode(.alwaysTemplate)
            if isColor != "" {
                cell.lastSeenImage.tintColor = UIColor(hex: isColor)
            } else {
                cell.lastSeenImage.tintColor = UIColor(hex: "C83747")
            }
        }
    }
    
    // Set Right Image Data
    private func setRightImageData(cell: RightImageTableViewCell, objChatList: Messages) {
        cell.swipeRecognizer.isEnabled = false
        cell.forwardedView.isHidden = objChatList.forward == "0"
        if objChatList.story != nil {
            cell.replyStoryView.isHidden = false
            cell.replyStoryTitleLabel.text = "Story"
            if objChatList.story?.video == nil {
                let imageUrl = URL(string: objChatList.story?.thumbnail ?? "")
                let indicator = SDWebImageActivityIndicator.medium
                cell.replyStoryImageView.sd_imageIndicator = indicator
                DispatchQueue.global(qos: .userInteractive).async {
                    cell.replyStoryImageView.sd_setImage(with: imageUrl, placeholderImage: UIImage(named: ""))
                }
                cell.replyStoryMessageLabel.text = "Image"
            } else {
                if let videoURL = URL(string: objChatList.story?.video?.filename ?? "") {
                    cell.replyStoryImageView.image = UIImage(named: "")
                    DispatchQueue.global(qos: .userInteractive).async {
                        self.generateThumbnail(from: videoURL, completion: { image in
                            DispatchQueue.main.async {
                                if let image = image {
                                    cell.replyStoryImageView.image = image
                                } else {
                                    cell.replyStoryImageView.image = UIImage(named: "")
                                }
                            }
                        })
                    }
                }
                cell.replyStoryMessageLabel.text = "Video"
            }
        } else {
            cell.replyStoryView.isHidden = true
            cell.replyStoryTitleLabel.text = ""
            cell.replyStoryMessageLabel.text = ""
        }
        if objChatList.reply != nil {
            cell.replyMessageView.isHidden = false
            if objChatList.reply?.from_id == AppInstance.instance._userId {
                cell.replyUserLabel.text = "You"
            } else {
                cell.replyUserLabel.text = AppInstance.instance.userProfile?.name
            }
            if objChatList.reply?._type == "right_text" || objChatList.reply?._type == "left_text" {
                if (Double(objChatList.reply?.lat ?? "0.0") ?? 0.0) > 0.0 || Double(objChatList.reply?.lng ?? "0.0") ?? 0.0 > 0.0 {
                    cell.replyMessageLabel.text = "Location"
                } else {
                    cell.replyMessageLabel.text = self.decryptionAESModeECB(messageData: objChatList.reply?._text ?? "", key: objChatList.reply?._time ?? "") ?? ""
                }
            } else if objChatList.reply?._type == "right_image" || objChatList.reply?._type == "left_image" {
                cell.replyMessageLabel.text = "Image"
            } else if objChatList.reply?._type == "right_audio" || objChatList.reply?._type == "left_audio" {
                cell.replyMessageLabel.text = "Audio"
            } else if objChatList.reply?._type == "right_video" || objChatList.reply?._type == "left_video" {
                cell.replyMessageLabel.text = "Video"
            } else if objChatList.reply?._type == "right_file" || objChatList.reply?._type == "left_file" {
                cell.replyMessageLabel.text = "File"
            } else if objChatList.reply?._type == "right_contact" || objChatList.reply?._type == "left_contact" {
                cell.replyMessageLabel.text = "Contact"
            } else if objChatList.reply?._type == "right_sticker" || objChatList.reply?._type == "left_sticker" {
                cell.replyMessageLabel.text = "Sticker"
            } else if objChatList.reply?._type == "right_gif" || objChatList.reply?._type == "left_gif" {
                cell.replyMessageLabel.text = "Gif"
            }
        } else {
            cell.replyMessageView.isHidden = true
        }
        let imageUrl = URL(string: objChatList.media ?? "")
        cell.imageUrl = imageUrl
        if AppInstance.instance.isDownloadImage == false {
            if let imageURL = imageUrl, let localURL = MediaDownloader.shared.getCachedLocalURL(for: imageURL),
               FileManager.default.fileExists(atPath: localURL.path) {
                cell.loadImage(from: localURL)
                cell.blurView.isHidden = true // Hide download button
            } else {
                let indicator = SDWebImageActivityIndicator.medium
                cell.sendImageView.sd_imageIndicator = indicator
                DispatchQueue.global(qos: .userInteractive).async {
                    cell.sendImageView.sd_setImage(with: imageUrl, placeholderImage: UIImage(named: ""))
                }
            }
        } else {
            cell.blurView.isHidden = true
            let indicator = SDWebImageActivityIndicator.medium
            cell.sendImageView.sd_imageIndicator = indicator
            DispatchQueue.global(qos: .userInteractive).async {
                cell.sendImageView.sd_setImage(with: imageUrl, placeholderImage: UIImage(named: ""))
            }
        }
        cell.messageTimeLabel.text = objChatList.time?.timeStampStringToTime()
        if objChatList._reaction == "" {
            cell.reactionImageView.isHidden = true
        } else {
            cell.reactionImageView.isHidden = false
            cell.reactionImageView.image = self.setReactionEmoji(type: objChatList._reaction)
        }
        if objChatList.fav == "yes" {
            cell.starImageView.isHidden = false
        } else {
            cell.starImageView.isHidden = true
        }
        if isColor != "" {
            cell.messageView.backgroundColor = UIColor(hex: isColor)
        } else {
            cell.messageView.backgroundColor = UIColor(hex: "C83747")
        }
        if objChatList.seen == "0" {
            cell.lastSeenImage.image = UIImage(named: "ic_seenCheck")?.withRenderingMode(.alwaysTemplate)
            if isColor != "" {
                cell.lastSeenImage.tintColor = UIColor(hex: isColor)
            } else {
                cell.lastSeenImage.tintColor = UIColor(hex: "C83747")
            }
        } else {
            cell.lastSeenImage.image = UIImage(named: "ic_seen")?.withRenderingMode(.alwaysTemplate)
            if isColor != "" {
                cell.lastSeenImage.tintColor = UIColor(hex: isColor)
            } else {
                cell.lastSeenImage.tintColor = UIColor(hex: "C83747")
            }
        }
    }
    
    // Set Right Video Data
    private func setRightVideoData(cell: RightVideoTableViewCell, objChatList: Messages) {
        cell.swipeRecognizer.isEnabled = false
        cell.forwardedView.isHidden = objChatList.forward == "0"
        if objChatList.story != nil {
            cell.replyStoryView.isHidden = false
            cell.replyStoryTitleLabel.text = "Story"
            if objChatList.story?.video == nil {
                let imageUrl = URL(string: objChatList.story?.thumbnail ?? "")
                let indicator = SDWebImageActivityIndicator.medium
                cell.replyStoryImageView.sd_imageIndicator = indicator
                DispatchQueue.global(qos: .userInteractive).async {
                    cell.replyStoryImageView.sd_setImage(with: imageUrl, placeholderImage: UIImage(named: ""))
                }
                cell.replyStoryMessageLabel.text = "Image"
            } else {
                if let videoURL = URL(string: objChatList.story?.video?.filename ?? "") {
                    cell.replyStoryImageView.image = UIImage(named: "")
                    DispatchQueue.global(qos: .userInteractive).async {
                        self.generateThumbnail(from: videoURL, completion: { image in
                            DispatchQueue.main.async {
                                if let image = image {
                                    cell.replyStoryImageView.image = image
                                } else {
                                    cell.replyStoryImageView.image = UIImage(named: "")
                                }
                            }
                        })
                    }
                }
                cell.replyStoryMessageLabel.text = "Video"
            }
        } else {
            cell.replyStoryView.isHidden = true
            cell.replyStoryTitleLabel.text = ""
            cell.replyStoryMessageLabel.text = ""
        }
        if objChatList.reply != nil {
            cell.replyMessageView.isHidden = false
            if objChatList.reply?.from_id == AppInstance.instance._userId {
                cell.replyUserLabel.text = "You"
            } else {
                cell.replyUserLabel.text = AppInstance.instance.userProfile?.name
            }
            if (Double(objChatList.reply?.lat ?? "0.0") ?? 0.0) > 0.0 || Double(objChatList.reply?.lng ?? "0.0") ?? 0.0 > 0.0 {
                if (Double(objChatList.lat ?? "0.0") ?? 0.0) > 0.0 || Double(objChatList.lng ?? "0.0") ?? 0.0 > 0.0 {
                    cell.replyMessageLabel.text = "Location"
                } else {
                    cell.replyMessageLabel.text = self.decryptionAESModeECB(messageData: objChatList.reply?._text ?? "", key: objChatList.reply?._time ?? "") ?? ""
                }
            } else if objChatList.reply?._type == "right_image" || objChatList.reply?._type == "left_image" {
                cell.replyMessageLabel.text = "Image"
            } else if objChatList.reply?._type == "right_audio" || objChatList.reply?._type == "left_audio" {
                cell.replyMessageLabel.text = "Audio"
            } else if objChatList.reply?._type == "right_video" || objChatList.reply?._type == "left_video" {
                cell.replyMessageLabel.text = "Video"
            } else if objChatList.reply?._type == "right_file" || objChatList.reply?._type == "left_file" {
                cell.replyMessageLabel.text = "File"
            } else if objChatList.reply?._type == "right_contact" || objChatList.reply?._type == "left_contact" {
                cell.replyMessageLabel.text = "Contact"
            } else if objChatList.reply?._type == "right_sticker" || objChatList.reply?._type == "left_sticker" {
                cell.replyMessageLabel.text = "Sticker"
            } else if objChatList.reply?._type == "right_gif" || objChatList.reply?._type == "left_gif" {
                cell.replyMessageLabel.text = "Gif"
            }
        } else {
            cell.replyMessageView.isHidden = true
        }
        let videoUrl = URL(string: objChatList.media ?? "")
        cell.videoUrl = videoUrl
        cell.videoImageView.image = UIImage(named: "")
        if AppInstance.instance.isDownloadVideo == false {
            if let videoUrl = videoUrl, let localURL = MediaDownloader.shared.getCachedLocalURL(for: videoUrl),
               FileManager.default.fileExists(atPath: localURL.path) {
                DispatchQueue.global(qos: .userInteractive).async {
                    AppInstance.instance.generateThumbnail(from: localURL, completion: { image in
                        DispatchQueue.main.async {
                            if let image = image {
                                cell.videoImageView.image = image
                            } else {
                                cell.videoImageView.image = UIImage(named: "")
                            }
                        }
                    })
                }
                cell.blurView.isHidden = true // Hide download button
            } else {
                DispatchQueue.global(qos: .userInteractive).async {
                    AppInstance.instance.generateThumbnail(from: videoUrl, completion: { image in
                        DispatchQueue.main.async {
                            if let image = image {
                                cell.videoImageView.image = image
                            } else {
                                cell.videoImageView.image = UIImage(named: "")
                            }
                        }
                    })
                }
            }
        } else {
            cell.blurView.isHidden = true
            DispatchQueue.global(qos: .userInteractive).async {
                AppInstance.instance.generateThumbnail(from: videoUrl, completion: { image in
                    DispatchQueue.main.async {
                        if let image = image {
                            cell.videoImageView.image = image
                        } else {
                            cell.videoImageView.image = UIImage(named: "")
                        }
                    }
                })
            }
        }
        cell.messageTimeLabel.text = objChatList.time?.timeStampStringToTime()
        if objChatList._reaction == "" {
            cell.reactionImageView.isHidden = true
        } else {
            cell.reactionImageView.isHidden = false
            cell.reactionImageView.image = self.setReactionEmoji(type: objChatList._reaction)
        }
        if objChatList.fav == "yes" {
            cell.starImageView.isHidden = false
        } else {
            cell.starImageView.isHidden = true
        }
        if isColor != "" {
            cell.messageView.backgroundColor = UIColor(hex: isColor)
        } else {
            cell.messageView.backgroundColor = UIColor(hex: "C83747")
        }
        if objChatList.seen == "0" {
            cell.lastSeenImage.image = UIImage(named: "ic_seenCheck")?.withRenderingMode(.alwaysTemplate)
            if isColor != "" {
                cell.lastSeenImage.tintColor = UIColor(hex: isColor)
            } else {
                cell.lastSeenImage.tintColor = UIColor(hex: "C83747")
            }
        } else {
            cell.lastSeenImage.image = UIImage(named: "ic_seen")?.withRenderingMode(.alwaysTemplate)
            if isColor != "" {
                cell.lastSeenImage.tintColor = UIColor(hex: isColor)
            } else {
                cell.lastSeenImage.tintColor = UIColor(hex: "C83747")
            }
        }
    }
    
    // Set Right Gif Data
    private func setRightGifData(cell: RightGifTableViewCell, objChatList: Messages) {
        cell.swipeRecognizer.isEnabled = false
        cell.forwardedView.isHidden = objChatList.forward == "0"
        if objChatList.story != nil {
            cell.replyStoryView.isHidden = false
            cell.replyStoryTitleLabel.text = "Story"
            if objChatList.story?.video == nil {
                let imageUrl = URL(string: objChatList.story?.thumbnail ?? "")
                let indicator = SDWebImageActivityIndicator.medium
                cell.replyStoryImageView.sd_imageIndicator = indicator
                DispatchQueue.global(qos: .userInteractive).async {
                    cell.replyStoryImageView.sd_setImage(with: imageUrl, placeholderImage: UIImage(named: ""))
                }
                cell.replyStoryMessageLabel.text = "Image"
            } else {
                if let videoURL = URL(string: objChatList.story?.video?.filename ?? "") {
                    cell.replyStoryImageView.image = UIImage(named: "")
                    DispatchQueue.global(qos: .userInteractive).async {
                        self.generateThumbnail(from: videoURL, completion: { image in
                            DispatchQueue.main.async {
                                if let image = image {
                                    cell.replyStoryImageView.image = image
                                } else {
                                    cell.replyStoryImageView.image = UIImage(named: "")
                                }
                            }
                        })
                    }
                }
                cell.replyStoryMessageLabel.text = "Video"
            }
        } else {
            cell.replyStoryView.isHidden = true
            cell.replyStoryTitleLabel.text = ""
            cell.replyStoryMessageLabel.text = ""
        }
        if objChatList.reply != nil {
            cell.replyMessageView.isHidden = false
            if objChatList.reply?.from_id == AppInstance.instance._userId {
                cell.replyUserLabel.text = "You"
            } else {
                cell.replyUserLabel.text = AppInstance.instance.userProfile?.name
            }
            if (Double(objChatList.reply?.lat ?? "0.0") ?? 0.0) > 0.0 || Double(objChatList.reply?.lng ?? "0.0") ?? 0.0 > 0.0 {
                if (Double(objChatList.lat ?? "0.0") ?? 0.0) > 0.0 || Double(objChatList.lng ?? "0.0") ?? 0.0 > 0.0 {
                    cell.replyMessageLabel.text = "Location"
                } else {
                    cell.replyMessageLabel.text = self.decryptionAESModeECB(messageData: objChatList.reply?._text ?? "", key: objChatList.reply?._time ?? "") ?? ""
                }
            } else if objChatList.reply?._type == "right_image" || objChatList.reply?._type == "left_image" {
                cell.replyMessageLabel.text = "Image"
            } else if objChatList.reply?._type == "right_audio" || objChatList.reply?._type == "left_audio" {
                cell.replyMessageLabel.text = "Audio"
            } else if objChatList.reply?._type == "right_video" || objChatList.reply?._type == "left_video" {
                cell.replyMessageLabel.text = "Video"
            } else if objChatList.reply?._type == "right_file" || objChatList.reply?._type == "left_file" {
                cell.replyMessageLabel.text = "File"
            } else if objChatList.reply?._type == "right_contact" || objChatList.reply?._type == "left_contact" {
                cell.replyMessageLabel.text = "Contact"
            } else if objChatList.reply?._type == "right_sticker" || objChatList.reply?._type == "left_sticker" {
                cell.replyMessageLabel.text = "Sticker"
            } else if objChatList.reply?._type == "right_gif" || objChatList.reply?._type == "left_gif" {
                cell.replyMessageLabel.text = "Gif"
            }
        } else {
            cell.replyMessageView.isHidden = true
        }
        cell.messageTimeLabel.text = objChatList.time?.timeStampStringToTime()
        let gifUrl = URL(string: objChatList.stickers ?? "")
        let indicator = SDWebImageActivityIndicator.medium
        cell.videoImageView.sd_imageIndicator = indicator
        DispatchQueue.global(qos: .userInteractive).async {
            cell.videoImageView.sd_setImage(with: gifUrl, placeholderImage: UIImage(named: ""))
        }
        if objChatList._reaction == "" {
            cell.reactionImageView.isHidden = true
        } else {
            cell.reactionImageView.isHidden = false
            cell.reactionImageView.image = self.setReactionEmoji(type: objChatList._reaction)
        }
        if objChatList.fav == "yes" {
            cell.starImageView.isHidden = false
        } else {
            cell.starImageView.isHidden = true
        }
        if isColor != "" {
            cell.messageView.backgroundColor = UIColor(hex: isColor)
        } else {
            cell.messageView.backgroundColor = UIColor(hex: "C83747")
        }
        if objChatList.seen == "0" {
            cell.lastSeenImage.image = UIImage(named: "ic_seenCheck")?.withRenderingMode(.alwaysTemplate)
            if isColor != "" {
                cell.lastSeenImage.tintColor = UIColor(hex: isColor)
            } else {
                cell.lastSeenImage.tintColor = UIColor(hex: "C83747")
            }
        } else {
            cell.lastSeenImage.image = UIImage(named: "ic_seen")?.withRenderingMode(.alwaysTemplate)
            if isColor != "" {
                cell.lastSeenImage.tintColor = UIColor(hex: isColor)
            } else {
                cell.lastSeenImage.tintColor = UIColor(hex: "C83747")
            }
        }
    }
    
    // Set Right Contact Data
    private func setRightContactData(cell: RightContactTableViewCell, objChatList: Messages) {
        cell.swipeRecognizer.isEnabled = false
        cell.forwardedView.isHidden = objChatList.forward == "0"
        if objChatList.story != nil {
            cell.replyStoryView.isHidden = false
            cell.replyStoryTitleLabel.text = "Story"
            if objChatList.story?.video == nil {
                let imageUrl = URL(string: objChatList.story?.thumbnail ?? "")
                let indicator = SDWebImageActivityIndicator.medium
                cell.replyStoryImageView.sd_imageIndicator = indicator
                DispatchQueue.global(qos: .userInteractive).async {
                    cell.replyStoryImageView.sd_setImage(with: imageUrl, placeholderImage: UIImage(named: ""))
                }
                cell.replyStoryMessageLabel.text = "Image"
            } else {
                if let videoURL = URL(string: objChatList.story?.video?.filename ?? "") {
                    cell.replyStoryImageView.image = UIImage(named: "")
                    DispatchQueue.global(qos: .userInteractive).async {
                        self.generateThumbnail(from: videoURL, completion: { image in
                            DispatchQueue.main.async {
                                if let image = image {
                                    cell.replyStoryImageView.image = image
                                } else {
                                    cell.replyStoryImageView.image = UIImage(named: "")
                                }
                            }
                        })
                    }
                }
                cell.replyStoryMessageLabel.text = "Video"
            }
        } else {
            cell.replyStoryView.isHidden = true
            cell.replyStoryTitleLabel.text = ""
            cell.replyStoryMessageLabel.text = ""
        }
        if objChatList.reply != nil {
            cell.replyMessageView.isHidden = false
            if objChatList.reply?.from_id == AppInstance.instance._userId {
                cell.replyUserLabel.text = "You"
            } else {
                cell.replyUserLabel.text = AppInstance.instance.userProfile?.name
            }
            if (Double(objChatList.reply?.lat ?? "0.0") ?? 0.0) > 0.0 || Double(objChatList.reply?.lng ?? "0.0") ?? 0.0 > 0.0 {
                if (Double(objChatList.lat ?? "0.0") ?? 0.0) > 0.0 || Double(objChatList.lng ?? "0.0") ?? 0.0 > 0.0 {
                    cell.replyMessageLabel.text = "Location"
                } else {
                    cell.replyMessageLabel.text = self.decryptionAESModeECB(messageData: objChatList.reply?._text ?? "", key: objChatList.reply?._time ?? "") ?? ""
                }
            } else if objChatList.reply?._type == "right_image" || objChatList.reply?._type == "left_image" {
                cell.replyMessageLabel.text = "Image"
            } else if objChatList.reply?._type == "right_audio" || objChatList.reply?._type == "left_audio" {
                cell.replyMessageLabel.text = "Audio"
            } else if objChatList.reply?._type == "right_video" || objChatList.reply?._type == "left_video" {
                cell.replyMessageLabel.text = "Video"
            } else if objChatList.reply?._type == "right_file" || objChatList.reply?._type == "left_file" {
                cell.replyMessageLabel.text = "File"
            } else if objChatList.reply?._type == "right_contact" || objChatList.reply?._type == "left_contact" {
                cell.replyMessageLabel.text = "Contact"
            } else if objChatList.reply?._type == "right_sticker" || objChatList.reply?._type == "left_sticker" {
                cell.replyMessageLabel.text = "Sticker"
            } else if objChatList.reply?._type == "right_gif" || objChatList.reply?._type == "left_gif" {
                cell.replyMessageLabel.text = "Gif"
            }
        } else {
            cell.replyMessageView.isHidden = true
        }
        let phoneData = self.decryptionAESModeECB(messageData: objChatList._text, key: objChatList._time) ?? ""
        let dict = htmlToString(text: phoneData)
        print(dict)
        let contactName = ((dict?["Key"] as? String) ?? "")
        let phoneNumber = ((dict?["Value"] as? String) ?? "")
        if contactName != "" {
            cell.contactNameLabel.text = "\(contactName)"
        } else if phoneNumber != "" {
            cell.contactNameLabel.text = "\(phoneNumber)"
        }
        cell.messageTimeLabel.text = objChatList.time?.timeStampStringToTime()
        if objChatList._reaction == "" {
            cell.reactionImageView.isHidden = true
        } else {
            cell.reactionImageView.isHidden = false
            cell.reactionImageView.image = self.setReactionEmoji(type: objChatList._reaction)
        }
        if objChatList.fav == "yes" {
            cell.starImageView.isHidden = false
        } else {
            cell.starImageView.isHidden = true
        }
        if isColor != "" {
            cell.messageView.backgroundColor = UIColor(hex: isColor)
        } else {
            cell.messageView.backgroundColor = UIColor(hex: "C83747")
        }
        if objChatList.seen == "0" {
            cell.lastSeenImage.image = UIImage(named: "ic_seenCheck")?.withRenderingMode(.alwaysTemplate)
            if isColor != "" {
                cell.lastSeenImage.tintColor = UIColor(hex: isColor)
            } else {
                cell.lastSeenImage.tintColor = UIColor(hex: "C83747")
            }
        } else {
            cell.lastSeenImage.image = UIImage(named: "ic_seen")?.withRenderingMode(.alwaysTemplate)
            if isColor != "" {
                cell.lastSeenImage.tintColor = UIColor(hex: isColor)
            } else {
                cell.lastSeenImage.tintColor = UIColor(hex: "C83747")
            }
        }
    }
    
    // Set Right Document Document
    private func setRightDocumentData(cell: RightDocumentTableViewCell, objChatList: Messages) {
        cell.swipeRecognizer.isEnabled = false
        cell.forwardedView.isHidden = objChatList.forward == "0"
        if objChatList.story != nil {
            cell.replyStoryView.isHidden = false
            cell.replyStoryTitleLabel.text = "Story"
            if objChatList.story?.video == nil {
                let imageUrl = URL(string: objChatList.story?.thumbnail ?? "")
                let indicator = SDWebImageActivityIndicator.medium
                cell.replyStoryImageView.sd_imageIndicator = indicator
                DispatchQueue.global(qos: .userInteractive).async {
                    cell.replyStoryImageView.sd_setImage(with: imageUrl, placeholderImage: UIImage(named: ""))
                }
                cell.replyStoryMessageLabel.text = "Image"
            } else {
                if let videoURL = URL(string: objChatList.story?.video?.filename ?? "") {
                    cell.replyStoryImageView.image = UIImage(named: "")
                    DispatchQueue.global(qos: .userInteractive).async {
                        self.generateThumbnail(from: videoURL, completion: { image in
                            DispatchQueue.main.async {
                                if let image = image {
                                    cell.replyStoryImageView.image = image
                                } else {
                                    cell.replyStoryImageView.image = UIImage(named: "")
                                }
                            }
                        })
                    }
                }
                cell.replyStoryMessageLabel.text = "Video"
            }
        } else {
            cell.replyStoryView.isHidden = true
            cell.replyStoryTitleLabel.text = ""
            cell.replyStoryMessageLabel.text = ""
        }
        if objChatList.reply != nil {
            cell.replyMessageView.isHidden = false
            if objChatList.reply?.from_id == AppInstance.instance._userId {
                cell.replyUserLabel.text = "You"
            } else {
                cell.replyUserLabel.text = AppInstance.instance.userProfile?.name
            }
            if objChatList.reply?._type == "right_text" || objChatList.reply?._type == "left_text" {
                if (Double(objChatList.reply?.lat ?? "0.0") ?? 0.0) > 0.0 || Double(objChatList.reply?.lng ?? "0.0") ?? 0.0 > 0.0 {
                    cell.replyMessageLabel.text = "Location"
                } else {
                    cell.replyMessageLabel.text = self.decryptionAESModeECB(messageData: objChatList.reply?._text ?? "", key: objChatList.reply?._time ?? "") ?? ""
                }
            } else if objChatList.reply?._type == "right_image" || objChatList.reply?._type == "left_image" {
                cell.replyMessageLabel.text = "Image"
            } else if objChatList.reply?._type == "right_audio" || objChatList.reply?._type == "left_audio" {
                cell.replyMessageLabel.text = "Audio"
            } else if objChatList.reply?._type == "right_video" || objChatList.reply?._type == "left_video" {
                cell.replyMessageLabel.text = "Video"
            } else if objChatList.reply?._type == "right_file" || objChatList.reply?._type == "left_file" {
                cell.replyMessageLabel.text = "File"
            } else if objChatList.reply?._type == "right_contact" || objChatList.reply?._type == "left_contact" {
                cell.replyMessageLabel.text = "Contact"
            } else if objChatList.reply?._type == "right_sticker" || objChatList.reply?._type == "left_sticker" {
                cell.replyMessageLabel.text = "Sticker"
            } else if objChatList.reply?._type == "right_gif" || objChatList.reply?._type == "left_gif" {
                cell.replyMessageLabel.text = "Gif"
            }
        } else {
            cell.replyMessageView.isHidden = true
        }
        let fileURL = URL(string: objChatList.media ?? "")
        cell.messageTimeLabel.text = objChatList.time?.timeStampStringToTime()
        cell.fileNameLabel.text = fileURL?.lastPathComponent ?? ""
        cell.fileSizeLabel.text = objChatList.file_size
        if objChatList._reaction == "" {
            cell.reactionImageView.isHidden = true
        } else {
            cell.reactionImageView.isHidden = false
            cell.reactionImageView.image = self.setReactionEmoji(type: objChatList._reaction)
        }
        if objChatList.fav == "yes" {
            cell.starImageView.isHidden = false
        } else {
            cell.starImageView.isHidden = true
        }
        if isColor != "" {
            cell.messageView.backgroundColor = UIColor(hex: isColor)
            cell.fileImageView.tintColor = UIColor(hex: isColor)
        } else {
            cell.messageView.backgroundColor = UIColor(hex: "C83747")
            cell.fileImageView.tintColor = UIColor(hex: "C83747")
        }
        if objChatList.seen == "0" {
            cell.lastSeenImage.image = UIImage(named: "ic_seenCheck")?.withRenderingMode(.alwaysTemplate)
            if isColor != "" {
                cell.lastSeenImage.tintColor = UIColor(hex: isColor)
            } else {
                cell.lastSeenImage.tintColor = UIColor(hex: "C83747")
            }
        } else {
            cell.lastSeenImage.image = UIImage(named: "ic_seen")?.withRenderingMode(.alwaysTemplate)
            if isColor != "" {
                cell.lastSeenImage.tintColor = UIColor(hex: isColor)
            } else {
                cell.lastSeenImage.tintColor = UIColor(hex: "C83747")
            }
        }
    }
    
    // Set Right Audio Data
    private func setRightAudioData(cell: RightAudioTableViewCell, objChatList: Messages) {
        cell.swipeRecognizer.isEnabled = false
        cell.forwardedView.isHidden = objChatList.forward == "0"
        if objChatList.story != nil {
            cell.replyStoryView.isHidden = false
            cell.replyStoryTitleLabel.text = "Story"
            if objChatList.story?.video == nil {
                let imageUrl = URL(string: objChatList.story?.thumbnail ?? "")
                let indicator = SDWebImageActivityIndicator.medium
                cell.replyStoryImageView.sd_imageIndicator = indicator
                DispatchQueue.global(qos: .userInteractive).async {
                    cell.replyStoryImageView.sd_setImage(with: imageUrl, placeholderImage: UIImage(named: ""))
                }
                cell.replyStoryMessageLabel.text = "Image"
            } else {
                if let videoURL = URL(string: objChatList.story?.video?.filename ?? "") {
                    cell.replyStoryImageView.image = UIImage(named: "")
                    DispatchQueue.global(qos: .userInteractive).async {
                        self.generateThumbnail(from: videoURL, completion: { image in
                            DispatchQueue.main.async {
                                if let image = image {
                                    cell.replyStoryImageView.image = image
                                } else {
                                    cell.replyStoryImageView.image = UIImage(named: "")
                                }
                            }
                        })
                    }
                }
                cell.replyStoryMessageLabel.text = "Video"
            }
        } else {
            cell.replyStoryView.isHidden = true
            cell.replyStoryTitleLabel.text = ""
            cell.replyStoryMessageLabel.text = ""
        }
        if objChatList.reply != nil {
            cell.replyMessageView.isHidden = false
            if objChatList.reply?.from_id == AppInstance.instance._userId {
                cell.replyUserLabel.text = "You"
            } else {
                cell.replyUserLabel.text = AppInstance.instance.userProfile?.name
            }
            if objChatList.reply?._type == "right_text" || objChatList.reply?._type == "left_text" {
                if (Double(objChatList.reply?.lat ?? "0.0") ?? 0.0) > 0.0 || Double(objChatList.reply?.lng ?? "0.0") ?? 0.0 > 0.0 {
                    cell.replyMessageLabel.text = "Location"
                } else {
                    cell.replyMessageLabel.text = self.decryptionAESModeECB(messageData: objChatList.reply?._text ?? "", key: objChatList.reply?._time ?? "") ?? ""
                }
            } else if objChatList.reply?._type == "right_image" || objChatList.reply?._type == "left_image" {
                cell.replyMessageLabel.text = "Image"
            } else if objChatList.reply?._type == "right_audio" || objChatList.reply?._type == "left_audio" {
                cell.replyMessageLabel.text = "Audio"
            } else if objChatList.reply?._type == "right_video" || objChatList.reply?._type == "left_video" {
                cell.replyMessageLabel.text = "Video"
            } else if objChatList.reply?._type == "right_file" || objChatList.reply?._type == "left_file" {
                cell.replyMessageLabel.text = "File"
            } else if objChatList.reply?._type == "right_contact" || objChatList.reply?._type == "left_contact" {
                cell.replyMessageLabel.text = "Contact"
            } else if objChatList.reply?._type == "right_sticker" || objChatList.reply?._type == "left_sticker" {
                cell.replyMessageLabel.text = "Sticker"
            } else if objChatList.reply?._type == "right_gif" || objChatList.reply?._type == "left_gif" {
                cell.replyMessageLabel.text = "Gif"
            }
        } else {
            cell.replyMessageView.isHidden = true
        }
        if let audioURL = URL(string: objChatList.media ?? "") {
            cell.playButtonTapHandler = {
                AudioPlayerHelper.shared.playAudio(at: IndexPath(row: 0, section: 0), url: audioURL)
            }
            cell.setupAudioPlayer(url: audioURL, indexPath: IndexPath(row: 0, section: 0))
        }
        cell.messageTimeLabel.text = objChatList.time?.timeStampStringToTime()
        if objChatList._reaction == "" {
            cell.reactionImageView.isHidden = true
        } else {
            cell.reactionImageView.isHidden = false
            cell.reactionImageView.image = self.setReactionEmoji(type: objChatList._reaction)
        }
        if objChatList.fav == "yes" {
            cell.starImageView.isHidden = false
        } else {
            cell.starImageView.isHidden = true
        }
        if isColor != "" {
            cell.messageView.backgroundColor = UIColor(hex: isColor)
            cell.playButton.tintColor = UIColor(hex: isColor)
        } else {
            cell.messageView.backgroundColor = UIColor(hex: "C83747")
            cell.playButton.tintColor = UIColor(hex: "C83747")
        }
        if objChatList.seen == "0" {
            cell.lastSeenImage.image = UIImage(named: "ic_seenCheck")?.withRenderingMode(.alwaysTemplate)
            if isColor != "" {
                cell.lastSeenImage.tintColor = UIColor(hex: isColor)
            } else {
                cell.lastSeenImage.tintColor = UIColor(hex: "C83747")
            }
        } else {
            cell.lastSeenImage.image = UIImage(named: "ic_seen")?.withRenderingMode(.alwaysTemplate)
            if isColor != "" {
                cell.lastSeenImage.tintColor = UIColor(hex: isColor)
            } else {
                cell.lastSeenImage.tintColor = UIColor(hex: "C83747")
            }
        }
    }
    
    // Set Right Sticker Data
    private func setRightStickerData(cell: RightStickerCell, objChatList: Messages) {
        cell.swipeRecognizer.isEnabled = false
        cell.forwardedView.isHidden = objChatList.forward == "0"
        if objChatList.story != nil {
            cell.replyStoryView.isHidden = false
            cell.replyStoryTitleLabel.text = "Story"
            if objChatList.story?.video == nil {
                let imageUrl = URL(string: objChatList.story?.thumbnail ?? "")
                let indicator = SDWebImageActivityIndicator.medium
                cell.replyStoryImageView.sd_imageIndicator = indicator
                DispatchQueue.global(qos: .userInteractive).async {
                    cell.replyStoryImageView.sd_setImage(with: imageUrl, placeholderImage: UIImage(named: ""))
                }
                cell.replyStoryMessageLabel.text = "Image"
            } else {
                if let videoURL = URL(string: objChatList.story?.video?.filename ?? "") {
                    cell.replyStoryImageView.image = UIImage(named: "")
                    DispatchQueue.global(qos: .userInteractive).async {
                        self.generateThumbnail(from: videoURL, completion: { image in
                            DispatchQueue.main.async {
                                if let image = image {
                                    cell.replyStoryImageView.image = image
                                } else {
                                    cell.replyStoryImageView.image = UIImage(named: "")
                                }
                            }
                        })
                    }
                }
                cell.replyStoryMessageLabel.text = "Video"
            }
        } else {
            cell.replyStoryView.isHidden = true
            cell.replyStoryTitleLabel.text = ""
            cell.replyStoryMessageLabel.text = ""
        }
        if objChatList.reply != nil {
            cell.replyMessageView.isHidden = false
            if objChatList.reply?.from_id == AppInstance.instance._userId {
                cell.replyUserLabel.text = "You"
            } else {
                cell.replyUserLabel.text = AppInstance.instance.userProfile?.name
            }
            if objChatList.reply?._type == "right_text" || objChatList.reply?._type == "left_text" {
                if (Double(objChatList.reply?.lat ?? "0.0") ?? 0.0) > 0.0 || Double(objChatList.reply?.lng ?? "0.0") ?? 0.0 > 0.0 {
                    cell.replyMessageLabel.text = "Location"
                } else {
                    cell.replyMessageLabel.text = self.decryptionAESModeECB(messageData: objChatList.reply?._text ?? "", key: objChatList.reply?._time ?? "") ?? ""
                }
            } else if objChatList.reply?._type == "right_image" || objChatList.reply?._type == "left_image" {
                cell.replyMessageLabel.text = "Image"
            } else if objChatList.reply?._type == "right_audio" || objChatList.reply?._type == "left_audio" {
                cell.replyMessageLabel.text = "Audio"
            } else if objChatList.reply?._type == "right_video" || objChatList.reply?._type == "left_video" {
                cell.replyMessageLabel.text = "Video"
            } else if objChatList.reply?._type == "right_file" || objChatList.reply?._type == "left_file" {
                cell.replyMessageLabel.text = "File"
            } else if objChatList.reply?._type == "right_contact" || objChatList.reply?._type == "left_contact" {
                cell.replyMessageLabel.text = "Contact"
            } else if objChatList.reply?._type == "right_sticker" || objChatList.reply?._type == "left_sticker" {
                cell.replyMessageLabel.text = "Sticker"
            } else if objChatList.reply?._type == "right_gif" || objChatList.reply?._type == "left_gif" {
                cell.replyMessageLabel.text = "Gif"
            }
        } else {
            cell.replyMessageView.isHidden = true
        }
        let stickerUrl = URL(string: objChatList.media ?? "")
        let indicator = SDWebImageActivityIndicator.medium
        cell.sendImageView.sd_imageIndicator = indicator
        DispatchQueue.global(qos: .userInteractive).async {
            cell.sendImageView.sd_setImage(with: stickerUrl, placeholderImage: UIImage(named: ""))
        }
        cell.messageTimeLabel.text = objChatList.time?.timeStampStringToTime()
        if objChatList._reaction == "" {
            cell.reactionImageView.isHidden = true
        } else {
            cell.reactionImageView.isHidden = false
            cell.reactionImageView.image = self.setReactionEmoji(type: objChatList._reaction)
        }
        if objChatList.fav == "yes" {
            cell.starImageView.isHidden = false
        } else {
            cell.starImageView.isHidden = true
        }
        if objChatList.seen == "0" {
            cell.lastSeenImage.image = UIImage(named: "ic_seenCheck")?.withRenderingMode(.alwaysTemplate)
            if isColor != "" {
                cell.lastSeenImage.tintColor = UIColor(hex: isColor)
            } else {
                cell.lastSeenImage.tintColor = UIColor(hex: "C83747")
            }
        } else {
            cell.lastSeenImage.image = UIImage(named: "ic_seen")?.withRenderingMode(.alwaysTemplate)
            if isColor != "" {
                cell.lastSeenImage.tintColor = UIColor(hex: isColor)
            } else {
                cell.lastSeenImage.tintColor = UIColor(hex: "C83747")
            }
        }
    }
    
}

// MARK: AudioPlayerDelegate Methods
extension MessageInfoViewController: AudioPlayerDelegate {
    
    func playbackStateChanged(at indexPath: IndexPath?) {
        if let indexPath = indexPath {
            self.tableView.reloadRows(at: [indexPath], with: .none)
        }
    }
    
}
