package com.example.sae41_2025;

import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import com.example.sae41_2025.Case;
import com.example.exo1.CaseGestureListener;
import android.util.Log;

public class CaseOnTouchListener implements View.OnTouchListener {

    private Case uneCase;
    private GameActivity gameAcitivity;
    private GestureDetector gestureDetector;
    private Boolean isHorizontal = null;
    private Direction direction = null;

    private int ligneCaseToucheeActuelle;
    private int colonneCaseToucheeActuelle;

    private int[] resIdsSauvegardes = null;
    private boolean[] bloquesSauvegardes = null;
    private boolean isSauvegarde = false;

    public CaseOnTouchListener(GameActivity context, Case c) {
        this.uneCase = c;
        this.gameAcitivity = context;
        this.gestureDetector = new GestureDetector(context, new CaseGestureListener(this));
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        int action = event.getActionMasked();
        
        // animation : 
        // garder letat initial de la ligne/col
        // choper le decalage quand il change et l'appliquer 
        // appliquer -> revenir a l'etat inital et mettre le decalage
        // tres utile pour anim et pour plus tard si on doit annuler des coups car non respect regle

        if (action == MotionEvent.ACTION_DOWN) {
            this.isHorizontal = null;
            this.direction = null;
            this.isSauvegarde = false;
            this.resIdsSauvegardes = null;
            this.bloquesSauvegardes = null;
        } else if (action == MotionEvent.ACTION_MOVE) {
            if (this.isHorizontal != null) {
                if (!this.isSauvegarde) {
                    if (this.isHorizontal) {
                        this.resIdsSauvegardes = this.gameAcitivity.getLigneResIds(this.uneCase.getMyX());
                        this.bloquesSauvegardes = this.gameAcitivity.getLigneBloques(this.uneCase.getMyX());
                    } else {
                        this.resIdsSauvegardes = this.gameAcitivity.getColonneResIds(this.uneCase.getMyY());
                        this.bloquesSauvegardes = this.gameAcitivity.getColonneBloques(this.uneCase.getMyY());
                    }
                    this.isSauvegarde = true;
                }
                if (this.isHorizontal) {
                    int decalage = this.colonneCaseToucheeActuelle - this.uneCase.getMyY();
                    this.gameAcitivity.restoreLigne(this.uneCase.getMyX(), this.resIdsSauvegardes, this.bloquesSauvegardes);
                    this.gameAcitivity.decalerLigne(this.uneCase.getMyX(), decalage);
                } else {
                    int decalage = this.ligneCaseToucheeActuelle - this.uneCase.getMyX();
                    this.gameAcitivity.restoreColonne(this.uneCase.getMyY(), this.resIdsSauvegardes, this.bloquesSauvegardes);
                    this.gameAcitivity.decalerColonne(this.uneCase.getMyY(), decalage);
                }
            }
        } else if (action == MotionEvent.ACTION_UP) {
            if (this.isHorizontal != null && this.isHorizontal) {
                int decalage = this.colonneCaseToucheeActuelle - this.uneCase.getMyY();
                if (this.isSauvegarde) 
                    this.gameAcitivity.restoreLigne(this.uneCase.getMyX(), this.resIdsSauvegardes, this.bloquesSauvegardes);
                this.gameAcitivity.decalerLigne(this.uneCase.getMyX(), decalage);
            } else if (this.isHorizontal != null) {
                int decalage = this.ligneCaseToucheeActuelle - this.uneCase.getMyX();
                if (this.isSauvegarde) 
                    this.gameAcitivity.restoreColonne(this.uneCase.getMyY(), this.resIdsSauvegardes, this.bloquesSauvegardes);
                this.gameAcitivity.decalerColonne(this.uneCase.getMyY(), decalage);
            }
            this.gameAcitivity.effacerTousSurlignements();
        } else if (action == MotionEvent.ACTION_CANCEL) {
            if (this.isSauvegarde) {
                if (this.isHorizontal != null && this.isHorizontal)
                    this.gameAcitivity.restoreLigne(this.uneCase.getMyX(), this.resIdsSauvegardes, this.bloquesSauvegardes);
                else if (this.isHorizontal != null)
                    this.gameAcitivity.restoreColonne(this.uneCase.getMyY(), this.resIdsSauvegardes, this.bloquesSauvegardes);
            }
            this.gameAcitivity.effacerTousSurlignements();
        }
        
        return this.gestureDetector.onTouchEvent(event);
    }

    public void updatePosition(MotionEvent e2) {
        int col = this.uneCase.getMyY() + (int) Math.floor(e2.getX() / this.uneCase.getCaseWidth());
        int ligne = this.uneCase.getMyX() + (int) Math.floor(e2.getY() / this.uneCase.getCaseHeight());

        this.colonneCaseToucheeActuelle = Math.max(0, Math.min(col, this.gameAcitivity.getNbCols() - 1));
        this.ligneCaseToucheeActuelle = Math.max(0, Math.min(ligne, this.gameAcitivity.getNbLignes() - 1));
    }

    public void setOrientation(float distanceX, float distanceY) {
        if (this.isHorizontal == null) {
            boolean horizontal = Math.abs(distanceX) > Math.abs(distanceY);
            if (horizontal && this.gameAcitivity.ligneEstBloquee(this.uneCase.getMyX())) 
                return;
            if (!horizontal && this.gameAcitivity.colonneEstBloquee(this.uneCase.getMyY())) 
                return;
            this.isHorizontal = horizontal;
            if (horizontal) 
                this.gameAcitivity.surlignerLigne(this.uneCase.getMyX());
            else 
                this.gameAcitivity.surlignerColonne(this.uneCase.getMyY());
        }

        if (this.isHorizontal) {
            this.direction = distanceX > 0 ? Direction.GAUCHE : Direction.DROITE;
        } else {
            this.direction = distanceY > 0 ? Direction.HAUT : Direction.BAS;
        }
        debugAffichage();
    }

    public void debugAffichage() {
        Log.d("blou", "Direction: " + this.direction + ", Ligne: " + this.ligneCaseToucheeActuelle + ", Colonne: " + this.colonneCaseToucheeActuelle);
    }
}