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

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

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.util.Util;
import com.ntels.onecable.admin.common.widget.CustomActivity;
import com.ntels.onecable.admin.view.dialog.DialogSpinner;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Toast;

import java.util.ArrayList;

import static com.ntels.onecable.admin.R.id.llRowDevice1;

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

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

    /*공통*/
    private Activity mActivity;
    private Util mUtil = new Util();
    private boolean mIsReloadFinish = false;
    private String mDeviceId = "";
    private String mDeviceTypeId = "";
    private String mDvcgrpName = "";
    private AppConfig mAppConfig;

    private int mDevicePosition = -1;
    private ArrayList<String> mDeviceNameList = new ArrayList<String>();
    private ArrayList<String> mDeviceSnList = new ArrayList<String>();
    private ArrayList<String> mDeviceEnableList = new ArrayList<String>();

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        mActivity = ThingsDeviceAdd.this;

        mAppConfig = (AppConfig) mActivity.getApplicationContext();

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

        if (getIntent() != null) {
            mDeviceId = getIntent().getStringExtra(Constants.EXTRA_DEVICE_ID);
            mDeviceTypeId = getIntent().getStringExtra(Constants.EXTRA_DEVICE_TYPE_ID);
            mDvcgrpName = getIntent().getStringExtra(Constants.EXTRA_DVCGRP_NAME);
        }

        setToolbar();
        setDisp();
    }

    @Override
    public void onClick(View view) {
        Intent intent;
        switch (view.getId()) {
            case R.id.llBtn1:
                onBackPressed();
                break;
            case llRowDevice1:
                intent = new Intent(mActivity, ThingsDeviceAddStep0.class);
//                intent = new Intent(mActivity, ThingsDeviceAdd1st.class);
                intent.putExtra(Constants.EXTRA_DEVICE_NAME, mUtil.getString(mActivity, R.string.layout_things_device_add_02));
                intent.putExtra(Constants.EXTRA_COMPANY_NAME, mUtil.getString(mActivity, R.string.layout_things_device_add_03));
                intent.putExtra(Constants.EXTRA_DEVICE_DEL_DESC, mUtil.getString(mActivity, R.string.layout_things_device_add_del_04));
                intent.putExtra(Constants.EXTRA_DEVICE_ADD_DESC, mUtil.getString(mActivity, 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);
                intent.putExtra(Constants.EXTRA_DEVICE_ID, mDeviceId);
                mUtil.callActivityForResultNoAnim(mActivity, intent, Constants.ACT_THINGS_DEVICE_ADD_01);
                break;
            case R.id.llRowDevice2:
                intent = new Intent(mActivity, ThingsDeviceAddStep0.class);
                intent.putExtra(Constants.EXTRA_DEVICE_NAME, mUtil.getString(mActivity, R.string.layout_things_device_add_04));
                intent.putExtra(Constants.EXTRA_COMPANY_NAME, mUtil.getString(mActivity, R.string.layout_things_device_add_05));
                intent.putExtra(Constants.EXTRA_DEVICE_DEL_DESC, mUtil.getString(mActivity, R.string.layout_things_device_add_del_05));
                intent.putExtra(Constants.EXTRA_DEVICE_ADD_DESC, mUtil.getString(mActivity, 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);
                intent.putExtra(Constants.EXTRA_DEVICE_ID, mDeviceId);
                mUtil.callActivityForResultNoAnim(mActivity, intent, Constants.ACT_THINGS_DEVICE_ADD_01);
                break;
            case R.id.llRowDevice3:
                intent = new Intent(mActivity, ThingsDeviceAddStep0.class);
                intent.putExtra(Constants.EXTRA_DEVICE_NAME, mUtil.getString(mActivity, R.string.layout_things_device_add_06));
                intent.putExtra(Constants.EXTRA_COMPANY_NAME, mUtil.getString(mActivity, R.string.layout_things_device_add_07));
                intent.putExtra(Constants.EXTRA_DEVICE_DEL_DESC, mUtil.getString(mActivity, R.string.layout_things_device_add_del_06));
                intent.putExtra(Constants.EXTRA_DEVICE_ADD_DESC, mUtil.getString(mActivity, 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);
                intent.putExtra(Constants.EXTRA_DEVICE_ID, mDeviceId);
                mUtil.callActivityForResultNoAnim(mActivity, intent, Constants.ACT_THINGS_DEVICE_ADD_01);
                break;
            case R.id.llRowDevice4:
                intent = new Intent(mActivity, ThingsDeviceAddStep0.class);
                intent.putExtra(Constants.EXTRA_DEVICE_NAME, mUtil.getString(mActivity, R.string.layout_things_device_add_08));
                intent.putExtra(Constants.EXTRA_COMPANY_NAME, mUtil.getString(mActivity, R.string.layout_things_device_add_09));
                intent.putExtra(Constants.EXTRA_DEVICE_DEL_DESC, mUtil.getString(mActivity, R.string.layout_things_device_add_del_07));
                intent.putExtra(Constants.EXTRA_DEVICE_ADD_DESC, mUtil.getString(mActivity, 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);
                intent.putExtra(Constants.EXTRA_DEVICE_ID, mDeviceId);
                mUtil.callActivityForResultNoAnim(mActivity, intent, Constants.ACT_THINGS_DEVICE_ADD_01);
                break;
            case R.id.llRowDevice5:
                intent = new Intent(mActivity, ThingsDeviceAddStep0.class);
                intent.putExtra(Constants.EXTRA_DEVICE_NAME, mUtil.getString(mActivity, R.string.layout_things_device_add_10));
                intent.putExtra(Constants.EXTRA_COMPANY_NAME, mUtil.getString(mActivity, R.string.layout_things_device_add_11));
                intent.putExtra(Constants.EXTRA_DEVICE_DEL_DESC, mUtil.getString(mActivity, R.string.layout_things_device_add_del_09));
                intent.putExtra(Constants.EXTRA_DEVICE_ADD_DESC, mUtil.getString(mActivity, 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);
                intent.putExtra(Constants.EXTRA_DEVICE_ID, mDeviceId);
                mUtil.callActivityForResultNoAnim(mActivity, intent, Constants.ACT_THINGS_DEVICE_ADD_01);
                break;
            case R.id.llRowDevice6:
                intent = new Intent(mActivity, ThingsDeviceAddStep0.class);
                intent.putExtra(Constants.EXTRA_DEVICE_NAME, mUtil.getString(mActivity, R.string.layout_things_device_add_12));
                intent.putExtra(Constants.EXTRA_COMPANY_NAME, mUtil.getString(mActivity, R.string.layout_things_device_add_11));
                intent.putExtra(Constants.EXTRA_DEVICE_DEL_DESC, mUtil.getString(mActivity, R.string.layout_things_device_add_del_11));
                intent.putExtra(Constants.EXTRA_DEVICE_ADD_DESC, mUtil.getString(mActivity, 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);
                intent.putExtra(Constants.EXTRA_DEVICE_ID, mDeviceId);
                mUtil.callActivityForResultNoAnim(mActivity, intent, Constants.ACT_THINGS_DEVICE_ADD_01);
                break;
            case R.id.llRowDevice7:
                intent = new Intent(mActivity, ThingsDeviceAddStep0.class);
                intent.putExtra(Constants.EXTRA_DEVICE_NAME, mUtil.getString(mActivity, R.string.layout_things_device_add_13));
                intent.putExtra(Constants.EXTRA_COMPANY_NAME, mUtil.getString(mActivity, R.string.layout_things_device_add_14));
                intent.putExtra(Constants.EXTRA_DEVICE_DEL_DESC, mUtil.getString(mActivity, R.string.layout_things_device_add_del_14));
                intent.putExtra(Constants.EXTRA_DEVICE_ADD_DESC, mUtil.getString(mActivity, 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);
                intent.putExtra(Constants.EXTRA_DEVICE_ID, mDeviceId);
                mUtil.callActivityForResultNoAnim(mActivity, intent, Constants.ACT_THINGS_DEVICE_ADD_01);
                break;
            case R.id.llRowDevice8:
                // Netatmo에서 access_camera permission을 받아야 사용 가능.
                intent = new Intent(mActivity, ThingsDeviceAddStep0.class);
                intent.putExtra(Constants.EXTRA_DEVICE_NAME, mUtil.getString(mActivity, R.string.layout_things_device_add_16));
                intent.putExtra(Constants.EXTRA_COMPANY_NAME, mUtil.getString(mActivity, R.string.layout_things_device_add_17));
                intent.putExtra(Constants.EXTRA_DEVICE_ADD_DESC, mUtil.getString(mActivity, R.string.layout_things_device_add_del_16));
                intent.putExtra(Constants.EXTRA_DEVICE_IMAGE, R.drawable.img_things_product_addimg_07_netatmo);
                intent.putExtra(Constants.EXTRA_DEVICE_IMAGE1, R.drawable.img_things_product_addimg_07_netatmo);
                intent.putExtra(Constants.EXTRA_DEVICE_ID, mDeviceId);
                mUtil.callActivityForResultNoAnim(mActivity, 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);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case Constants.ACT_SPINNER:
                    int tempDevicesPosition = data.getIntExtra(Constants.EXTRA_SPINNER_RESULT, -1);
                    if (tempDevicesPosition == -1) {
                        mUtil.setShowToast(mActivity, mUtil.getString(mActivity, R.string.MSG929), Toast.LENGTH_SHORT);
                        onBackPressed();
                    } else {
                        mDevicePosition = tempDevicesPosition;
                        mDeviceId = mAppConfig.getHomehubDeviceId(mDevicePosition);
                    }
                    break;
                case Constants.ACT_THINGS_DEVICE_ADD_01:
                    mActivity.setResult(RESULT_OK, data);
                    finish();
                    break;
                default:
                    break;
            }
        } else {
            switch (requestCode) {
                case Constants.ACT_SPINNER:
//                    mUtil.setShowToast(mActivity, mUtil.getString(mActivity, R.string.MSG929), Toast.LENGTH_SHORT);
                    onBackPressed();
                    break;
                default:
                    break;
            }
        }
    }

    @Override
    public void onBackPressed() {

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

    /**
     * 툴바 셋팅.
     */
    private void setToolbar() {

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

        mUtil.getTextView(mToolbar, R.id.tvToolbarTitle1).setText(mDvcgrpName + (mDvcgrpName.equals("")?"":" ") +  mUtil.getString(mActivity, R.string.layout_things_device_add_01));
    }

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

        mUtil.getView(mActivity, R.id.llRowDevice1).setVisibility(View.GONE);
        mUtil.getView(mActivity, R.id.llRowDevice2).setVisibility(View.GONE);
        mUtil.getView(mActivity, R.id.llRowDevice3).setVisibility(View.GONE);
        mUtil.getView(mActivity, R.id.llRowDevice4).setVisibility(View.GONE);
        mUtil.getView(mActivity, R.id.llRowDevice5).setVisibility(View.GONE);
        mUtil.getView(mActivity, R.id.llRowDevice6).setVisibility(View.GONE);
        mUtil.getView(mActivity, R.id.llRowDevice7).setVisibility(View.GONE);
        mUtil.getView(mActivity, R.id.llRowDevice8).setVisibility(View.GONE);
        if (mDeviceTypeId.equals(Constants.DEVICE_TYPE_ID_DOORSENSOR)) {
            mUtil.getView(mActivity, R.id.llRowDevice6).setVisibility(View.VISIBLE);
        } else if (mDeviceTypeId.equals(Constants.DEVICE_TYPE_ID_SMARTPLUG)){
            mUtil.getView(mActivity, R.id.llRowDevice2).setVisibility(View.VISIBLE);
            mUtil.getView(mActivity, R.id.llRowDevice3).setVisibility(View.VISIBLE);
        } else if (mDeviceTypeId.equals(Constants.DEVICE_TYPE_ID_GASVALVE_CLOSEONLY)) {
            mUtil.getView(mActivity, R.id.llRowDevice1).setVisibility(View.VISIBLE);
        } else {
            mUtil.getView(mActivity, R.id.llRowDevice1).setVisibility(View.VISIBLE);
            mUtil.getView(mActivity, R.id.llRowDevice2).setVisibility(View.VISIBLE);
            mUtil.getView(mActivity, R.id.llRowDevice3).setVisibility(View.VISIBLE);
            mUtil.getView(mActivity, R.id.llRowDevice4).setVisibility(View.VISIBLE);
            mUtil.getView(mActivity, R.id.llRowDevice5).setVisibility(View.VISIBLE);
            mUtil.getView(mActivity, R.id.llRowDevice6).setVisibility(View.VISIBLE);
            mUtil.getView(mActivity, R.id.llRowDevice7).setVisibility(View.VISIBLE);
            mUtil.getView(mActivity, R.id.llRowDevice8).setVisibility(View.VISIBLE);
        }
        mUtil.getView(mActivity, R.id.llRowDevice1).setOnClickListener(this);
        mUtil.getView(mActivity, R.id.llRowDevice2).setOnClickListener(this);
        mUtil.getView(mActivity, R.id.llRowDevice3).setOnClickListener(this);
        mUtil.getView(mActivity, R.id.llRowDevice4).setOnClickListener(this);
        mUtil.getView(mActivity, R.id.llRowDevice5).setOnClickListener(this);
        mUtil.getView(mActivity, R.id.llRowDevice6).setOnClickListener(this);
        mUtil.getView(mActivity, R.id.llRowDevice7).setOnClickListener(this);
        mUtil.getView(mActivity, R.id.llRowDevice8).setOnClickListener(this);

        mUtil.getView(mActivity, R.id.llBtn1).setOnClickListener(this);

        if (mDeviceId.equals("")) {
            setSpinnerDeviceList();

            Intent intent = new Intent();
            intent.setClass(mActivity, DialogSpinner.class);
            intent.putExtra(Constants.EXTRA_SPINNER_LIST1, mDeviceNameList);
            intent.putExtra(Constants.EXTRA_SPINNER_LIST2, mDeviceSnList);
            intent.putExtra(Constants.EXTRA_SPINNER_LIST3, mDeviceEnableList);
            intent.putExtra(Constants.EXTRA_SPINNER_TITLE, mUtil.getString(mActivity, R.string.layout_things_device_add_00));
            intent.putExtra(Constants.EXTRA_SPINNER_TYPE, 0);
            intent.putExtra(Constants.EXTRA_SPINNER_POSITION, mDevicePosition);
            mUtil.callActivityForResultNoAnim(mActivity, intent, Constants.ACT_SPINNER);
        }
    }

    private void setSpinnerDeviceList() {
        mDeviceNameList.clear();
        mDeviceSnList.clear();
        mDeviceEnableList.clear();
        if (mAppConfig.getDevicesList().size() > 0) {
            for (int i = 0; i < mAppConfig.getDevicesList().size(); i++) {
                mDeviceNameList.add(mAppConfig.getHomehubDeviceName(i));
                mDeviceSnList.add("S/N : " + mAppConfig.getHomehubDeviceSn(i) + "\n"
                                + mUtil.getString(mActivity, R.string.layout_things_device_add_99) + " : "  );//+ mAppConfig.get);
                if (mAppConfig.getDevicesList().get(i).get(Constants.KEY_CTRT_CNT) == mAppConfig.getDevicesList().get(i).get(Constants.KEY_CTRT_CNT)) {
                    mDeviceEnableList.add("N");
                } else {
                    mDeviceEnableList.add("N");
                }
            }
        }
    }


}
