//
//  RightTextTableViewCell.swift
//  WoWonder
//
//  Created by Mac on 09/11/22.
//  Copyright © 2022 ScriptSun. All rights reserved.
//

import UIKit
import SkeletonView
import LinkPresentation

class RightTextTableViewCell: SwipeToReplyCell {
    
    @IBOutlet weak var cellView: UIView!
    @IBOutlet weak var stackView: UIStackView!
    @IBOutlet weak var messageView: UIView!
    @IBOutlet weak var forwardedView: UIView!
    @IBOutlet weak var forwardedLabel: UILabel!
    @IBOutlet weak var replyStoryView: UIView!
    @IBOutlet weak var replyStoryBorderView: UIView!
    @IBOutlet weak var replyStoryTitleLabel: UILabel!
    @IBOutlet weak var replyStoryMessageLabel: UILabel!
    @IBOutlet weak var replyStoryImageView: UIImageView!
    @IBOutlet weak var reactionImageView: UIImageView!
    @IBOutlet weak var replyMessageView: UIView!
    @IBOutlet weak var replyUserLabel: UILabel!
    @IBOutlet weak var replyMessageLabel: UILabel!
    @IBOutlet weak var messaageTextLabel: UILabel!
    @IBOutlet weak var messageTimeLabel: UILabel!
    @IBOutlet weak var lastSeenImage: UIImageView!
    @IBOutlet weak var starImageVIew: UIImageView!
    
    var linkView = LPLinkView()
        
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
        self.messageView.clipsToBounds = true
        self.messageView.layer.cornerRadius = 16
        self.messageView.layer.maskedCorners = [.layerMaxXMaxYCorner, .layerMinXMaxYCorner, .layerMinXMinYCorner]
        self.messaageTextLabel.showAnimatedSkeleton()
        
        if let index = self.stackView.arrangedSubviews.firstIndex(of: self.replyMessageView) {
            self.stackView.insertArrangedSubview(self.linkView, at: index + 1)
            self.linkView.isHidden = true
        }
    }
    
    override func layoutSubviews() {
        self.stackView.layoutIfNeeded()
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        // Configure the view for the selected state
    }
    
    func hideAnimation() {
        self.messaageTextLabel.hideSkeleton()
    }
    
    func setUPLinkView(messageString: String, tableView: UITableView) {
        if let url = self.isStringContainsURL(messageString) {
            self.fetchMetaDetaProvider(urlString: url.absoluteString) { metadata in
                DispatchQueue.main.async {
                    if let metadata = metadata {
                        self.linkView.metadata = metadata
                        self.linkView.sizeToFit()
                        self.linkView.isHidden = false
                        tableView.beginUpdates()
                        tableView.endUpdates()
                        DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
                            self.contentView.layoutIfNeeded()
                            tableView.layoutIfNeeded()
                            tableView.beginUpdates()
                            tableView.endUpdates()
                        }
                    } else {
                        self.linkView.metadata = LPLinkMetadata()
                        self.linkView.sizeToFit()
                        self.linkView.isHidden = true
                    }
                }
            }
        } else {
            self.linkView.metadata = LPLinkMetadata()
            self.linkView.sizeToFit()
            self.linkView.isHidden = true
        }
    }
    
}
