//
//  SwipeToReplyCell.swift
//  WoWonder
//
//  Created by iMac on 18/07/23.
//  Copyright © 2023 ScriptSun. All rights reserved.
//

import UIKit

protocol SwipeToReplyCellDelegate {
    func handleSwipeToReply(indexPath: IndexPath)
}

class SwipeToReplyCell: UITableViewCell {
    
    var initialCenter = CGPoint()
    var deleteOnDragRelease = false
    var replyButton = UIButton(type: .custom)
    var delegate: SwipeToReplyCellDelegate?
    var indexPath = IndexPath(row: 0, section: 0)
    var isColor = ""
    var swipeRecognizer = UIPanGestureRecognizer()
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
        self.swipeRecognizer = UIPanGestureRecognizer(target: self, action: #selector(handlePan(recognizer:)))
        self.swipeRecognizer.delegate = self
        self.contentView.addGestureRecognizer(self.swipeRecognizer)
    }
    
    override func layoutSubviews() {
        self.contentView.clipsToBounds = false
        replyButton.iscircleCorner = true
        replyButton.clipsToBounds = true
        if isColor != "" {
            replyButton.backgroundColor = UIColor(hex: isColor)
        } else {
            replyButton.backgroundColor = UIColor(hex: "C83747")
        }
        replyButton.contentEdgeInsets = UIEdgeInsets(top: 6, left: 6, bottom: 6, right: 6)
        replyButton.setImage(UIImage(named: "icon_chat_reply"), for: .normal)
        replyButton.tintColor = .white
        replyButton.frame = CGRect(x: -36, y: 0, width: 24, height: 24)
        replyButton.center.y = ((self.contentView.frame.height - 8 - 26) / 2) + 8
        contentView.addSubview(replyButton)
    }

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

        // Configure the view for the selected state
    }
    
    @objc func handlePan(recognizer: UIPanGestureRecognizer) {
        if recognizer.state == .began {
            initialCenter = contentView.center
        }
        if recognizer.state == .changed {
            let translation = recognizer.translation(in: contentView)
            if 120 > contentView.frame.origin.x {
                let newX = initialCenter.x + translation.x
                contentView.center = CGPoint(x: max(initialCenter.x, newX), y: initialCenter.y)
            }
            // deleteOnDragRelease = newX < initialCenter.x
            deleteOnDragRelease = contentView.frame.origin.x < 40            
        }
        if recognizer.state == .ended {
            let originalFrame = CGRect(x: 0, y: contentView.frame.origin.y, width: contentView.bounds.size.width, height: contentView.bounds.size.height)
            if !deleteOnDragRelease {
                print("Swiped Left to Right >>>>> ")
                self.delegate?.handleSwipeToReply(indexPath: self.indexPath)
                UIView.animate(withDuration: 0.2, animations: {
                    self.contentView.frame = originalFrame
                })
            } else {
                UIView.animate(withDuration: 0.2, animations: {
                    self.contentView.frame = originalFrame
                })
                /*self.delegate?.deleteCell(self)*/
            }
        }
    }
    
    override func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
        if let panGestureRecognizer = gestureRecognizer as? UIPanGestureRecognizer {
            let translation = panGestureRecognizer.translation(in: superview!)
            if abs(translation.x) > abs(translation.y) {
                return true
            }
            return false
        }
        return false
    }

}
