package macampcorp.macamp.activities;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;

import macampcorp.macamp.game.Game;
import macampcorp.macamp.lang.Locale;
import macampcorp.macamp.utils.files.FileManager;


public class TutoActivity extends AppCompatActivity  {
    public static final int REQUEST_CODE=1010;
    protected ImageView mImage;
    protected ArrayList<String> mPathList = new ArrayList<>();
    protected int mCurrent=0;

    protected Button mNext;
    protected Button mPrev;
    protected long mTime=0;

    @Override
    protected void onCreate(Bundle savedInstanceState)  {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tuto);
        setAssetspath();
        mImage=findViewById(R.id.image);
        mNext=findViewById(R.id.btn_next);
        mPrev=findViewById(R.id.btn_prev);

        update();
    }


    public void onNext(View v)
    {

        if(mCurrent==mPathList.size()-1)
        { //OK
            onPass(null);
        }
        else
        { //suivant
            mCurrent++;
        }
        update();
    }

    public void exit()
    {
        setResult(0);
        finish();
    }

    public void onPass(View v)
    {
        if(mCurrent<mPathList.size()-1)
        { //Si ce n''est pas le dernier on demande confirmation
            new AlertDialog.Builder(this)
                    .setTitle(R.string.tuto_pass_title)
                    .setMessage(R.string.no)
                    .setNegativeButton(android.R.string.no,  null)
                    .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface arg0, int arg1) {
                            exit();
                        }
                    }).create().show();

        }
        else
            exit();

    }

    public void onPrev(View v)
    {
        if(mCurrent>0) mCurrent--;
        else onPass(null);
        update();
    }

    private void update()
    {
        String x = mPathList.get(mCurrent);
        FileManager.getAfdMedia(this, x);
        mImage.setImageBitmap(FileManager.openImage(this, x));
        if(mCurrent==0) mPrev.setVisibility(View.INVISIBLE);
        else mPrev.setVisibility(View.VISIBLE);

        if(mCurrent==mPathList.size()-1) mNext.setText(R.string.btn_ok);
        else mNext.setText(R.string.btn_next);
    }

    @Override
    public void onBackPressed()
    {
        onPrev(null);
    }

    private  boolean testTutoFile(String path)
    {
        AssetManager mg = getResources().getAssets();
        InputStream is = null;
        try {
            is = mg.open(path);
            is.close();
            return true;
        } catch (IOException ex) {
            return false;
        }
    }

    private void setAssetspath()
    {
        mPathList.clear();
        boolean ok=true;
        for(int i=0; i<1000 && ok; i++)
        {
            String x = "tuto/"+i+".png";
            ok=testTutoFile(x);
            if(ok) mPathList.add(x);
        }
    }

    public static void start(Activity c)
    {
        Intent it = new Intent(c, TutoActivity.class);
        c.startActivityForResult(it, REQUEST_CODE);
    }
}
