package com.example.patrice.notification_message;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Patrice on 24/06/2018.
 */

public class appairage extends AppCompatActivity
{

    private Button buttonDisplayToken;
    private TextView textViewToken;

    JSONParser jParser = new JSONParser();
    private static String url_add_token_db = "http://analog-design.net/WORKAREA/WEBSITE/douche/reception.php";

    SharedPreferences sharedpreferences;

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

        buttonDisplayToken = (Button) findViewById(R.id.buttonDisplayToken);
        textViewToken = (TextView) findViewById(R.id.textViewToken);


        buttonDisplayToken.setOnClickListener(new View.OnClickListener()
        {
            public void onClick(View v)
            {
                String token = SharedPrefManager.getInstance(appairage.this).getDeviceToken();

                //if token is not null
                if (token != null) {
                    //displaying the token
                    textViewToken.setText(token);
                    new update_database().execute(token);


                } else {
                    //if token is null that means something wrong
                    textViewToken.setText("Token not generated");
                }
            }
        });



    }

    public class update_database extends AsyncTask<String, Void, String> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected String doInBackground(String... args)
        {

            String token = args[0];

            List<NameValuePair> params = new ArrayList<NameValuePair>();
            params.add(new BasicNameValuePair("token", token));
            params.add(new BasicNameValuePair("user", "vincent"));

            JSONObject json = jParser.makeHttpRequest(url_add_token_db, "POST", params);

            Log.d("Create Response", json.toString());

            String reponse = null;

            try {
                //int success = json.getInt(TAG_SUCCESS);
                String msg = json.getString("success");

                if (msg.equals("1")) {
                    reponse = "OK";
                } else {
                    reponse = "FAIL";
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }

            return (reponse);

        }

        @Override
        protected void onPostExecute(String result)
        {
            if (result.equals("OK"))
            {
                Toast.makeText(getApplicationContext(), "Token ajouter avec succés", Toast.LENGTH_LONG).show();
                sharedpreferences = getSharedPreferences("PREFERENCE", Context.MODE_PRIVATE);
                SharedPreferences.Editor editor = sharedpreferences.edit();
                editor.putBoolean("isFirstRun", false);
                editor.commit();
                finish();

            } else {
                Toast.makeText(getApplicationContext(), "Erreur de traitement : Sauvegarde impossible !", Toast.LENGTH_LONG).show();
            }
        }
    }



}
