package macampcorp.macamp.activities.viewers;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import macampcorp.macamp.activities.R;
import macampcorp.macamp.exceptions.BadResourceTypeException;
import macampcorp.macamp.game.Resource;
import macampcorp.macamp.utils.files.FileManager;
import uk.co.senab.photoview.PhotoViewAttacher;

/**
 * An example full-screen activity that shows and hides the system UI (i.e.
 * status bar and navigation/system bar) with user interaction.
 */
public class ImageViewerActivity extends AppCompatActivity {


    public static final int REQUEST_CODE = 30;
    private Resource mResource;
    private ImageView mImageView;
    private PhotoViewAttacher mAttacher;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_viewer);
        mImageView = (ImageView) findViewById(R.id.imageView);
        if(getIntent().hasExtra("resource"))
        {
            mResource = (Resource) getIntent().getSerializableExtra("resource");
            if(mResource!=null) setTitle(mResource.getTitle());
        }

        final TextView title = (TextView) findViewById(R.id.title);
        title.setVisibility(View.VISIBLE);

        if(mResource != null )
        {
            title.setText(mResource.getTitle());
            if(mResource.isImage())
            {
                Bitmap bmp=null;

                try {
                    bmp = FileManager.openImage(this, mResource.getFile());
                } catch (Exception e) {
                    e.printStackTrace();
                    Toast.makeText(this, "Impossible de charger '"+mResource.getName()+"' ('"+mResource.getFile()+"')", Toast.LENGTH_LONG).show();
                }

                mImageView.setImageBitmap(bmp);
                mAttacher = new PhotoViewAttacher(mImageView);
                mAttacher.setMaximumScale(20);

            }else
            {
                title.setText("Erreur: ");
                throw  new BadResourceTypeException(mResource, Resource.IMAGE);
            }

        } else
        {
            title.setText("Erreur ressource invalide");
            throw new RuntimeException("Erreur ressource invalide");
        }

    }


    public void onClick(View v)
    {

    }

}
