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

package com.ntels.kneet.v3.view.main_more;

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.Toast;

import com.ntels.kneet.v3.common.AppConfig;
import com.ntels.kneet.v3.common.Constants;
import com.ntels.kneet.v3.common.http.HttpSend;
import com.ntels.kneet.v3.common.http.Response;
import com.ntels.kneet.v3.common.c_interface.Main_Interface;
import com.ntels.kneet.v3.common.c_interface.OnDataSendToReslut;
import com.ntels.kneet.v3.common.util.Dlog;
import com.ntels.kneet.v3.common.util.Util;
import com.ntels.kneet.v3.common.widget.CustomActivity;
import com.ntels.kneet.v3.R;
import com.ntels.kneet.v3.view.dialog.CustomDialog;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;

public class Homehub_Password_Change extends CustomActivity implements View.OnClickListener,
                                                                       CustomDialog.OnMsgAlterToResult {

    /*툴바 관련*/
    private Toolbar toolbar;

    /*통신 관련*/
    private              ArrayList<HashMap<String, Object>> alTotalList                               = new ArrayList<HashMap<String, Object>>();
    private              int                                iType                                     = 0;
    private static final int                                MSG_COM_P_DEVICES_DEVICE_ID_PASSWORD = 1;

    /*공통*/
    private Activity       act;
    private AppConfig      appConfig;
    private Main_Interface mCallback;
    private Intent         intent;
    private Util         util                         = new Util();
    private CustomDialog customDialog                 = new CustomDialog();
    private String       sCallType                    = "";
    private String       sHTTP_new_owner_auth_password = "";

    /**
     * <PRE>
     * 1. MethodName: onCreate
     * 2. ClassName :
     * 3. Comment   : onCreate
     * 4. Developer : 최진오
     * 5. Created   : 2015. 10.
     * </PRE>
     */
    @Override public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

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

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

        if ( savedInstanceState != null ) {
            // sTitle =
            // savedInstanceState.getString(Constants.EXTRA_SPINNER_TITLE);
            // aList1 =
            // savedInstanceState.getStringArrayList(Constants.EXTRA_SPINNER_LIST1);
            // aList2 =
            // savedInstanceState.getStringArrayList(Constants.EXTRA_SPINNER_LIST2);
            // // 0: 세로 한개 선택, 1: 가로 한개선택
            // iType = savedInstanceState.getInt(Constants.EXTRA_SPINNER_TYPE,
            // 0);
            // sBtn1 =
            // savedInstanceState.getString(Constants.EXTRA_SPINNER_BTN1);
            // sBtn2 =
            // savedInstanceState.getString(Constants.EXTRA_SPINNER_BTN2);
            // iPosition =
            // savedInstanceState.getInt(Constants.EXTRA_SPINNER_POSITION, 0);
        } else {
            Intent intent = getIntent();
            sCallType = intent.getStringExtra(Constants.EXTRA_CALL_TYPE);
            //            sHTTP_old_password = intent.getStringExtra(Constants.EXTRA_PASSWORD);
        }

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

        //        sHTTP_update_field = "password";
    }

    @Override public void onConfigurationChanged(Configuration newConfig) {

        super.onConfigurationChanged(newConfig);
    }

    @Override public void onClick(View v) {

        switch (v.getId()) {
            case R.id.llBtn1:
                sHTTP_new_owner_auth_password = util.getEditTextVal(act, R.id.edtPass);
                if ( sHTTP_new_owner_auth_password.equals("") ) {
                    util.setShowToast(act, util.getString(act, R.string.MSG829), Toast.LENGTH_SHORT);
                    return;
                }
                if ( util.getEditTextVal(act, R.id.edtPassConfirm).equals("") ) {
                    util.setShowToast(act, util.getString(act, R.string.MSG830), Toast.LENGTH_SHORT);
                    return;
                }
                if ( !sHTTP_new_owner_auth_password.equals(util.getEditTextVal(act, R.id.edtPassConfirm)) ) {
                    util.setShowToast(act, util.getString(act, R.string.MSG863) + util.getString(act, R.string.MSG902), Toast.LENGTH_SHORT);
                    return;
                }
                v.setEnabled(false);
                ComHandler.sendMessageDelayed(ComHandler.obtainMessage(MSG_COM_P_DEVICES_DEVICE_ID_PASSWORD), 10);
                break;
            case R.id.llBtn2:
                onBackPressed();
                break;
            default:
                break;
        }
    }

    Handler ComHandler = new Handler(new IncomingHandlerCallBack());

    class IncomingHandlerCallBack implements Handler.Callback {

        @Override public boolean handleMessage(Message msg) {

            iType = msg.what;
            switch (msg.what) {
                case MSG_COM_P_DEVICES_DEVICE_ID_PASSWORD:
                    //dialog_show();
                    send_P_devices_device_id_password();
                    break;

                default:
                    break;
            }
            return true;
        }
    }
    /**
     * <PRE>
     * 1. MethodName: send_P_devices_device_id_password
     * 2. ClassName :
     * 3. Comment   : [Send][PUT](소유권을 확인할 때 사용하는) 디바이스 비밀번호를 변경한다.)
     * 4. Developer : 정백호
     * 5. Created   : 2016. 06.
     * </PRE>
     */
    private void send_P_devices_device_id_password() {

        try {
            HttpSend httpSend;

            //            httpSend = new HttpSend(act, !swipeRefreshLayout.isRefreshing());
            httpSend = new HttpSend(act);

            httpSend.setOnDataSendToReslutEvent(new OnDataSendToReslut() {
                @Override public void resultResponse(Response result) {

                    result_P_devices_device_id_password(result);
                }
            });
            String sHTTP_device_id = appConfig.getSelect_Homegrp_Homehub_Device_Id();
            httpSend.P_devices_device_id_password(sHTTP_device_id, sHTTP_new_owner_auth_password);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    /**
     * <PRE>
     * 1. MethodName: result_P_devices_device_id_password
     * 2. ClassName :
     * 3. Comment   : [Result][PUT](소유권을 확인할 때 사용하는) 디바이스 비밀번호를 변경한다.)
     * 4. Developer : 정백호
     * 5. Created   : 2016. 06.
     * </PRE>
     */
    private void result_P_devices_device_id_password(Response rpResult) {

        try {
            if ( rpResult.getStatusCode() == 200 ) {
                util.setShowToast(act, util.getString(act, R.string.MSG859), Toast.LENGTH_SHORT);
                intent = new Intent();
                act.setResult(RESULT_OK, intent);
                finish();
            } else {
                JSONObject joResult = new JSONObject(rpResult.getContent());
                Dlog.v(joResult.toString());
                String sERR_CODE = util.null2string(joResult.get(Constants.HTTP_err_code));
                String sERR_MSG = util.null2string(joResult.get(Constants.HTTP_err_msg));

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

        }
    }

    /**
     * <PRE>
     * 1. MethodName: onBackPressed
     * 2. ClassName :
     * 3. Comment   : 뒤로가기 버튼
     * 4. Developer : 최진오
     * 5. Created   : 2015. 10.
     * </PRE>
     */
    @Override public void onBackPressed() {

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

    /**
     * <PRE>
     * 1. MethodName: onResume
     * 2. ClassName :
     * 3. Comment   : onResume
     * 4. Developer : 최진오
     * 5. Created   : 2015. 10.
     * </PRE>
     */
    @Override public void onResume() {

        super.onResume();

        // if (!GreenBilllab.getIntro()) {
        // if (Main.reLoginCheck()) {
        // onBackPressed();
        // }
        // }
    }

    @Override public void resultCustomDialogResponse(int iMsgID, int iBtnResult) {

        switch (iMsgID) {

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