package com.example.sae41_2025;

import android.content.Context;
import android.graphics.Color;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.view.View;

public class Case extends RelativeLayout {

    private ImageView tileImageView;
    private int tileResId;
    private ImageView cadenaImageView;
    private View surlignageView;
    private boolean bloque = false;
    private int x;
    private int y;
    private int caseWidth;
    private int caseHeight;

    public Case(Context context, int imageResId, int x, int y) {
        super(context);
        this.x = x;
        this.y = y;
        this.setSurlignageView(context);
        this.setTileImage(context, imageResId);
        this.setCadenaImage(context);
    }   

    private void setTileImage(Context context, int imageResId) {
        this.tileResId = imageResId;
        this.tileImageView = new ImageView(context);
        this.tileImageView.setImageResource(imageResId);
        this.tileImageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
        RelativeLayout.LayoutParams tileParams = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
        this.addView(this.tileImageView, tileParams);
    }

    private void setSurlignageView(Context context) {
        this.surlignageView = new View(context);
        this.surlignageView.setBackgroundColor(Color.argb(120, 255, 220, 0));
        this.surlignageView.setVisibility(View.GONE);
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
        this.addView(this.surlignageView, params);
    }

    private void setCadenaImage(Context context) {
        this.cadenaImageView = new ImageView(context);
        this.cadenaImageView.setImageResource(R.drawable.lock);
        this.cadenaImageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
        this.cadenaImageView.setVisibility(View.GONE);
        RelativeLayout.LayoutParams cadenaParams = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
        cadenaParams.addRule(RelativeLayout.CENTER_IN_PARENT);
        this.addView(this.cadenaImageView, cadenaParams);
    }

    public void setBloque(boolean bloque) {
        this.bloque = bloque;
        this.cadenaImageView.setVisibility(bloque ? View.VISIBLE : View.GONE);
    }

    public boolean isBloque() {
        return this.bloque;
    }

    public CaseEtat toEtat() {
        return new CaseEtat(this.tileResId, this.isBloque());
    }

    public int getMyX() {
        return x;
    }

    public void setMyX(int x) {
        this.x = x;
    }

    public int getMyY() {
        return y;
    }

    public void setMyY(int y) {
        this.y = y;
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        this.caseWidth = w;
        this.caseHeight = h;
    }

    public int getCaseWidth() {
        return this.caseWidth;
    }

    public int getCaseHeight() {
        return this.caseHeight;
    }

    public int getTileResId() {
        return this.tileResId;
    }

    public void updateTile(int imageResId) {
        this.tileResId = imageResId;
        this.tileImageView.setImageResource(imageResId);
    }

    public void setSurlignee(boolean surlignee) {
        this.surlignageView.setVisibility(surlignee ? View.VISIBLE : View.GONE);
    }
}
