package com.getbubblenow.android.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.widget.AppCompatButton;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;
import android.widget.Toast;

import com.getbubblenow.android.R;
import com.getbubblenow.android.api.ApiConstants;
import com.getbubblenow.android.repository.DataRepository;
import com.getbubblenow.android.resource.StatusResource;
import com.getbubblenow.android.viewmodel.LoginViewModel;


public class LoginActivity extends BaseActivityBubble {

    private LoginViewModel loginViewModel;
    private EditText userName;
    private EditText password;
    private AppCompatButton sign;

    private boolean userNameStateFlag = false;
    private boolean passwordStateFlag = false;
    private static final String USER_NAME_KEY = "userName";
    private static final String PASSWORD_KEY = "password";
    private static final String NO_INTERNET_CONNECTION = "no internet connection";
    private static final String LOGIN_FAILED = "Login Failed";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initUI();
        loginViewModel = new ViewModelProvider(this).get(LoginViewModel.class);
    }

    private void initUI() {
        initViews();
        initListeners();
    }

    private void initListeners() {
        sign.setOnClickListener(new OnClickListener() {
            @Override public void onClick(final View v) {
                signIn();
            }
        });
        userNameStateListener();
        passwordStateListener();
        sendListener();
    }

    private void initViews() {
        userName = findViewById(R.id.userName);
        password = findViewById(R.id.password);
        sign = findViewById(R.id.signButton);
    }


    private void sendListener() {
        password.setOnEditorActionListener(new OnEditorActionListener() {
            @Override public boolean onEditorAction(final TextView v, final int actionId, final KeyEvent event) {
                if(((event!=null)&&(event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) || (actionId == EditorInfo.IME_ACTION_SEND)){
                    if(!TextUtils.isEmpty(password.getText().toString()) && !TextUtils.isEmpty(userName.getText().toString())) {
                        signIn();
                        return true;
                    }
                    else {
                        Toast.makeText(LoginActivity.this,getString(R.string.empty_message),Toast.LENGTH_SHORT).show();
                        return true;
                    }
                }
                return false;
            }
        });
    }

    private void userNameStateListener() {
        userName.addTextChangedListener(new TextWatcher() {
            @Override public void beforeTextChanged(final CharSequence s, final int start, final int count, final int after) {

            }

            @Override public void onTextChanged(final CharSequence s, final int start, final int before, final int count) {

            }

            @Override public void afterTextChanged(final Editable s) {

                if (userName.getText().toString().trim().isEmpty()) {
                    userNameStateFlag = false;
                } else {
                    userNameStateFlag = true;
                }
                setButtonState();
            }
        });
    }

    private void passwordStateListener() {

        password.addTextChangedListener(new TextWatcher() {
            @Override public void beforeTextChanged(final CharSequence s, final int start, final int count, final int after) {

            }

            @Override public void onTextChanged(final CharSequence s, final int start, final int before, final int count) {

            }

            @Override public void afterTextChanged(final Editable s) {

                if (password.getText().toString().trim().isEmpty()) {
                    passwordStateFlag = false;
                } else {
                    passwordStateFlag = true;
                }
                setButtonState();
            }
        });
    }

    private void signIn() {
        if (loginViewModel.checkRepositoryInstance()) {
            loginViewModel.buildRepositoryInstance(this, ApiConstants.BOOTSTRAP_URL);
        } else {
            loginViewModel.buildClientService(ApiConstants.BOOTSTRAP_URL);
        }
        final String usernameInput = userName.getText().toString().trim();
        final String passwordInput = password.getText().toString().trim();
        showLoadingDialog();
        loginViewModel.login(this,usernameInput,passwordInput).observe(LoginActivity.this, new Observer<StatusResource<byte[]>>() {
            @Override public void onChanged(final StatusResource<byte[]> stringStatusResource) {
                switch (stringStatusResource.status) {
                    case SUCCESS:
                        closeLoadingDialog();
                        final Intent mainActivityIntent = new Intent(LoginActivity.this, MainActivity.class);
                        mainActivityIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                        startActivity(mainActivityIntent);
                        break;
                    case LOADING:
                        Log.d("TAG", "Loading");
                        break;
                    case ERROR:
                        closeLoadingDialog();
                        if (stringStatusResource.message.equals(NO_INTERNET_CONNECTION)) {
                            showNetworkNotAvailableMessage();
                        } else if (stringStatusResource.message.equals(LOGIN_FAILED)) {
                            Toast.makeText(LoginActivity.this, LOGIN_FAILED, Toast.LENGTH_LONG).show();
                        } else {
                            showErrorDialog(stringStatusResource.message);
                        }
                        break;
                }
            }
        });
    }

    private void setButtonState(){
        if(userNameStateFlag  && passwordStateFlag){
            sign.setBackgroundDrawable(getDrawable(R.drawable.sign_in_enable));
            sign.setEnabled(true);
        }
        else {
            sign.setBackgroundDrawable(getDrawable(R.drawable.sign_in_disable));
            sign.setEnabled(false);
        }
    }

    @Override protected void onSaveInstanceState(@NonNull final Bundle outState) {
        outState.putBoolean(USER_NAME_KEY,userNameStateFlag);
        outState.putBoolean(PASSWORD_KEY,passwordStateFlag);
        super.onSaveInstanceState(outState);
    }

    @Override protected void onRestoreInstanceState(@NonNull final Bundle savedInstanceState) {
        userNameStateFlag = savedInstanceState.getBoolean(USER_NAME_KEY);
        passwordStateFlag = savedInstanceState.getBoolean(PASSWORD_KEY);
        super.onRestoreInstanceState(savedInstanceState);
    }
}
