package com.example.sae41_2025;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Switch;
import android.widget.TextView;
import android.util.Log;

public class MainActivity extends Activity {

    public static final int INTENT_RESQUESTCODE = 1;
    public static final String GRILLE_ETAT = "grille_etats";

    private boolean difficulte = false;
    private CaseEtat[][] grilleEnCours = null;

    private RelativeLayout menuLayout;

    private TextView textModeDifficile;
    private Switch switchDifficile;

    private LinearLayout btnLancerPartie;
    private LinearLayout btnContinuerPartie;
    private LinearLayout btnOublierPartie;
    private LinearLayout btnLancerSeed;

    private String TAG = "blou";

    public void setDifficulte(boolean difficulte) {
        this.difficulte = difficulte;
    }

    public CaseEtat[][] getGrilleEnCours() {
        return this.grilleEnCours;
    }

    public RelativeLayout getMenuLayout() {
        return this.menuLayout;    
    }

    public Switch getSwitchDifficulte() {
        return this.switchDifficile;
    }

    public void resetGrille() {
        this.grilleEnCours = null;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_menu);

        this.menuLayout = findViewById(R.id.relativeLayout);
        this.switchDifficile = findViewById(R.id.switchModeDifficile);
        this.textModeDifficile = findViewById(R.id.textModeDifficile);
        this.btnLancerPartie = findViewById(R.id.btnLancerPartie);
        this.btnLancerSeed = findViewById(R.id.btnLancerSeed);
        this.btnContinuerPartie = findViewById(R.id.btnContinuerPartie);
        this.btnOublierPartie = findViewById(R.id.btnOublierPartie);

        actualiserBtnMenu();
    }

    @Override
    protected void onResume() {
        super.onResume();
        this.switchDifficile.setOnCheckedChangeListener(new SwitchDifficulteeListener(this));
        this.btnLancerPartie.setOnClickListener(new BtnLancerPartieListener(this));
        this.btnContinuerPartie.setOnClickListener(new BtnContinuerPartieListener(this));
        this.btnOublierPartie.setOnClickListener(new BtnOublierPartieListener(this));
    }

    @Override
    protected void onPause() {
        super.onPause();
        this.switchDifficile.setOnCheckedChangeListener(null);
        this.btnLancerPartie.setOnClickListener(null);
        this.btnContinuerPartie.setOnClickListener(null);
        this.btnOublierPartie.setOnClickListener(null);
    }



    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == INTENT_RESQUESTCODE && resultCode == Activity.RESULT_OK && data != null) {
            this.grilleEnCours = (CaseEtat[][]) data.getSerializableExtra(GRILLE_ETAT);
        }
        Log.d(TAG, "partie en cours recup (onActivityResult)");
        actualiserBtnMenu();
    }

    public void actualiserBtnMenu() {
        if (this.grilleEnCours != null) {
            this.btnContinuerPartie.setVisibility(View.VISIBLE);
            this.btnOublierPartie.setVisibility(View.VISIBLE);
            this.btnLancerPartie.setVisibility(View.GONE);
            this.btnLancerSeed.setVisibility(View.GONE);
            this.textModeDifficile.setVisibility(View.GONE);
            this.switchDifficile.setVisibility(View.GONE);
        } else {
            this.btnContinuerPartie.setVisibility(View.GONE);
            this.btnOublierPartie.setVisibility(View.GONE);
            this.btnLancerPartie.setVisibility(View.VISIBLE);
            this.btnLancerSeed.setVisibility(View.VISIBLE);
            this.textModeDifficile.setVisibility(View.VISIBLE);
            this.switchDifficile.setVisibility(View.VISIBLE);
        }
    }
}
