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

package com.ntels.onecable.admin.view.mainmore;

import com.ntels.onecable.admin.R;
import com.ntels.onecable.admin.common.AppConfig;
import com.ntels.onecable.admin.common.Constants;
import com.ntels.onecable.admin.common.c_interface.OnDataSendToReslut;
import com.ntels.onecable.admin.common.http.HttpSend;
import com.ntels.onecable.admin.common.http.Response;
import com.ntels.onecable.admin.common.util.Dlog;
import com.ntels.onecable.admin.common.util.Util;
import com.ntels.onecable.admin.common.widget.CustomActivity;
import com.ntels.onecable.admin.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 :   Homemode
 * 2. FileName  :   Homemode.java
 * 3. Package   :   com.ntels.onecable.admin.view.mainmore
 * 4. Date      :   2016-12-01
 * 5. Author    :   aaron
 * 6. Comment   :   Initialize
 * </pre>
 */
public class Homemode extends CustomActivity implements View.OnClickListener,
        CustomDialog.OnMsgAlterToResult {

    private static final int MSG_COM_P_MODES_MODE_ID_CURRENT_MODE = 1;
    Boolean isReload = false;
    Handler comHandler = new Handler(new IncomingHandlerCallBack());
    /*통신 관련*/
    private int type = 0;
    /*공통*/
    private Activity mActivity;
    private AppConfig mAppConfig;
    private Intent intent;
    private Util mUtil = new Util();
    private CustomDialog customDialog = new CustomDialog();
    private String modeId = "";
    private String basicModeCode = "";
    private String modeIcon = "";
    private String modeIcon1 = "";
    private String modeName = "";
    private String modeDelYn = "";
    private String homegrpRuleId = "";

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        mActivity = Homemode.this;
        mAppConfig = (AppConfig) mActivity.getApplicationContext();
        customDialog.setOnMsgAlterToResultEvent(this);

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

        if (null != getIntent()) {
            Intent intent = getIntent();
            modeId = intent.getStringExtra(Constants.EXTRA_MODE_ID);
            basicModeCode = intent.getStringExtra(Constants.EXTRA_BASIC_MODE_CODE);
            modeIcon = intent.getStringExtra(Constants.EXTRA_MODE_ICON);
            modeIcon1 = intent.getStringExtra(Constants.EXTRA_MODE_ICON1);
            modeName = intent.getStringExtra(Constants.EXTRA_MODE_NAME);
            modeDelYn = intent.getStringExtra(Constants.EXTRA_MODE_DEL_YN);
            homegrpRuleId = intent.getStringExtra(Constants.EXTRA_HOMEGRP_RULE_ID);
        }

        setDisp();
    }

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

        mUtil.getView(mActivity, R.id.btnModeChange).setOnClickListener(this);
        mUtil.getView(mActivity, R.id.btnModeSetup).setOnClickListener(this);
        mUtil.getView(mActivity, R.id.btnClose).setOnClickListener(this);

        mUtil.setImage(mActivity, modeIcon, mUtil.getImageView(mActivity, R.id.ivModeIcon));
        Dlog.v(modeId, modeId);
        if (basicModeCode.equals("HOME")) {
            mUtil.getTextView(mActivity, R.id.tvTitle)
                    .setText(mUtil.getString(mActivity, R.string.layout_mode_detail_03));
            mUtil.getTextView(mActivity, R.id.tvTitle1)
                    .setText(mUtil.getString(mActivity, R.string.layout_mode_detail_04));
        } else if (basicModeCode.equals("AWAY")) {
            mUtil.getTextView(mActivity, R.id.tvTitle)
                    .setText(mUtil.getString(mActivity, R.string.layout_mode_detail_05));
            mUtil.getTextView(mActivity, R.id.tvTitle1)
                    .setText(mUtil.getString(mActivity, R.string.layout_mode_detail_06));
        } else if (basicModeCode.equals("MORNING")) {
            mUtil.getTextView(mActivity, R.id.tvTitle)
                    .setText(mUtil.getString(mActivity, R.string.layout_mode_detail_07));
            mUtil.getTextView(mActivity, R.id.tvTitle1)
                    .setText(mUtil.getString(mActivity, R.string.layout_mode_detail_08));
        } else if (basicModeCode.equals("NIGHT")) {
            mUtil.getTextView(mActivity, R.id.tvTitle)
                    .setText(mUtil.getString(mActivity, R.string.layout_mode_detail_09));
            mUtil.getTextView(mActivity, R.id.tvTitle1)
                    .setText(mUtil.getString(mActivity, R.string.layout_mode_detail_10));
        }

        mUtil.getButton(mActivity, R.id.btnModeChange).setText(modeName);

        mUtil.getView(mActivity, R.id.btnModeSetup).setEnabled(true);
        if (mAppConfig.getCtrtGroupGradeCode().equals("MAS")) {
            mUtil.getTextView(mActivity, R.id.tvDesc).setVisibility(View.INVISIBLE);
            if (homegrpRuleId.equals("")) {
                mUtil.getTextView(mActivity, R.id.tvDesc).setVisibility(View.VISIBLE);
                mUtil.getTextView(mActivity, R.id.tvDesc)
                        .setText(mUtil.getString(mActivity, R.string.layout_mode_detail_01));
            } else if (!modeDelYn.equals("0")) {
                mUtil.getTextView(mActivity, R.id.tvDesc).setVisibility(View.VISIBLE);
                mUtil.getTextView(mActivity, R.id.tvDesc)
                        .setText(mUtil.getString(mActivity, R.string.layout_mode_detail_02));
            }
        } else {
            mUtil.getTextView(mActivity, R.id.tvDesc).setVisibility(View.INVISIBLE);
            mUtil.getView(mActivity, R.id.btnModeSetup).setVisibility(View.INVISIBLE);
        }
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {

        super.onConfigurationChanged(newConfig);
    }

    @Override
    public void onClick(View view) {

        switch (view.getId()) {
//            case R.id.btnModeSetup:
//                mUtil.callBtnGoogleAnalytics(mActivity, "Set_Mode", modeName);
//                intent = new Intent(mActivity, HomemodeEdit.class);
//                intent.putExtra(Constants.EXTRA_MODE_ID, modeId);
//                intent.putExtra(Constants.EXTRA_MODE_ICON, modeIcon);
//                intent.putExtra(Constants.EXTRA_MODE_ICON1, modeIcon1);
//                intent.putExtra(Constants.EXTRA_MODE_NAME, modeName);
//                intent.putExtra(Constants.EXTRA_MODE_DEL_YN, modeDelYn);
//                intent.putExtra(Constants.EXTRA_HOMEGRP_RULE_ID, homegrpRuleId);
//                mUtil.callActivityForResultNoAnim(mActivity, intent, Constants.ACT_HOMEMODE_EDIT);
//                break;
            case R.id.btnClose:
                onBackPressed();
                break;
            case R.id.btnModeChange:
                if (mAppConfig.getSelectCtrtGroupHomehubOnlineState().equals("ON")) {
                    mUtil.callBtnGoogleAnalytics(mActivity, "Chage_Mode", modeName);
                    mUtil.getView(mActivity, R.id.progressLoading).setVisibility(View.VISIBLE);
                    mUtil.getView(mActivity, R.id.btnModeChange).setVisibility(View.INVISIBLE);
                    mUtil.getView(mActivity, R.id.ivSucess).setVisibility(View.INVISIBLE);
                    mUtil.setVibrator(100);
                    comHandler.sendMessageDelayed(comHandler.obtainMessage(MSG_COM_P_MODES_MODE_ID_CURRENT_MODE), 10);
                } else {
                    mUtil.setShowToast(mActivity, mUtil.getString(mActivity, R.string.MSG866), Toast.LENGTH_SHORT);
                    onBackPressed();
                }
                break;
            default:
                break;
        }
    }

    /**
     * 현재 홈모드 변경(현재 홈모드를 변경한다) 요청.
     */
    private void sendPutCurrentMode() {

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

                    resultPutCurrentMode(result);
                }
            });

            //공통 변수 입력
//      httpSend.putMode(modeId);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

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

        try {
            if (response.getStatusCode() == 200) {
                mUtil.getView(mActivity, R.id.btnModeSetup).setEnabled(true);
                mUtil.getImageView(mActivity, R.id.ivModeIcon).setVisibility(View.INVISIBLE);
                mUtil.getTextView(mActivity, R.id.tvTitle)
                        .setText(modeName + mUtil.getString(mActivity, R.string.layout_mode_detail_12));
                mUtil.getTextView(mActivity, R.id.tvTitle1).setText("");
                mUtil.getTextView(mActivity, R.id.tvTitle1).setVisibility(View.INVISIBLE);
                mUtil.getView(mActivity, R.id.progressLoading).setVisibility(View.INVISIBLE);
                mUtil.getView(mActivity, R.id.btnModeChange).setVisibility(View.INVISIBLE);
                mUtil.getView(mActivity, R.id.ivSucess).setVisibility(View.VISIBLE);
                isReload = true;
            } else {
                JSONObject joResult = new JSONObject(response.getContent());
                Dlog.v(joResult.toString());
//                String errorCode = mUtil.null2string(joResult.get(Constants.KEY_ERR_CODE));
                String resultCode = mUtil.null2string(joResult.get(Constants.KEY_RESULT_CODE));
//                String errorMsg = mUtil.null2string(joResult.get(Constants.KEY_ERR_MSG));
                String resultMsg = mUtil.null2string(joResult.get(Constants.KEY_RESULT_MSG));

                if (resultCode.equals("401")) {
                    customDialog.msgAlert(mActivity, Constants.MSG998, resultMsg);
                } else if (resultCode.equals("401-106")) {
                    customDialog.msgAlert(mActivity, Constants.MSG997, resultMsg);
                } else {
                    customDialog.msgAlert(mActivity, Constants.MSG000, resultMsg);
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            customDialog.msgAlert(mActivity, Constants.MSG807);
        }
    }

    @Override
    public void onBackPressed() {

        intent = new Intent();
        intent.putExtra(Constants.EXTRA_RELOAD_FLAG, isReload);
        mActivity.setResult(RESULT_CANCELED, intent);
        finish();
    }

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

        switch (msgId) {
            case Constants.MSG998:
                mUtil.callApp_Start(mActivity, "LOGOUT");
                break;
            case Constants.MSG997:
                mUtil.callApp_Start(mActivity, "HOME_RELOAD");
                break;
            case Constants.MSG807:
                if (btnResult == 1) {
                    comHandler.sendMessageDelayed(comHandler.obtainMessage(type), 10);
                }
                break;
            default:
                break;
        }
    }

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

        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case Constants.ACT_HOMEMODE_EDIT:
                    isReload = true;
                    modeDelYn = mUtil.null2string(data.getStringExtra(Constants.EXTRA_MODE_DEL_YN));
                    homegrpRuleId = mUtil.null2string(data.getStringExtra(Constants.EXTRA_HOMEGRP_RULE_ID));
                    setDisp();
                    break;
                default:
                    break;
            }
        } else {
            switch (requestCode) {
                default:
                    break;
            }
        }
    }

    class IncomingHandlerCallBack implements Handler.Callback {

        @Override
        public boolean handleMessage(Message msg) {

            type = msg.what;
            switch (msg.what) {
                case MSG_COM_P_MODES_MODE_ID_CURRENT_MODE:
                    sendPutCurrentMode();
                    break;

                default:
                    break;
            }
            return true;
        }
    }
}
