/*
 * Copyright (c) 2015. Ncomz. All Rights Reserved
 */

package com.ntels.onecable.view;

import com.ntels.onecable.R;
import com.ntels.onecable.common.AppConfig;
import com.ntels.onecable.common.Constants;
import com.ntels.onecable.common.util.Util;
import com.ntels.onecable.common.widget.CustomActivity;
import com.ntels.onecable.view.dialog.CustomDialog;

import android.app.Activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.Toolbar;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.AbsoluteSizeSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

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

/**
 * <pre>
 * 1. ClassName :   FindId2nd
 * 2. FileName  :   FindId2nd.java
 * 3. Package   :   com.ntels.onecable.view
 * 4. Date      :   2016-11-28
 * 5. Author    :   aaron
 * 6. Comment   :   Initialize
 * </pre>
 */
public class FindId2nd extends CustomActivity implements View.OnClickListener,
        CustomDialog.OnMsgAlterToResult {

    private static final int MSG_COM_P_MEMBER_PASSWORD = 1;

    private static final String FIND_ID_CURRENT_STEP = "2";

    private Activity mActivity;
    private Intent mIntent;
    private AppConfig mAppConfig;
    private Util mUtil = new Util();
    private CustomDialog mCustomDialog = new CustomDialog();
    private int mType = 0;
    private String mNickname = "";
    private String mId = "";

    @BindView(R.id.tvComplete1) TextView mNicknameTextView;
    @BindView(R.id.tvComplete2) TextView mIdTextView;

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        mActivity = FindId2nd.this;
        mAppConfig = (AppConfig) mActivity.getApplicationContext();
        mCustomDialog.setOnMsgAlterToResultEvent(this);

        View viewToLoad = LayoutInflater.from(mActivity).inflate(R.layout.find_id_2nd, null);
        this.setContentView(viewToLoad);

        ButterKnife.bind(this);

        if (null != getIntent()) {
            mNickname = getIntent().getStringExtra(Constants.EXTRA_NICKNAME);
            mId = getIntent().getStringExtra(Constants.EXTRA_MEMBER_ID);
        }

        setToolbar();
        displayListView();
    }

    @Override
    public void onBackPressed() {

        mIntent = new Intent();
        mActivity.setResult(RESULT_CANCELED, mIntent);
        finish();
        return;
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {

        super.onConfigurationChanged(newConfig);
    }

    @OnClick({R.id.llBtn1, R.id.llBtn2})
    public void onClick(View view) {

        switch (view.getId()) {
            case R.id.llBtn2:
                mIntent = new Intent(mActivity, FindPassword1st.class);
                mUtil.callActivity(mActivity, mIntent);
                finish();
                break;
            case R.id.llBtn1:
                onBackPressed();
                break;
            default:
                break;
        }
    }

    @Override
    public void resultCustomDialogResponse(int msgId, int btnResult) {

        switch (msgId) {

            case Constants.MSG807:
                if (btnResult == 1) {
                    mComHandler.sendMessageDelayed(mComHandler.obtainMessage(mType),
                            Constants.HANDLER_DELAY_MILLIS);
                } else {
                    mActivity.finish();
                }
                break;
            case Constants.MSG840:
                mActivity.finish();
                break;
            default:
                break;
        }
    }

    Handler mComHandler = new Handler(new IncomingHandlerCallBack());

    class IncomingHandlerCallBack implements Handler.Callback {

        @Override
        public boolean handleMessage(Message msg) {

            mType = msg.what;
            switch (msg.what) {
                case MSG_COM_P_MEMBER_PASSWORD:
                    break;
                default:
                    break;
            }
            return true;
        }
    }

    /**
     * 툴바 셋팅.
     */
    private void setToolbar() {

        Toolbar toolbar = (Toolbar) mActivity.findViewById(R.id.toolbar);
        mUtil.setKeyboardHide(mActivity, toolbar);
        mUtil.getTextView(toolbar, R.id.tvToolbarTitle1).setVisibility(View.VISIBLE);
        mUtil.getTextView(toolbar, R.id.tvToolbarTitle2).setVisibility(View.GONE);
        mUtil.getTextView(toolbar, R.id.tvToolbarTitleDivider).setVisibility(View.GONE);
        mUtil.getTextView(toolbar, R.id.tvToolbarTitle3).setVisibility(View.GONE);
        mUtil.getTextView(toolbar, R.id.tvToolbarTitle4).setVisibility(View.GONE);
        mUtil.getView(toolbar, R.id.llJoinStep).setVisibility(View.VISIBLE);
        mUtil.getTextView(toolbar, R.id.btnToolbarClose).setVisibility(View.GONE);
        mUtil.getTextView(toolbar, R.id.btnToolbarMore).setVisibility(View.GONE);

        mUtil.getTextView(toolbar, R.id.tvToolbarTitle1).setText(mUtil.getString(mActivity, R.string.layout_find_id_2nd_01));
        mUtil.getTextView(toolbar, R.id.tvCurrentStep).setText(FIND_ID_CURRENT_STEP);
        mUtil.getTextView(toolbar, R.id.tvTotalStep).setText(Constants.FIND_ID_TOTAL_STEP);
    }

    /**
     * UI display.
     */
    private void displayListView() {

        String nickname = "".equals(mNickname) ? getString(R.string.layout_find_id_2nd_04) : mNickname;
        mNicknameTextView.setText(getString(R.string.layout_find_id_2nd_02, nickname));

        // ID를 나타내는 부분만 텍스트 사이즈 변경.
        String completeMsg = getString(R.string.layout_find_id_2nd_03, mId);
        SpannableStringBuilder sps = new SpannableStringBuilder(completeMsg);
        sps.setSpan(new AbsoluteSizeSpan(((int) getResources().getDimension(R.dimen.size_font_08)), false), 0, (mId.length()),
                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        mIdTextView.append(sps);
    }

}
