package fanch.multijeu.sudoku.activity;

import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import fanch.multijeu.R;
import fanch.multijeu.sudoku.data.SimpleSudokuResolver;
import fanch.multijeu.sudoku.data.Sudoku;
import fanch.multijeu.sudoku.data.SudokuGenerator;
import fanch.multijeu.sudoku.view.SudokuCase;
import fanch.multijeu.sudoku.view.SudokuOverlay;
import fanch.multijeu.sudoku.view.SudokuView;

public class SudokuActivity extends AppCompatActivity implements SudokuView.SudokuListener {



    public static class Attrs extends SudokuView.Attrs {
        public SudokuOverlay.Attrs overlayAttr = new SudokuOverlay.Attrs();
        public int difficulty=2;
        public boolean canResolve=true;
        public boolean canIndices=true;

    }

    protected final static int DIFFICULTIES[]= {45, 40, 35, 30, 27, 25};

    protected SudokuView mGrid;
    protected SudokuOverlay mOverlay;
    protected Button        mBtnResolve;
    protected Button        mBtnIndices;
    protected Attrs         mAttrs = new Attrs();


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

        mGrid=findViewById(R.id.sudoku_grid);
        mBtnIndices=findViewById(R.id.btn_indices);
        mBtnResolve=findViewById(R.id.btn_reslove);
        mOverlay=findViewById(R.id.sudoku_overlay);

        mGrid.setListener(this);
        mOverlay.setListener(mGrid);

        Attrs a = (Attrs) getIntent().getSerializableExtra("attrs");

        setAttrs(a);
        //OldSudoku s = new OldSudoku("000900001302600000700000960080500400005100007070003009800026040000001690020090700");
        //OldSudoku s = new OldSudoku("403067100000304000092000006000580304050040010806073000100000740000701000008430201");
        //Sudoku s = new Sudoku("..8..94......7....93.2...8.....6.9.8.7.......49..38.71.....6.17....4.89........2.");
        //Sudoku s = new Sudoku("008009400000070000930200080000060908070000000490038071000006017000040890000000020");
       Sudoku s = new SudokuGenerator();
       ((SudokuGenerator) s).generate(DIFFICULTIES[a.difficulty]);
        mGrid.setSudoku(s);

    }

    static void start(Context c)
    {
        Intent in = new Intent(c, SudokuActivity.class);
        c.startActivity(in);
    }

    static void start(Context c, Attrs attrs)
    {
        Intent in = new Intent(c, SudokuActivity.class);
        in.putExtra("attrs", attrs);
        c.startActivity(in);
    }

    @Override
    public void onClick(SudokuCase c, int x, int y) {
        if(c.getState()!= SudokuCase.CaseState.LOCKED &&
            c.getState()!= SudokuCase.CaseState.INDICE)
        {
            mOverlay.click(c, x, y, mGrid.possibleValues(x,y));
        }
    }

    @Override
    public void onSudokuDone(boolean ok) {
        if(ok)
        {
            Toast.makeText(this, "Bien joué !", Toast.LENGTH_SHORT).show();
        }else{
            Toast.makeText(this, "Erreur !", Toast.LENGTH_SHORT).show();
        }
    }

    protected void setAttrs(Attrs a)
    {
        mAttrs=a;
        mGrid.setAttrs(mAttrs);
        mOverlay.setAttrs(mAttrs.overlayAttr);
        mBtnIndices.setVisibility( a.canIndices?View.VISIBLE:View.INVISIBLE);
        mBtnResolve.setVisibility( a.canResolve?View.VISIBLE:View.INVISIBLE);
    }

    public void onResolve(View view) {
        mGrid.resolve();
    }

    public void onIndice(View view) {
        mGrid.nextIndice();
    }
}
