package app.mar.activities;

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 app.mar.utils.FontChangeCrawler;
import app.mar.game.Resource;
import app.mar.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 Button mButton;
    private PhotoViewAttacher mAttacher;


    public static final int CONTENT_NULL=0;
    public static final int CONTENT_MAP=1;
    public static final int CONTENT_INFO=2;
    public static final int CONTENT_IMAGE=3;
    public static final int CONTENT_3D=4;
    protected int mContent=CONTENT_NULL;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_viewer);
        mImageView = (ImageView) findViewById(R.id.imageView);
        mButton = (Button) findViewById(R.id.info);
        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 && !getIntent().hasExtra("information") && !getIntent().hasExtra("map"))
        {
            title.setText(mResource.getTitle());
            if(mResource.isImage())
            {
                mContent=CONTENT_IMAGE;
                Bitmap bmp=null;
                Toast.makeText(this, "Chargement de '"+mResource.getName()+"'", Toast.LENGTH_SHORT).show();
                try {
                    bmp = FileManager.openImage(this, mResource.getName());
                } catch (Exception e) {
                    e.printStackTrace();
                    Toast.makeText(this, "Impossible de charger '"+mResource.getName()+"'", Toast.LENGTH_LONG).show();
                }

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

            }

        } else if(getIntent().hasExtra("map") )
        {
            mContent=CONTENT_MAP;
            Bitmap bmp=null;
            try {
                bmp = FileManager.openImage(this, "map");
            } catch (Exception e) {
                e.printStackTrace();
            }
            title.setVisibility(View.GONE);
            mImageView.setImageBitmap(bmp);
            mAttacher = new PhotoViewAttacher(mImageView);
            mAttacher.setMaximumScale(20);
            mButton.setVisibility(View.GONE);
        }else
        {
            title.setText("Erreur");
        }
        FontChangeCrawler.setFont(this);

    }

    public void onClickInfo(View v)
    {

    }


    public void onClick(View v)
    {
        if(mContent==CONTENT_IMAGE) {

            final TextView title = (TextView) findViewById(R.id.title);
            if (title.getVisibility() == View.VISIBLE) {
                title.setVisibility(View.GONE);
                mButton.setVisibility(View.GONE);
            } else {

                title.setVisibility(View.VISIBLE);
                mButton.setVisibility(View.VISIBLE);
            }
        }
    }

}
