package com.les4elefantastiq.les4elefantcowork.activities;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import com.google.gson.Gson;
import com.les4elefantastiq.les4elefantcowork.R;
import com.les4elefantastiq.les4elefantcowork.activities.utils.BaseActivity;
import com.les4elefantastiq.les4elefantcowork.managers.CoworkerManager;
import com.les4elefantastiq.les4elefantcowork.managers.ProfileManager;
import com.les4elefantastiq.les4elefantcowork.models.linkedinmodels.LinkedInCoworker;
import com.linkedin.platform.APIHelper;
import com.linkedin.platform.LISessionManager;
import com.linkedin.platform.errors.LIApiError;
import com.linkedin.platform.errors.LIAuthError;
import com.linkedin.platform.listeners.ApiListener;
import com.linkedin.platform.listeners.ApiResponse;
import com.linkedin.platform.listeners.AuthListener;
import com.linkedin.platform.utils.Scope;

public class SignInActivity extends BaseActivity implements View.OnClickListener, ApiListener, AuthListener {

    // -------------- Objects, Variables -------------- //

    private ProgressDialog progressDialog;

    // -------------------- Views --------------------- //

    // ------------------ LifeCycle ------------------- //

    @SuppressWarnings("ConstantConditions")
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sign_in_activity);

        // Set status bar color
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(getResources().getColor(R.color.colorPrimary));
        }

        findViewById(R.id.button_linked_in).setOnClickListener(this);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // Add this line to your existing onActivityResult() method
        LISessionManager.getInstance(getApplicationContext()).onActivityResult(this, requestCode, resultCode, data);
    }

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


    // ------------------ Listeners ------------------- //

    @Override
    public void onClick(View view) {
        progressDialog = ProgressDialog.show(SignInActivity.this, null, "Please wait ...", true, false);
        ProfileManager.signWithLinkedIn(SignInActivity.this);
    }

    @Override
    public void onApiSuccess(ApiResponse apiResponse) {
        // Login coworker
        new LoginAsyncTask().execute(apiResponse);
    }

    @Override
    public void onApiError(LIApiError LIApiError) {
        showConnectionErrorAlertDialog();
    }

    @Override
    public void onAuthSuccess() {
        // Authentication was successful.
        // Now, get profile data
        String url = "https://api.linkedin.com/v1/people/~:(first-name,last-name,id,picture-urls::(original),positions,summary,headline)?format=json";

        APIHelper apiHelper = APIHelper.getInstance(this);
        apiHelper.getRequest(getApplicationContext(), url, this);
    }

    @Override
    public void onAuthError(LIAuthError error) {
        showConnectionErrorAlertDialog();
    }

    // ------------------- Methods -------------------- //

    public static Scope buildScope() {
        return Scope.build(Scope.R_BASICPROFILE, Scope.W_SHARE);
    }

    public void showConnectionErrorAlertDialog() {
        if (progressDialog != null) {
            progressDialog.dismiss();
        }

        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage(R.string.Connection_error);
        AlertDialog dialog = builder.create();
        dialog.show();
    }

    // ------------------ AsyncTasks ------------------ //

    private class LoginAsyncTask extends AsyncTask<ApiResponse, Void, Boolean> {

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

        @Override
        protected Boolean doInBackground(ApiResponse... apiResponses) {
            LinkedInCoworker linkedInCoworker = new Gson().fromJson(apiResponses[0].getResponseDataAsString(), LinkedInCoworker.class);
            ProfileManager.linkedInId = linkedInCoworker.linkedInId;
            Boolean success = CoworkerManager.login(linkedInCoworker.getCoworker());

            return success;
        }

        @Override
        protected void onPostExecute(Boolean success) {
            super.onPostExecute(success);
            progressDialog.dismiss();

            if (success) {
                startActivity(new Intent(SignInActivity.this, NavigationActivity.class));
                finish();
            }
        }
    }
}
@@ -76,156 +76,22 @@
 app.
-AlertDialog;%0Aimport android.app.Dialog;%0Aimport android.app.DialogFragment;%0Aimport android.app.ProgressDialog;%0Aimport android.content.DialogInterface
+ProgressDialog
 ;%0Aim
@@ -247,33 +247,8 @@
 le;%0A
-import android.util.Log;%0A
 impo
@@ -297,16 +297,16 @@
 Window;%0A
-
 import a
@@ -331,16 +331,45 @@
 Manager;
+%0Aimport android.widget.Toast;
 %0A%0Aimport
@@ -3858,185 +3858,117 @@
     
-AlertDialog.Builder builder = new AlertDialog.Builder(this);%0A        builder.setMessage(R.string.Connection_error);%0A        AlertDialog dialog = builder.create();%0A        dialog
+Toast.makeText(this, R.string.Whoops_an_error_has_occured__Check_your_internet_connection, Toast.LENGTH_LONG)
 .sho

Fix message no connexion