//
//  SpeakPracticeReviewCell.swift
//  SpeakEasyLearnEnglish
//
//  Created by edy on 2025/7/23.
//

import UIKit

class SpeakPracticeReviewCell: UICollectionViewCell {

    
    @IBOutlet var SpeakLeftTips: UIView!
    @IBOutlet weak var SpeakLeftLabel: UILabel!
    
    @IBOutlet var SpeakRightTips: UIView!
    @IBOutlet weak var SpeakRightLabel: UILabel!
    
    static let id = "SpeakPracticeReviewCell"
    enum SpeakPracticeReviewState {
        case left
        case right
        case none
    }
    
    private var beginP = CGPoint()
    var SpeakCallblack: ((_ state:SpeakPracticeReviewState)-> Void)?
    
    var data:String = "" {
        didSet{
            Word.SpeakPracticeWord.text = data
        }
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        self.gesture(target: self, class: UIPanGestureRecognizer.self, selector: #selector(panGesture(_:)))
        Word.snp.makeConstraints { make in
            make.left.right.bottom.top.equalToSuperview()
        }
        SpeakLeftLabel.contentInsets = UIEdgeInsets(top: 3, left: 10, bottom: 3, right: 10)
        SpeakRightLabel.contentInsets = UIEdgeInsets(top: 3, left: 10, bottom: 3, right: 10)
        SpeakLeftTips.isHidden = true
        SpeakRightTips.isHidden = true
    }

    override func layoutSubviews() {
        super.layoutSubviews()
        contentView.bringSubviewToFront(SpeakLeftTips)
        contentView.bringSubviewToFront(SpeakRightTips)
    }
    
    private let leftMax:Double = -70
    private let rightMax:Double = 70
    
    @objc private func panGesture(_ recognizer:UIGestureRecognizer) -> Void {
        let p = recognizer.location(in: self)
        if recognizer.state == .began {
            beginP = p
        }else if recognizer.state == .cancelled || recognizer.state == .ended || recognizer.state == .failed {
            SpeakLeftTips.isHidden = true
            SpeakRightTips.isHidden = true
            if self.frame.origin.x < leftMax {
                if let call = SpeakCallblack {
                    call(.left)
                }
            }else if self.frame.origin.x > rightMax {
                if let call = SpeakCallblack {
                    call(.right)
                }
            }else{
                UIView.animate(withDuration: 0.2) {
                    self.frame = self.bounds
                }
            }
        }else {
            let newP:CGPoint = CGPoint(x: p.x - beginP.x, y: p.y - beginP.y)
            let bound = self.frame
            self.frame = CGRectMake(bound.origin.x + newP.x ,bound.origin.y + newP.y, bound.width, bound.height)
            guard let call = callblack else { return }
            if self.frame.origin.x < leftMax {
                call(SpeakPracticeReviewState.left)
                SpeakLeftTips.isHidden = false
                SpeakRightTips.isHidden = true
            }else if self.frame.origin.x > rightMax {
                call(SpeakPracticeReviewState.right)
                SpeakLeftTips.isHidden = true
                SpeakRightTips.isHidden = false
            }else{
                call(SpeakPracticeReviewState.none)
                SpeakLeftTips.isHidden = true
                SpeakRightTips.isHidden = true
            }
        }
    }
    
    private lazy var Word: SpeakPracticeWordView = {
        let w = SpeakPracticeWordView()
        contentView.addSubview(w)
        return w
    }()
    
    deinit {
        SpeakCallblack = nil
    }
}
