package com.p3.bewoplaner.page.login;

import com.google.firebase.iid.FirebaseInstanceId;

import com.p3.bewoplaner.BewoplanerApplication;
import com.p3.bewoplaner.R;
import com.p3.bewoplaner.data.AppPrefsConstants;
import com.p3.bewoplaner.data.prefs.AppPrefs;
import com.p3.bewoplaner.di.InjectionHelper;
import com.p3.bewoplaner.page.main.MainActivity;
import com.p3.bewoplaner.utils.AppStoppedService;
import com.p3.bewoplaner.utils.DialogHandler;
import com.p3.bewoplaner.utils.ui.ViewUtils;
import com.securepreferences.SecurePreferences;

import android.content.Intent;
import android.os.Bundle;
import android.provider.Settings;
import android.support.annotation.Nullable;
import android.support.annotation.StringRes;
import android.support.design.widget.Snackbar;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ProgressBar;

import java.util.Map;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.OnEditorAction;

/**
 * Created by imarneanu on 2/10/17.
 */
public class LoginActivity extends AppCompatActivity
        implements LoginView {

    @BindView(R.id.login_chat_code)
    EditText mChatCodeEditText;
    @BindView(R.id.login_account)
    EditText mAccountEditText;
    @BindView(R.id.login_username)
    EditText mUsernameEditText;
    @BindView(R.id.login_password)
    EditText mPasswordEditText;
    @BindView(R.id.login_button)
    Button mLoginButton;
    @BindView(R.id.login_progress_bar)
    ProgressBar mProgressBar;
    @BindView(R.id.login_save_password_checkBox)
    CheckBox mSavePasswordCheckbox;

    @Inject
    LoginPresenter mLoginPresenter;

    @Inject
    @AppPrefs
    SecurePreferences mSecuredPrefs;

    private Snackbar mSnackbar;
    private Callback mCallback;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        InjectionHelper.getLoginComponent(this).inject(this);
        ButterKnife.bind(this);
        mLoginPresenter.attachView(this);
        decryptPreference();
        ViewUtils.setProgressBarColor(mProgressBar, R.color.colorDefault);
        mCallback.logout(Settings.Secure.getString(this.getContentResolver(),
                Settings.Secure.ANDROID_ID), true, null);
    }

    @OnEditorAction(R.id.login_password)
    public boolean passwordDoneClicked(int actionId) {
        if (actionId == EditorInfo.IME_ACTION_DONE) {
            ViewUtils.hideKeyboard(this);
        }
        return false;
    }

    @OnClick(R.id.login_button)
    void loginButtonClicked() {
        mCallback.checkVoucher(mAccountEditText.getText().toString().trim(),
                mChatCodeEditText.getText().toString().trim(),
                mPasswordEditText.getText().toString(),
                mSavePasswordCheckbox.isChecked());
    }

    @Override
    public void voucherIsNotValid() {
        DialogHandler.createNoTitleDialog(this,
                R.string.login_voucher_code_is_not_valid,
                false,
                android.R.string.ok,
                (dialog, which) -> dialog.dismiss()).show();
    }

    @Override
    public void voucherIsValid() {
        ((BewoplanerApplication) getApplication()).resetApplicationComponent();
        verifyIfIsChecked();
    }

    private void verifyIfIsChecked() {
        mLoginPresenter.detachView();
        InjectionHelper.destroyLoginComponent();
        InjectionHelper.getLoginComponent(this).inject(this);
        mLoginPresenter.attachView(this);
        mCallback.tryToLogin(mUsernameEditText.getText().toString().trim(), mPasswordEditText.getText().toString());
    }

    @Override
    public void loginSuccessful() {
        mCallback.tryToRegisterDevice(Settings.Secure.getString(this.getContentResolver(),
                Settings.Secure.ANDROID_ID), FirebaseInstanceId.getInstance().getToken());
        if (!mSavePasswordCheckbox.isChecked()) {
            startService(new Intent(getBaseContext(), AppStoppedService.class));
        }
        Intent intent = new Intent(this, MainActivity.class);
        startActivity(intent);
        finish();
    }

    private void decryptPreference() {
        for (Map.Entry<String, ?> entry : mSecuredPrefs.getAll().entrySet()) {
            final String key = entry.getKey();
            if (key == null) {
                continue;
            } else if (key.equals(SecurePreferences.hashPrefKey(AppPrefsConstants.CHAT_CODE))) {
                mChatCodeEditText.setText(String.valueOf(mSecuredPrefs.getString(AppPrefsConstants.CHAT_CODE, null)));
            } else if (key.equals(SecurePreferences.hashPrefKey(AppPrefsConstants.ACCOUNT_NO))) {
                mAccountEditText.setText(String.valueOf(mSecuredPrefs.getString(AppPrefsConstants.ACCOUNT_NO, null)));
            }
        }
    }

    @Override
    public void setCallback(Callback callback) {
        mCallback = callback;
    }

    @Override
    public void showLoading() {
        mSavePasswordCheckbox.setEnabled(false);
        mUsernameEditText.setEnabled(false);
        mPasswordEditText.setEnabled(false);
        mChatCodeEditText.setEnabled(false);
        mAccountEditText.setEnabled(false);
        mProgressBar.setVisibility(View.VISIBLE);
        mLoginButton.setVisibility(View.GONE);
    }

    @Override
    public void showContent() {
        mSavePasswordCheckbox.setEnabled(true);
        mUsernameEditText.setEnabled(true);
        mPasswordEditText.setEnabled(true);
        mChatCodeEditText.setEnabled(true);
        mAccountEditText.setEnabled(true);
        mProgressBar.setVisibility(View.GONE);
        mLoginButton.setVisibility(View.VISIBLE);
    }

    @Override
    public void showError(@StringRes int messageRes) {
        showError(getString(messageRes));
    }

    @Override
    public void showError(String message) {
        if (mSnackbar == null) {
            mSnackbar = Snackbar.make(findViewById(R.id.login_coordinator_layout), message, Snackbar.LENGTH_LONG)
                    .setAction("Retry", view -> verifyIfIsChecked());
            mSnackbar.setActionTextColor(ContextCompat.getColor(this, R.color.colorDefault));
        }
        mSnackbar.setText(message);
        mSnackbar.show();
    }

    @Override
    protected void onDestroy() {
        InjectionHelper.destroyLoginComponent();
        mLoginPresenter.detachView();
        super.onDestroy();
    }
}
