package kr.co.hanwha.hifive.admin.admin;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.ScrollView;

import kr.co.hanwha.hifive.R;
import kr.co.hanwha.hifive.base.BaseActivity;
import kr.co.hanwha.hifive.data.model.LoginData;
import kr.co.hanwha.hifive.data.source.MemberDataRepository;
import kr.co.hanwha.hifive.dialog.HFDefaultDialog;
import kr.co.hanwha.hifive.enums.BaseAnimationType;
import kr.co.hanwha.hifive.enums.BaseToolbarType;
import kr.co.hanwha.hifive.network.hanwha.HanwhaNetworkCallback;
import kr.co.hanwha.hifive.network.hanwha.HanwhaNetworkError;
import kr.co.hanwha.hifive.util.HFActivityUtil;
import kr.co.hanwha.hifive.util.HFInfoUtil;
import kr.co.hanwha.hifive.view.HFAlertEditTextView;

/**
 * Created by sanghoonkwon on 2018. 3. 9..
 */

public class AdminModeActivity extends BaseActivity {
    private HFAlertEditTextView mAlertEditId;
    private HFAlertEditTextView mAlertEditPw;
    private ScrollView mScrollView;

    private boolean mFocusId = false;
    private boolean mFocusPw = false;

    @Override
    protected int getContentViewResource() {
        return R.layout.activity_admin_mode;
    }

    @Override
    protected BaseToolbarType isToolbarStyle() {
        return null;
    }

    @Override
    protected BaseAnimationType isShowActivityAnimation() {
        return null;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mScrollView = (ScrollView) findViewById(R.id.scrollView);
        mAlertEditId = (HFAlertEditTextView) findViewById(R.id.id_alertEditText);
        mAlertEditPw = (HFAlertEditTextView) findViewById(R.id.pw_alertEditText);
        mAlertEditId.setOnAlertEditTextListener(onAlertEditTextListener);
        mAlertEditPw.setOnAlertEditTextListener(onAlertEditTextListener);

        findViewById(R.id.start_button).setOnClickListener(onClickListener);
    }

    @Override
    public void onBackPressed() {
        showAlert(getString(R.string.alert_finish_title), getString(R.string.alert_finish_message),
                new HFDefaultDialog.OnDialogCallback() {
                    @Override
                    public void onNegative() {
                    }

                    @Override
                    public void onPositive() {
                        HFActivityUtil.startRootActivity(AdminModeActivity.this, true);
                        finish();
                    }
                });

    }
    /**************************************************
     * Support methods
     **************************************************/
    /**
     * 유효성 검사
     */
    private boolean checkLoginData() {
        if (mAlertEditId.isEmpty()) {
            mAlertEditId.setAlertMessage(R.string.login_alert_empty_id);
            mAlertEditId.setFocus();
            return false;
        } else {
            mAlertEditId.setAlertMessage("");
        }

        if (mAlertEditPw.isEmpty()) {
            mAlertEditPw.setAlertMessage(R.string.login_alert_empty_pw);
            mAlertEditPw.setFocus();
            return false;
        } else {
            mAlertEditPw.setAlertMessage("");
        }

        return true;
    }

    /**
     * 로그인
     *
     * @param member_id
     * @param password
     */
    private void doAccountLogin(@NonNull final String member_id, @NonNull String password) {
        MemberDataRepository.getInstance().doAccountLogin(this, member_id, password, new HanwhaNetworkCallback<LoginData>() {
            @Override
            public void onSuccess(final LoginData response) {
                HFInfoUtil.setMemberLoginId(AdminModeActivity.this, member_id);
                Intent intent = new Intent(AdminModeActivity.this, AdminMenuActivity.class);
                startActivity(intent);
                finish();
            }

            @Override
            public void onFailure(HanwhaNetworkError error) {
                showAlert(error);
                showLoadingDialog(false);
            }

            @Override
            public void onPreExecute() {
                showLoadingDialog(true);
            }

            @Override
            public void onPostExecute() {
                showLoadingDialog(false);
            }
        });
    }

    /**************************************************
     * AlertEditTextView.OnAlertEditTextListener
     **************************************************/
    HFAlertEditTextView.OnAlertEditTextListener onAlertEditTextListener = new HFAlertEditTextView.OnAlertEditTextListener() {
        @Override
        public void onFocusChange(@NonNull HFAlertEditTextView v, boolean b) {
            switch (v.getId()) {
                case R.id.id_alertEditText:
                    mFocusId = b;
                    break;
                case R.id.pw_alertEditText:
                    mFocusPw = b;
                    break;
            }
            if (mFocusId || mFocusPw)
                mScrollView.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        mScrollView.smoothScrollTo(0, 1000);
                    }
                }, 500);
        }

        @Override
        public void onImeActionNext(@NonNull HFAlertEditTextView v) {
            switch (v.getId()) {
                case R.id.id_alertEditText:
                    mAlertEditPw.setFocus();
                    break;
                case R.id.pw_alertEditText:

                    break;
            }
        }

        @Override
        public void onValueChanged(@NonNull HFAlertEditTextView v) {
            switch (v.getId()) {
                case R.id.id_alertEditText:
                    mAlertEditId.setAlertMessage("");
                    break;
                case R.id.pw_alertEditText:
                    mAlertEditPw.setAlertMessage("");
                    break;
            }
        }
    };

    /**************************************************
     * View.OnClickListener
     **************************************************/
    View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.start_button:
                    if (checkLoginData()) {
                        String member_id = mAlertEditId.getText().trim();
                        doAccountLogin(member_id, mAlertEditPw.getText());
                    }
                    break;
            }
        }
    };
}
