package de.fu_berlin.cdv.chasingpictures;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;
import java.util.List;

import de.fu_berlin.cdv.chasingpictures.activity.Slideshow;
import de.fu_berlin.cdv.chasingpictures.api.Picture;
import de.fu_berlin.cdv.chasingpictures.api.Place;
import de.fu_berlin.cdv.chasingpictures.security.Access;


public class MainActivity extends Activity {
    private static final String TAG = "MainActivity";
    private boolean triedLogin = false;

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

//        MediaPlayer mediaPlayer = MediaPlayer.create(this, R.raw.intro);
//        mediaPlayer.start(); // no need to call prepare(); create() does that for you
    }

    @Override
    protected void onResume() {
        super.onResume();
        // Check if we are logged in, but only once
        if (!triedLogin && !Access.hasAccess(this)) {
            triedLogin = true;
            Intent intent = new Intent(this, LoginPage.class);
            startActivityForResult(intent, LoginPage.LOGIN);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == LoginPage.LOGIN
                && resultCode == RESULT_OK
                && Access.hasAccess(this)) {
            Toast.makeText(this, R.string.login_success, Toast.LENGTH_SHORT).show();
            findViewById(R.id.show_login_page_button).setEnabled(false);
        }
    }

    public void logOut(View view) {
        Access.revokeAccess(this);
        if (!Access.hasAccess(this)) {
            Toast.makeText(this, getString(R.string.logout_success), Toast.LENGTH_SHORT).show();
            findViewById(R.id.show_login_page_button).setEnabled(true);
        }
    }

    public void showLoginPage(View view) {
        Intent intent = new Intent(this, LoginPage.class);
        startActivity(intent);
    }

    public void toNext(View view){
        //region Save picture
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.rathaus);
        File tempFile = null;
        try {
            tempFile = File.createTempFile("rathaus", "png", getCacheDir());
            FileOutputStream out = new FileOutputStream(tempFile);
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
            out.close();
        } catch (IOException e) {
            Log.e(TAG, "Image file could not be written.", e);
        }

        Picture picture = new Picture();
        picture.setTime(new Date());
        picture.setCachedFile(tempFile);
        //endregion

        Place dummy = new Place();
        dummy.setPicture(picture);
        dummy.setLatitude(52.5170716);
        dummy.setLongitude(13.3888716);
        Intent intent = Maps.createIntent(this, dummy);
        startActivity(intent);
    }

    public void showPictureSelectionPage(View view) {
        Intent intent = new Intent(this, PictureSelectionActivity.class);
        startActivity(intent);
    }

    public void showCamera(View view){
        Intent intent = new Intent(this, Camera.class);
        startActivity(intent);
    }

    public void showSlideshow(View view) {
        Place placeWithPictures = DebugUtilities.getPlaceWithPictures();
        if (placeWithPictures != null) {
            List<Picture> demoPictures = placeWithPictures.getPictures();
            Intent intent = Slideshow.createIntent(this, demoPictures);
            startActivity(intent);
        } else {
            Toast.makeText(
                    this,
                    "Did not receive any pictures for slideshow",
                    Toast.LENGTH_SHORT
            ).show();
        }
    }
}

@@ -186,32 +186,44 @@
 oid.
-graphics.drawable.Drawab
+os.AsyncTask;%0Aimport android.os.Bund
 le;%0A
@@ -232,38 +232,39 @@
 port android.os.
-Bu
+Ha
 ndle
+r
 ;%0Aimport android
@@ -572,16 +572,76 @@
 icture;%0A
+import de.fu_berlin.cdv.chasingpictures.api.PictureRequest;%0A
 import d
@@ -3694,99 +3694,370 @@
 lace
-WithPictur
+ = new Place();%0A        place.setId(6);%0A%0A        final PictureRequest pictureRequ
 es
+t
  = 
-DebugUtilities.getPlaceWithPictures();%0A        if (placeWithPictures != null
+new PictureRequest(this, place);%0A%0A        final Handler handler = new Handler();%0A%0A        final AsyncTask%3CPictureRequest, Void, Void%3E task = new AsyncTask%3CPictureRequest, Void, Void%3E() %7B%0A            @Override%0A            protected Void doInBackground(PictureRequest... params
 ) %7B%0A
@@ -4068,16 +4068,20 @@
         
+  
+  
 List%3CPic
@@ -4086,21 +4086,17 @@
 icture%3E 
-demoP
+p
 ictures 
@@ -4102,24 +4102,59 @@
  = p
-laceWithPictures
+arams%5B0%5D.sendRequest().getBody().getPlaces().get(0)
 .get
@@ -4169,32 +4169,42 @@
 ();%0A            
+    
+final 
 Intent intent = 
@@ -4230,19 +4230,34 @@
 ent(
-this, demoP
+getApplicationContext(), p
 ictu
@@ -4258,24 +4258,27 @@
  pictures);%0A
+%0A  
             
@@ -4281,75 +4281,69 @@
     
-startActivity(intent);%0A        %7D else %7B%0A            Toast.makeText(
+  handler.post(new Runnable() %7B%0A                    @Override
 %0A   
@@ -4359,21 +4359,35 @@
         
-this,
+public void run() %7B
 %0A       
@@ -4403,124 +4403,170 @@
     
-%22Did not receive any pictures for slideshow%22,%0A                    Toast.LENGTH_SHORT%0A            ).show();%0A        %7D
+    startActivity(intent);%0A                    %7D%0A                %7D);%0A%0A                return null;%0A            %7D%0A        %7D;%0A%0A        task.execute(pictureRequest);
 %0A   

Send picture request for slideshow from main activity instead of using debug data