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

package com.ntels.onecable.view.mainmore;

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

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

    private static final int MSG_COM_G_MEMBER_MEMBER_ID = 1;
    Handler comHandler = new Handler(new IncomingHandlerCallBack());
    /*통신 관련*/
    private int tag = 0;
    /*공통*/
    private Activity act;
    private AppConfig appConfig;
    private Intent intent;
    private Util util = new Util();
    private CustomDialog customDialog = new CustomDialog();
    private String callType = "";
    private String oldPassword = "";

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        act = SettingPasswordCheck.this;
        appConfig = (AppConfig) act.getApplicationContext();
        customDialog.setOnMsgAlterToResultEvent(this);

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

        if (null != getIntent()) {
            Intent intent = getIntent();
            callType = intent.getStringExtra(Constants.EXTRA_CALL_TYPE);
        }

        util.getView(act, R.id.llBtn1).setOnClickListener(this);
        util.getView(act, R.id.llBtn2).setOnClickListener(this);

        util.getEditText(act, R.id.edtPassword).setText("");
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {

        super.onConfigurationChanged(newConfig);
    }

    @Override
    public void onClick(View view) {

        switch (view.getId()) {
            case R.id.llBtn1:
                oldPassword = util.getEditTextVal(act, R.id.edtPassword);
                if (oldPassword.equals("")) {
                    util.setShowToast(act, util.getString(act, R.string.MSG847), Toast.LENGTH_SHORT);
                    return;
                }

                view.setEnabled(false);
                comHandler.sendMessageDelayed(comHandler.obtainMessage(MSG_COM_G_MEMBER_MEMBER_ID), 10);
                break;
            case R.id.llBtn2:
                onBackPressed();
                break;
            default:
                break;
        }
    }

    /**
     * (기존)비밀번호 확인(비밀번호 정보를 확인한다) 요청.
     */
    private void sendGetMemberPassword() {

        try {
            HttpSend httpSend = new HttpSend(act);
            httpSend.setOnDataSendToReslutEvent(new OnDataSendToReslut() {
                @Override
                public void resultResponse(Response result) {

                    resultGetMemberPassword(result);
                }
            });

            //공통 변수 입력
            httpSend.G_member_member_id(appConfig.getLogin_Member_Id(), oldPassword);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    /**
     * {@link #sendGetMemberPassword()} 요청 결과.
     *
     * @param response 결과 data
     */
    private void resultGetMemberPassword(Response response) {

        try {
            if (response.getStatusCode() == 200) {
                if (callType.equals("1")) {
                    intent = new Intent(act, SettingPasswordChange.class);
                    intent.putExtra(Constants.EXTRA_PASSWORD, oldPassword);
                    util.callActivityForResultNoAnim(act, intent, Constants.ACT_SETTING_PASSWORD_CHANGE);
                }
            } else {
                JSONObject joResult = new JSONObject(response.getContent());
                Dlog.v(joResult.toString());
                String errorCode = util.null2string(joResult.get(Constants.KEY_ERR_CODE));
                String errorMsg = util.null2string(joResult.get(Constants.KEY_ERR_MSG));

                if (errorCode.equals("401")) {
                    customDialog.msgAlert(act, Constants.MSG998, errorMsg);
                } else if (errorCode.equals("401-106")) {
                    customDialog.msgAlert(act, Constants.MSG997, errorMsg);
                } else {
                    customDialog.msgAlert(act, Constants.MSG000, errorMsg);
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            customDialog.msgAlert(act, Constants.MSG807);
        }
        try {
            util.getView(act, R.id.llBtn1).setEnabled(true);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    @Override
    public void onBackPressed() {

        intent = new Intent();
        act.setResult(RESULT_CANCELED, intent);
        finish();
        return;
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {

        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case Constants.ACT_SETTING_PASSWORD_CHANGE:
                    intent = new Intent();
                    act.setResult(RESULT_OK, intent);
                    finish();
                    break;
                case Constants.ACT_SETTING_HOME_DEL:
                    intent = new Intent();
                    act.setResult(RESULT_OK, intent);
                    finish();
                    break;
                default:
                    break;
            }
        } else {
            switch (requestCode) {
                case Constants.ACT_SETTING_ACCOUNT_DEL_CONFIRM:
                case Constants.ACT_SETTING_PASSWORD_CHANGE:
                    intent = new Intent();
                    act.setResult(RESULT_OK, intent);
                    finish();
                    break;
                default:
                    break;
            }
        }
    }

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

        switch (msgId) {

            case Constants.MSG998:
                util.callApp_Start(act, "LOGOUT");
                break;
            case Constants.MSG997:
                util.callApp_Start(act, "HOME_RELOAD");
                break;
            case Constants.MSG807:
                if (btnResult == 1) {
                    comHandler.sendMessageDelayed(comHandler.obtainMessage(tag), 10);
                }
                break;
            default:
                break;
        }
    }

    class IncomingHandlerCallBack implements Handler.Callback {

        @Override
        public boolean handleMessage(Message msg) {

            tag = msg.what;
            switch (msg.what) {
                case MSG_COM_G_MEMBER_MEMBER_ID:
                    sendGetMemberPassword();
                    break;
                default:
                    break;
            }
            return true;
        }
    }
}
