package me.jakemoritz.tasking.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

import com.google.android.gms.auth.api.Auth;
import com.google.android.gms.auth.api.signin.GoogleSignInAccount;
import com.google.android.gms.auth.api.signin.GoogleSignInOptions;
import com.google.android.gms.auth.api.signin.GoogleSignInResult;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.Scope;

import me.jakemoritz.tasking.R;
import me.jakemoritz.tasking.helper.SharedPrefsHelper;

public class LoginActivity extends AppCompatActivity implements GoogleApiClient.OnConnectionFailedListener,
        View.OnClickListener{

    private static final String TAG = LoginActivity.class.getSimpleName();

    /* Request code used to invoke sign in user interactions. */
    private static final int RC_SIGN_IN = 0;

    /* Client used to interact with Google APIs. */
    private GoogleApiClient mGoogleApiClient;

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

        // Configure sign-in to request the user's ID, email address, and basic
        // profile. ID and basic profile are included in DEFAULT_SIGN_IN.
        GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                .requestEmail()
                .requestScopes(new Scope(getString(R.string.gac_task_scope)), new Scope(getString(R.string.people_oathscope)))
                .build();

        // Build GoogleApiClient with access to basic profile
        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .enableAutoManage(this /* FragmentActivity */, this /* OnConnectionFailedListener */)
                .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
                .build();

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

    @SuppressLint("NewApi")
    @Override
    protected void onResume() {
        super.onResume();

        // Set fullscreen
        boolean apiGreaterThanOrEqual19 = (Build.VERSION.SDK_INT >= 19);

        int mUIFlag = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_FULLSCREEN;

        if (apiGreaterThanOrEqual19){
            mUIFlag = mUIFlag | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
        } else {
            // Set fullscreen
/*            mUIFlag = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                    | View.SYSTEM_UI_FLAG_LOW_PROFILE
                    | View.SYSTEM_UI_FLAG_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;*/
        }

        getWindow().getDecorView().setSystemUiVisibility(mUIFlag);
    }

    @Override
    protected void onStop() {
        super.onStop();
        mGoogleApiClient.disconnect();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        // Result returned from launching the Intent from GoogleSignInApi.getSignInIntent(...);
        if (requestCode == RC_SIGN_IN) {
            GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
            handleSignInResult(result);
        }
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.sign_in_button){
            onSignInClicked();
        }
    }

    private void onSignInClicked() {
        Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
        startActivityForResult(signInIntent, RC_SIGN_IN);
    }

    private void handleSignInResult(GoogleSignInResult result) {
        if (result.isSuccess()) {
            // Signed in successfully, show authenticated UI.
            GoogleSignInAccount acct = result.getSignInAccount();
            saveUserInfo(acct);

            Intent mainIntent = new Intent(this, MainActivity.class);
            startActivityForResult(mainIntent, 0);

            finish();
        } else {
            // Sign in failed, show unauthenticated UI
            Snackbar signInErrorSnackbar = Snackbar.make(findViewById(R.id.activity_login), R.string.auth_error, Snackbar.LENGTH_LONG);
            signInErrorSnackbar.show();
        }
    }

    private void saveUserInfo(GoogleSignInAccount acct){
        // Save sign-in state and user info
        SharedPrefsHelper.getInstance().setUserEmail(acct.getEmail());
        SharedPrefsHelper.getInstance().setLoggedIn(true);
        SharedPrefsHelper.getInstance().setUserDisplayName(acct.getDisplayName());
        SharedPrefsHelper.getInstance().setUserId(acct.getId());
    }

    @Override
    public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
    }
}


@@ -2455,79 +2455,8 @@
 Flag
- = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION %7C View.SYSTEM_UI_FLAG_FULLSCREEN
 ;%0A%0A 
@@ -2489,16 +2489,25 @@
 rEqual19
+ %7C%7C false
 )%7B%0A     
@@ -2527,17 +2527,8 @@
 g = 
-mUIFlag %7C
  Vie
@@ -2548,96 +2548,45 @@
 LAG_
-IMMERSIVE_STICKY;%0A        %7D else %7B%0A            // Set fullscreen%0A/*            mUIFlag =
+HIDE_NAVIGATION%0A                    %7C
  Vie
@@ -2602,23 +2602,16 @@
 UI_FLAG_
-LAYOUT_
 FULLSCRE
@@ -2659,29 +2659,102 @@
 LAG_
-LAYOUT_HIDE_NAVIGATIO
+IMMERSIVE_STICKY;%0A        %7D else %7B%0A            mUIFlag =  View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREE
 N%0A  
@@ -2917,69 +2917,9 @@
 REEN
-%0A                    %7C View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;*/
+;
 %0A   

Fix fullscreen logic for LoginActivity