package tools;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Environment;
import android.view.View;
import android.widget.EditText;

import androidx.core.content.FileProvider;

import java.io.File;

import kz.istt.locust.BuildConfig;

public class OnClickListenerImageShow implements View.OnClickListener{

    public EditText imageName;
    public Context context;

    public OnClickListenerImageShow(Context context, EditText imageName){
        this.context=context;
        this.imageName=imageName;
    }

    @Override
    public void onClick(View v)
    {
        File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/Pictures/Locust/" + imageName.getText().toString());
        if(!file.isFile()) file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/Pictures/Locust/_" + imageName.getText().toString());
        if (file.isFile()) {
            Uri uri = FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID + ".provider", file);
            Intent i = new Intent();
            i.setAction(Intent.ACTION_VIEW);
            i.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            i.setDataAndType(uri, "image/jpg");
            ((Activity) context).startActivity(i);
        }
    }

}
