package com.example.patrice.http01;

import android.app.ProgressDialog;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.util.Base64;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.view.View.OnClickListener;
import android.widget.Toast;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

import javax.net.ssl.HttpsURLConnection;

//https://androidjson.com/capture-image-camera-upload-server/  source du code

//<uses-permission android:name="android.permission.CAMERA" />
//<uses-feature android:name="android.hardware.camera" />
//<uses-feature android:name="android.hardware.camera.autofocus" />

    // https://mobikul.com/android-getting-image-camera-uploading-web-server-multipart-connection-type/

//http://www.codepool.biz/take-a-photo-from-android-camera-and-upload-it-to-a-remote-php-server.html

//http://www.thaicreate.com/mobile/android-photo-camera-upload-server.html

public class cam_capture01 extends AppCompatActivity
{

    Button CaptureImageFromCamera,UploadImageToServer;
    ImageView ImageViewHolder;
    EditText imageName;
    ProgressDialog progressDialog ;
    Intent intent ;

    public  static final int RequestPermissionCode  = 1 ;

    Bitmap bitmap;
    boolean check = true;

    String GetImageNameFromEditText;
    String ImageNameFieldOnServer = "image_name" ;
    String ImagePathFieldOnServer = "image_path" ;
    String ImageUploadPathOnSever ="https://androidjsonblog.000webhostapp.com/capture_img_upload_to_server.php" ;

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

        CaptureImageFromCamera = (Button)findViewById(R.id.button);
        ImageViewHolder = (ImageView)findViewById(R.id.imageView);
        UploadImageToServer = (Button) findViewById(R.id.button2);
        imageName = (EditText)findViewById(R.id.editText);

       // EnableRuntimePermissionToAccessCamera();

        CaptureImageFromCamera.setOnClickListener(new OnClickListener()
        {
            public void onClick(View arg0)
            {
                intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                startActivityForResult(intent, 7);
            }
        });

        UploadImageToServer.setOnClickListener(new OnClickListener()
        {
            public void onClick(View arg0)
            {
                GetImageNameFromEditText = imageName.getText().toString();
                ImageUploadToServerFunction();
            }
        });

    }

    // Star activity for result method to Set captured image on image view after click.
    protected void onActivityResult(int requestCode, int resultCode, Intent data)
    {

        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == 7 && resultCode == RESULT_OK && data != null && data.getData() != null)
        {
            Uri uri = data.getData();

            try
            {
                // Adding captured image in bitmap.
                bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
                // adding captured image in imageview.
                ImageViewHolder.setImageBitmap(bitmap);
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
        }

    }

    // Requesting runtime permission to access camera.
/*
    public void EnableRuntimePermissionToAccessCamera(){

        if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this,
                Manifest.permission.CAMERA))
        {

            // Printing toast message after enabling runtime permission.
            Toast.makeText(MainActivity.this,"CAMERA permission allows us to Access CAMERA app", Toast.LENGTH_LONG).show();

        } else {

            ActivityCompat.requestPermissions(MainActivity.this,new String[]{Manifest.permission.CAMERA}, RequestPermissionCode);

        }
    }
*/
    // Upload captured image online on server function.
    public void ImageUploadToServerFunction()
    {
        ByteArrayOutputStream byteArrayOutputStreamObject ;
        byteArrayOutputStreamObject = new ByteArrayOutputStream();
        // Converting bitmap image to jpeg format, so by default image will upload in jpeg format.
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStreamObject);
        byte[] byteArrayVar = byteArrayOutputStreamObject.toByteArray();
        final String ConvertImage = Base64.encodeToString(byteArrayVar, Base64.DEFAULT);

        class AsyncTaskUploadClass extends AsyncTask<Void,Void,String>
        {

            @Override
            protected void onPreExecute()
            {
                super.onPreExecute();
                // Showing progress dialog at image upload time.
                progressDialog = ProgressDialog.show(cam_capture01.this,"Image is Uploading","Please Wait",false,false);
            }

            @Override
            protected void onPostExecute(String string1)
            {
                super.onPostExecute(string1);
                // Dismiss the progress dialog after done uploading.
                progressDialog.dismiss();
                // Printing uploading success message coming from server on android app.
                Toast.makeText(cam_capture01.this,string1, Toast.LENGTH_LONG).show();
                // Setting image as transparent after done uploading.
                ImageViewHolder.setImageResource(android.R.color.transparent);
            }

            @Override
            protected String doInBackground(Void... params)
            {

                ImageProcessClass imageProcessClass = new ImageProcessClass();
                HashMap<String,String> HashMapParams = new HashMap<String,String>();
                HashMapParams.put(ImageNameFieldOnServer, GetImageNameFromEditText);
                HashMapParams.put(ImagePathFieldOnServer, ConvertImage);
                String FinalData = imageProcessClass.ImageHttpRequest(ImageUploadPathOnSever, HashMapParams);
                return FinalData;
            }
        }

        AsyncTaskUploadClass AsyncTaskUploadClassOBJ = new AsyncTaskUploadClass();
        AsyncTaskUploadClassOBJ.execute();
    }

    public class ImageProcessClass
    {

        public String ImageHttpRequest(String requestURL,HashMap<String, String> PData)
        {

            StringBuilder stringBuilder = new StringBuilder();

            try
            {
                URL url;
                HttpURLConnection httpURLConnectionObject ;
                OutputStream OutPutStream;
                BufferedWriter bufferedWriterObject ;
                BufferedReader bufferedReaderObject ;
                int RC ;

                url = new URL(requestURL);

                httpURLConnectionObject = (HttpURLConnection) url.openConnection();
                httpURLConnectionObject.setReadTimeout(19000);
                httpURLConnectionObject.setConnectTimeout(19000);
                httpURLConnectionObject.setRequestMethod("POST");
                httpURLConnectionObject.setDoInput(true);
                httpURLConnectionObject.setDoOutput(true);
                OutPutStream = httpURLConnectionObject.getOutputStream();
                bufferedWriterObject = new BufferedWriter(new OutputStreamWriter(OutPutStream, "UTF-8"));
                bufferedWriterObject.write(bufferedWriterDataFN(PData));
                bufferedWriterObject.flush();
                bufferedWriterObject.close();
                OutPutStream.close();
                RC = httpURLConnectionObject.getResponseCode();

                if (RC == HttpsURLConnection.HTTP_OK)
                {
                    bufferedReaderObject = new BufferedReader(new InputStreamReader(httpURLConnectionObject.getInputStream()));
                    stringBuilder = new StringBuilder();
                    String RC2;
                    while ((RC2 = bufferedReaderObject.readLine()) != null)
                    {
                        stringBuilder.append(RC2);
                    }
                }

            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
            return stringBuilder.toString();
        }

        private String bufferedWriterDataFN(HashMap<String, String> HashMapParams) throws UnsupportedEncodingException
        {

            StringBuilder stringBuilderObject;
            stringBuilderObject = new StringBuilder();

            for (Map.Entry<String, String> KEY : HashMapParams.entrySet())
            {

               if (check)
                    check = false;
                else
                    stringBuilderObject.append("&");

                stringBuilderObject.append(URLEncoder.encode(KEY.getKey(), "UTF-8"));
                stringBuilderObject.append("=");
                stringBuilderObject.append(URLEncoder.encode(KEY.getValue(), "UTF-8"));
            }

            return stringBuilderObject.toString();
        }

    }

    @Override
    public void onRequestPermissionsResult(int RC, String per[], int[] PResult)
    {

        switch (RC)
        {

            case RequestPermissionCode:

                if (PResult.length > 0 && PResult[0] == PackageManager.PERMISSION_GRANTED)
                {
                    Toast.makeText(cam_capture01.this,"Permission Granted, Now your application can access CAMERA.", Toast.LENGTH_LONG).show();
                }
                else
                {
                    Toast.makeText(cam_capture01.this,"Permission Canceled, Now your application cannot access CAMERA.", Toast.LENGTH_LONG).show();
                }
                break;
        }
    }

}


/*****************************************************************/
/*
<?php

        include 'DatabaseConfig.php';

// Create connection
        $conn = new mysqli($HostName, $HostUser, $HostPass, $DatabaseName);

        if($_SERVER['REQUEST_METHOD'] == 'POST')
        {
        $DefaultId = 0;

        $ImageData = $_POST['image_path'];

        $ImageName = $_POST['image_name'];

        $GetOldIdSQL ="SELECT id FROM ImageToServerTable ORDER BY id ASC";

        $Query = mysqli_query($conn,$GetOldIdSQL);

        while($row = mysqli_fetch_array($Query)){

        $DefaultId = $row['id'];
        }

        $ImagePath = "images/$DefaultId.png";

        $ServerURL = "https://androidjsonblog.000webhostapp.com/$ImagePath";

        $InsertSQL = "insert into ImageToServerTable (image_path,image_name) values ('$ServerURL','$ImageName')";

        if(mysqli_query($conn, $InsertSQL)){

        file_put_contents($ImagePath,base64_decode($ImageData));

        echo "Your Image Has Been Uploaded.";
        }

        mysqli_close($conn);
        }else{
        echo "Not Uploaded";
        }

        ?>


<?php

//Define your host here.
        $HostName = "localhost";

//Define your database username here.
        $HostUser = "id632449_androidjson";

//Define your database password here.
        $HostPass = "kdfjdfdskljomew9ry3873";

//Define your database name here.
        $DatabaseName = "id632449_androidjson";

        ?>

/*****************************************************************/
/*
<?php
        if (isset($_FILES['myFile'])) {
        // Example:
        move_uploaded_file($_FILES['myFile']['tmp_name'], "uploads/" . $_FILES['myFile']['name']);
        echo 'successful';
        }
        ?>
*/