/*
 * 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.c_interface.OnDataSendToReslut;
import com.ntels.onecable.common.http.HttpSend;
import com.ntels.onecable.common.http.Response;
import com.ntels.onecable.common.util.Dlog;
import com.ntels.onecable.common.util.Util;
import com.ntels.onecable.common.widget.CustomActivity;
import com.ntels.onecable.view.dialog.CustomDialog;

import org.json.JSONObject;

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.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

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

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

    private static final int MSG_COM_P_MEMBER_PASSWORD_TEMP = 1;
    private static final int MSG_COM_P_MEMBER_PASSWORD = 2;

    private static final String FIND_ID_CURRENT_STEP = "3";

    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 mLoginId = "";
    private String mCellphone = "";
    private String mSelectedQuizId = "";
    private String mAnswer = "";

    @BindView(R.id.tvComplete1) TextView mNicknameTextView;
    @BindView(R.id.btnResend) Button mResend;

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

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

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

        ButterKnife.bind(this);

        if (null != getIntent()) {
            mNickname = getIntent().getStringExtra(Constants.EXTRA_NICKNAME);
            mLoginId = getIntent().getStringExtra(Constants.EXTRA_MEMBER_ID);
            mCellphone = getIntent().getStringExtra(Constants.EXTRA_PHONE);
            mSelectedQuizId = getIntent().getStringExtra(Constants.EXTRA_QUIZ_ID);
            mAnswer = getIntent().getStringExtra(Constants.EXTRA_QUIZ_ANSWER);
        }

        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.btnResend, R.id.llBtn1})
    public void onClick(View view) {

        switch (view.getId()) {
            case R.id.btnResend:
                mComHandler.sendMessageDelayed(mComHandler.obtainMessage(MSG_COM_P_MEMBER_PASSWORD), Constants.HANDLER_DELAY_MILLIS);
                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_TEMP:
                    sendPostMemberPasswordTemp();
                    break;
                case MSG_COM_P_MEMBER_PASSWORD:
                    sendPostMemberPassword();
                    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_password_3rd_01));
        mUtil.getTextView(toolbar, R.id.tvCurrentStep).setText(FIND_ID_CURRENT_STEP);
        mUtil.getTextView(toolbar, R.id.tvTotalStep).setText(Constants.FIND_PASSWORD_TOTAL_STEP);
    }

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

        mNicknameTextView.setText(getString(R.string.layout_find_password_3rd_02, mNickname));
    }

    /**
     * [REST][POST] 임시비밀번호 재발송 요청.
     *
     */
    private void sendPostMemberPasswordTemp() {
        try {
            HttpSend httpSend;
            httpSend = new HttpSend(mActivity);
            httpSend.setOnDataSendToReslutEvent(new OnDataSendToReslut() {
                @Override
                public void resultResponse(Response result) {

                    resultPostMemberPasswordTemp(result);
                }
            });

//            httpSend.postMemberPasswordTemp();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    /**
     * 비밀번호 찾기 요청 결과.
     *
     * @param result 결과 data
     */
    private void resultPostMemberPasswordTemp(Response result) {
        try {
            if (result.getStatusCode() == 200) {
                mUtil.setShowToast(mActivity, getString(R.string.MSG938), Toast.LENGTH_SHORT);
            } else {
                JSONObject joResult = new JSONObject(result.getContent());
                Dlog.v(joResult.toString());
                String resultCode = mUtil.null2string(joResult.get(Constants.KEY_RESULT_CODE));
                String resultMsg = mUtil.null2string(joResult.get(Constants.KEY_RESULT_MSG));

                mUtil.setShowToast(mActivity, resultMsg, Toast.LENGTH_SHORT);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            mCustomDialog.msgAlert(mActivity, Constants.MSG807);
        }
    }

    /**
     * [REST][POST] 비밀번호 찾기 요청.
     *
     */
    private void sendPostMemberPassword() {
        try {
            HttpSend httpSend;
            httpSend = new HttpSend(mActivity);
            httpSend.setOnDataSendToReslutEvent(new OnDataSendToReslut() {
                @Override
                public void resultResponse(Response result) {

                    resultPostMemberPassword(result);
                }
            });

            httpSend.postMemberPassword(mLoginId, mCellphone, mSelectedQuizId, mAnswer);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    /**
     * 비밀번호 찾기 요청 결과.
     *
     * @param result 결과 data
     */
    private void resultPostMemberPassword(Response result) {
        try {
            if (result.getStatusCode() == 200) {
                mUtil.setShowToast(mActivity, getString(R.string.MSG938), Toast.LENGTH_SHORT);
            } else {
                JSONObject joResult = new JSONObject(result.getContent());
                Dlog.v(joResult.toString());
                String resultCode = mUtil.null2string(joResult.get(Constants.KEY_RESULT_CODE));
                String resultMsg = mUtil.null2string(joResult.get(Constants.KEY_RESULT_MSG));

                mUtil.setShowToast(mActivity, resultMsg, Toast.LENGTH_SHORT);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            mCustomDialog.msgAlert(mActivity, Constants.MSG807);
        }
    }

}
