package kr.co.hanwha.hifive.screen.intro;

import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.ScaleAnimation;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import kr.co.hanwha.hifive.R;
import kr.co.hanwha.hifive.base.BaseActivity;
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.screen.intro.contract.IntroContract;
import kr.co.hanwha.hifive.util.HFActivityUtil;
import kr.co.hanwha.hifive.util.HFCommonUtil;
import kr.co.hanwha.hifive.view.HFAlertEditTextView;
import kr.co.hanwha.hifive.view.HFNumberKeypadView;
import kr.co.hanwha.hifive.view.HFPasswordEditTextView;

/**
 * SPLASH, 아이디 로그인, 간편 로그인 Presenter
 *
 * @see IntroPresenter
 * Created by sanghoonkwon on 2018. 1. 10..
 */

public class IntroActivity extends BaseActivity implements IntroContract.View {
    private HFPasswordEditTextView mPasswordEdit;
    private HFNumberKeypadView mNumberKeypadView;
    private TextView mTextViewAlert;
    private LinearLayout mLayoutAccountLogin;
    private ScrollView mScrollView;
    private HFAlertEditTextView mAlertEditId;
    private HFAlertEditTextView mAlertEditPw;
    private LinearLayout mLayoutSimpleLogin;
    private ImageView mImageViewIcon;

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

    private IntroContract.Presenter mPresenter;

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

    @Override
    protected BaseToolbarType isToolbarStyle() {
        return BaseToolbarType.NONE;
    }

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

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

        mPasswordEdit = (HFPasswordEditTextView) findViewById(R.id.passwordEditText);
        mPasswordEdit.setOnInputPasswordListener(onInputPasswordListener);

        mNumberKeypadView = (HFNumberKeypadView) findViewById(R.id.numberKeypadView);
        mNumberKeypadView.setOnNumberKeypadListener(onNumberKeypadListener);

        mTextViewAlert = (TextView) findViewById(R.id.alert_textView);

        mLayoutAccountLogin = (LinearLayout) findViewById(R.id.accountLogin_linearLayout);
        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);

        mLayoutSimpleLogin = (LinearLayout) findViewById(R.id.simpleLogin_linearLayout);

        mImageViewIcon = (ImageView) findViewById(R.id.icon_imageView);

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

        mPresenter = new IntroPresenter(this);
        mPresenter.checkUpdate();
    }

    @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() {
                        finish();
                    }
                });

    }

    @Override
    public void showAlertMessage(String message) {
        mTextViewAlert.setText(message);

    }

    @Override
    public void showSimpleLogin() {
        mLayoutSimpleLogin.setVisibility(View.VISIBLE);
        findViewById(R.id.intro_linearLayout).setVisibility(View.GONE);
        int DURATION_ICON = 500;
        int DURATION_LAYOUT = 800;

        Animation anim = new AlphaAnimation(0.0f, 1.0f);
        anim.setDuration(DURATION_LAYOUT);
        mLayoutSimpleLogin.startAnimation(anim);

        int screenHeight = HFCommonUtil.getScreenHeight(this);
        int largeWidth = HFCommonUtil.getPixelFromDimension(this, R.dimen.intro_icon_large_width);
        int targetY = (screenHeight - largeWidth) / 2 + HFCommonUtil.getPixelFromDimension(this, R.dimen.intro_icon_margin_top) * 2;//TODO - CHANGE
        mImageViewIcon.setTranslationY(0);
        mImageViewIcon.animate()
                .translationY(-targetY)
                .setDuration(DURATION_ICON)
                .start();

        float startScale = 1.0f;
        float endScale = 0.60f;
        Animation animScale = new ScaleAnimation(
                startScale, endScale,
                startScale, endScale,
                Animation.RELATIVE_TO_SELF, 0.5f,
                Animation.RELATIVE_TO_SELF, 0.5f);
        animScale.setFillAfter(true);
        animScale.setDuration(DURATION_ICON);
        mImageViewIcon.startAnimation(animScale);
    }

    @Override
    public void showAccountLogin() {
        mLayoutAccountLogin.setVisibility(View.VISIBLE);
        findViewById(R.id.intro_linearLayout).setVisibility(View.GONE);
        int DURATION_ICON = 800;
        int DURATION_LAYOUT = 1200;

        Animation anim = new AlphaAnimation(0.0f, 1.0f);
        anim.setDuration(DURATION_LAYOUT);
        mLayoutAccountLogin.startAnimation(anim);

        int screenHeight = HFCommonUtil.getScreenHeight(this);
        int largeWidth = HFCommonUtil.getPixelFromDimension(this, R.dimen.intro_icon_large_width);
        int targetY = (screenHeight - largeWidth) / 2 + HFCommonUtil.getPixelFromDimension(this, R.dimen.intro_icon_margin_top) * 2;//TODO - CHANGE
        mImageViewIcon.setTranslationY(0);
        mImageViewIcon.animate()
                .translationY(-targetY)
                .setDuration(DURATION_ICON)
                .start();

        float startScale = 1.0f;
        float endScale = 0.60f;
        Animation animScale = new ScaleAnimation(
                startScale, endScale,
                startScale, endScale,
                Animation.RELATIVE_TO_SELF, 0.5f,
                Animation.RELATIVE_TO_SELF, 0.5f);
        animScale.setFillAfter(true);
        animScale.setDuration(DURATION_ICON);
        mImageViewIcon.startAnimation(animScale);
    }

    /**************************************************
     * 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;
    }

    /**************************************************
     * View.OnClickListener
     **************************************************/
    View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.start_button:
                    if (checkLoginData()) {
                        mPresenter.doAccountLogin(mAlertEditId.getText(), mAlertEditPw.getText());
                    }
                    break;
            }
        }
    };
    /**************************************************
     * 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;
            }
        }
    };

    /**************************************************
     * NumberKeypadView.OnNumberKeypadListener
     **************************************************/
    HFNumberKeypadView.OnNumberKeypadListener onNumberKeypadListener = new HFNumberKeypadView.OnNumberKeypadListener() {
        @Override
        public void onNumberClicked(int num) {
            mPasswordEdit.setNum(num);
        }

        @Override
        public void onDeleteClicked() {
            mPasswordEdit.delNum();
        }

        @Override
        public void easterEgg() {
            showAccountLogin();
        }
    };

    /**************************************************
     * PasswordEditTextView.OnInputPasswordListener
     **************************************************/
    HFPasswordEditTextView.OnInputPasswordListener onInputPasswordListener = new HFPasswordEditTextView.OnInputPasswordListener() {
        @Override
        public void onCompleteInput(String password) {
            mPresenter.doSimpleLogin(password);
        }

        @Override
        public void onErrorMessage(String message) {
            showToast(message);
        }

        @Override
        public void onTextChanged() {
            if (mPasswordEdit.getText().length() > 0)
                showAlertMessage("");
        }
    };
}
