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

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

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 com.ntels.kneet.v3.common.AppConfig;
import com.ntels.kneet.v3.common.Constants;
import com.ntels.kneet.v3.common.db.SQLManager;
import com.ntels.kneet.v3.common.c_interface.Main_Interface;
import com.ntels.kneet.v3.common.util.Util;
import com.ntels.kneet.v3.common.widget.CustomActionBarActivity;
import com.ntels.kneet.v3.R;
import com.ntels.kneet.v3.view.dialog.CustomDialog;

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

public class Things_Device_Add extends CustomActionBarActivity 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_MEMBER_PROFILE_IMAGE = 1;
    //    private static final int                                MSG_COM_G_MEMBER               = 2;
    //    private static final int                                MSG_COM_P_MEMBER_PUSH_NOTI     = 3;

    /*공통*/
    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 SQLManager   sqlManager        = new SQLManager();
    private boolean      bReloadFinishFlag = false;

    @Override public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

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

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

        setToolbar();
        setDisp();
    }

    private void setToolbar() {

        toolbar = (Toolbar) act.findViewById(R.id.toolbar);
        util.setKeyboardHide(act, toolbar);
        util.getTextView(toolbar, R.id.tvToolbarTitle1).setVisibility(View.VISIBLE);
        util.getTextView(toolbar, R.id.tvToolbarTitle2).setVisibility(View.GONE);
        util.getTextView(toolbar, R.id.tvToolbarTitleDivider).setVisibility(View.GONE);
        util.getTextView(toolbar, R.id.tvToolbarTitle3).setVisibility(View.GONE);
        util.getTextView(toolbar, R.id.tvToolbarTitle4).setVisibility(View.GONE);
        util.getTextView(toolbar, R.id.btnToolbarDel).setVisibility(View.GONE);
        util.getTextView(toolbar, R.id.btnToolbarClose).setVisibility(View.GONE);
        util.getTextView(toolbar, R.id.btnToolbarMore).setVisibility(View.GONE);

        util.getTextView(toolbar, R.id.tvToolbarTitle1).setText(util.getString(act, R.string.layout_things_device_add_01));
    }

    private void setDisp() {

        util.getView(act, R.id.llRowDevice1).setOnClickListener(this);
        util.getView(act, R.id.llRowDevice2).setOnClickListener(this);
        util.getView(act, R.id.llRowDevice3).setOnClickListener(this);
        util.getView(act, R.id.llRowDevice4).setOnClickListener(this);
        util.getView(act, R.id.llRowDevice5).setOnClickListener(this);
        util.getView(act, R.id.llRowDevice6).setOnClickListener(this);
        util.getView(act, R.id.llRowDevice7).setOnClickListener(this);
        util.getView(act, R.id.llBtn1).setOnClickListener(this);
    }
    @Override public void onConfigurationChanged(Configuration newConfig) {

        super.onConfigurationChanged(newConfig);
    }

    @Override public void onClick(View v) {
        Intent intent;
        switch (v.getId()) {
            case R.id.llBtn1:
                onBackPressed();
                break;
            case R.id.llRowDevice1:
                intent = new Intent(act, Things_Device_Add_01.class);
                intent.putExtra(Constants.EXTRA_DEVICE_NAME, util.getString(act, R.string.layout_things_device_add_02));
                intent.putExtra(Constants.EXTRA_COMPANY_NAME, util.getString(act, R.string.layout_things_device_add_03));
                intent.putExtra(Constants.EXTRA_DEVICE_DESC, util.getString(act, R.string.layout_things_device_add_del_01));
                intent.putExtra(Constants.EXTRA_DEVICE_DESC1, util.getString(act, R.string.layout_things_device_add_del_04));
                intent.putExtra(Constants.EXTRA_DEVICE_IMAGE, R.drawable.img_things_product_addimg_01_smartgasvalve);
                intent.putExtra(Constants.EXTRA_DEVICE_IMAGE1, R.drawable.img_things_product_addimg_01_smartgasvalve_wait);
                util.callActivityForResultNoAnim(act, intent, Constants.ACT_THINGS_DEVICE_ADD_01);
                break;
            case R.id.llRowDevice2:
                intent = new Intent(act, Things_Device_Add_01.class);
                intent.putExtra(Constants.EXTRA_DEVICE_NAME, util.getString(act, R.string.layout_things_device_add_04));
                intent.putExtra(Constants.EXTRA_COMPANY_NAME, util.getString(act, R.string.layout_things_device_add_05));
                intent.putExtra(Constants.EXTRA_DEVICE_DESC, util.getString(act, R.string.layout_things_device_add_del_02));
                intent.putExtra(Constants.EXTRA_DEVICE_DESC1, util.getString(act, R.string.layout_things_device_add_del_05));
                intent.putExtra(Constants.EXTRA_DEVICE_IMAGE, R.drawable.img_things_product_addimg_02_iotplug);
                intent.putExtra(Constants.EXTRA_DEVICE_IMAGE1, R.drawable.img_things_product_addimg_02_iotplug_wait);
                util.callActivityForResultNoAnim(act, intent, Constants.ACT_THINGS_DEVICE_ADD_01);
                break;
            case R.id.llRowDevice3:
                intent = new Intent(act, Things_Device_Add_01.class);
                intent.putExtra(Constants.EXTRA_DEVICE_NAME, util.getString(act, R.string.layout_things_device_add_06));
                intent.putExtra(Constants.EXTRA_COMPANY_NAME, util.getString(act, R.string.layout_things_device_add_07));
                intent.putExtra(Constants.EXTRA_DEVICE_DESC, util.getString(act, R.string.layout_things_device_add_del_02));
                intent.putExtra(Constants.EXTRA_DEVICE_DESC1, util.getString(act, R.string.layout_things_device_add_del_06));
                intent.putExtra(Constants.EXTRA_DEVICE_IMAGE, R.drawable.img_things_product_addimg_03_smartplug);
                intent.putExtra(Constants.EXTRA_DEVICE_IMAGE1, R.drawable.img_things_product_addimg_03_smartplug_wait);
                util.callActivityForResultNoAnim(act, intent, Constants.ACT_THINGS_DEVICE_ADD_01);
                break;
            case R.id.llRowDevice4:
                intent = new Intent(act, Things_Device_Add_01.class);
                intent.putExtra(Constants.EXTRA_DEVICE_NAME, util.getString(act, R.string.layout_things_device_add_08));
                intent.putExtra(Constants.EXTRA_COMPANY_NAME, util.getString(act, R.string.layout_things_device_add_09));
                intent.putExtra(Constants.EXTRA_DEVICE_DESC, util.getString(act, R.string.layout_things_device_add_del_03));
                intent.putExtra(Constants.EXTRA_DEVICE_DESC1, util.getString(act, R.string.layout_things_device_add_del_07));
                intent.putExtra(Constants.EXTRA_DEVICE_IMAGE, R.drawable.img_things_product_addimg_04_mutisensor_motion);
                intent.putExtra(Constants.EXTRA_DEVICE_IMAGE1, R.drawable.img_things_product_addimg_04_mutisensor_motion_wait);
                util.callActivityForResultNoAnim(act, intent, Constants.ACT_THINGS_DEVICE_ADD_01);
                break;
            case R.id.llRowDevice5:
                intent = new Intent(act, Things_Device_Add_01.class);
                intent.putExtra(Constants.EXTRA_DEVICE_NAME, util.getString(act, R.string.layout_things_device_add_10));
                intent.putExtra(Constants.EXTRA_COMPANY_NAME, util.getString(act, R.string.layout_things_device_add_11));
                intent.putExtra(Constants.EXTRA_DEVICE_DESC, util.getString(act, R.string.layout_things_device_add_del_03));
                intent.putExtra(Constants.EXTRA_DEVICE_DESC1, util.getString(act, R.string.layout_things_device_add_del_09));
                intent.putExtra(Constants.EXTRA_DEVICE_IMAGE, R.drawable.img_things_product_addimg_05_shocksensor);
                intent.putExtra(Constants.EXTRA_DEVICE_IMAGE1, R.drawable.img_things_product_addimg_05_shocksensor_wait);
                util.callActivityForResultNoAnim(act, intent, Constants.ACT_THINGS_DEVICE_ADD_01);
                break;
            case R.id.llRowDevice6:
                intent = new Intent(act, Things_Device_Add_01.class);
                intent.putExtra(Constants.EXTRA_DEVICE_NAME, util.getString(act, R.string.layout_things_device_add_12));
                intent.putExtra(Constants.EXTRA_COMPANY_NAME, util.getString(act, R.string.layout_things_device_add_11));
                intent.putExtra(Constants.EXTRA_DEVICE_DESC, util.getString(act, R.string.layout_things_device_add_del_03));
                intent.putExtra(Constants.EXTRA_DEVICE_DESC1, util.getString(act, R.string.layout_things_device_add_del_11));
                intent.putExtra(Constants.EXTRA_DEVICE_IMAGE, R.drawable.img_things_product_addimg_06_mutisensor_door);
                intent.putExtra(Constants.EXTRA_DEVICE_IMAGE1, R.drawable.img_things_product_addimg_06_mutisensor_door_wait);
                util.callActivityForResultNoAnim(act, intent, Constants.ACT_THINGS_DEVICE_ADD_01);
                break;
            case R.id.llRowDevice7:
                intent = new Intent(act, Things_Device_Add_01.class);
                intent.putExtra(Constants.EXTRA_DEVICE_NAME, util.getString(act, R.string.layout_things_device_add_13));
                intent.putExtra(Constants.EXTRA_COMPANY_NAME, util.getString(act, R.string.layout_things_device_add_14));
                intent.putExtra(Constants.EXTRA_DEVICE_DESC, util.getString(act, R.string.layout_things_device_add_del_13));
                intent.putExtra(Constants.EXTRA_DEVICE_DESC1, util.getString(act, R.string.layout_things_device_add_del_14));
                intent.putExtra(Constants.EXTRA_DEVICE_IMAGE, R.drawable.img_things_product_addimg_00_zwave);
                intent.putExtra(Constants.EXTRA_DEVICE_IMAGE1, R.drawable.img_things_product_addimg_00_zwave_wait);
                util.callActivityForResultNoAnim(act, intent, Constants.ACT_THINGS_DEVICE_ADD_01);
                break;
            default:
                break;
        }
    }

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

        super.onActivityResult(requestCode, resultCode, data);
        Boolean bReload_Flag = false;
        if ( resultCode == RESULT_OK ) {
            switch (requestCode) {
                case Constants.ACT_THINGS_DEVICE_ADD_01:
                    act.setResult(RESULT_OK, data);
                    finish();
//                    bReload_Flag = data.getBooleanExtra(Constants.EXTRA_RELOAD_FLAG, false);
//                    if ( bReload_Flag ) {
//                        bReloadFinishFlag = bReload_Flag;
//                    }
                    break;
            }
        }
    }

    @Override public void onBackPressed() {

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

    Boolean mFlag = false;

    @Override protected void onDestroy() {

        super.onDestroy();
    }

    @Override public void setAnimFinish(String sVal) {

        super.setAnimFinish(sVal);
    }

    Handler mHandler = new Handler(new FinishHandlerCallBack());

    class FinishHandlerCallBack implements Handler.Callback {

        @Override public boolean handleMessage(Message msg) {

            if ( msg.what == 0 ) {
                mFlag = false;
            }
            return true;
        }
    }

    @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;
            default:
                break;
        }
    }
}
