/*
 * 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.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 android.app.Activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Toast;

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

    /*통신 관련*/
    private int mType = 0;
    /*공통*/
    private Activity mActivity;
    private AppConfig mAppConfig;
    private Intent mIntent;
    private Util mUtil = new Util();
    private CustomDialog mCustomDialog = new CustomDialog();
    private String mDeviceName = "";
    private String mCompanyName = "";
    private String mDeviceDelDesc = "";
    private String mDeviceAddDesc = "";
    private int mDeviceImageId = 0;
    private int mDeviceImageId1 = 0;
    private String mDeviceId = "";
    private String mNodeId = "";


    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        mActivity = ThingsDeviceDelStep0.this;
        mAppConfig = (AppConfig) mActivity.getApplicationContext();
        mCustomDialog.setOnMsgAlterToResultEvent(this);

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

        Intent intent = getIntent();
        Dlog.v("getIntent() = " + getIntent());
        mDeviceName = intent.getStringExtra(Constants.EXTRA_DEVICE_NAME);
        mCompanyName = intent.getStringExtra(Constants.EXTRA_COMPANY_NAME);
        mDeviceDelDesc = intent.getStringExtra(Constants.EXTRA_DEVICE_DEL_DESC);
        mDeviceAddDesc = intent.getStringExtra(Constants.EXTRA_DEVICE_ADD_DESC);
        mDeviceImageId = intent.getIntExtra(Constants.EXTRA_DEVICE_IMAGE, 0);
        mDeviceImageId1 = intent.getIntExtra(Constants.EXTRA_DEVICE_IMAGE1, 0);
        mDeviceId = intent.getStringExtra(Constants.EXTRA_DEVICE_ID);
        mNodeId = intent.getStringExtra(Constants.EXTRA_NODE_ID);

//        mDeviceName = intent.getStringExtra(Constants.EXTRA_DEVICE_NAME);
//        mCompanyName = intent.getStringExtra(Constants.EXTRA_COMPANY_NAME);
//        mDeviceDesc = intent.getStringExtra(Constants.EXTRA_DEVICE_DESC);
//        mDeviceDesc1 = intent.getStringExtra(Constants.EXTRA_DEVICE_DESC1);
//        mDeviceImageId = intent.getIntExtra(Constants.EXTRA_DEVICE_IMAGE, 0);
//        mDeviceImageId1 = intent.getIntExtra(Constants.EXTRA_DEVICE_IMAGE1, 0);
//        mDeviceId = intent.getStringExtra(Constants.EXTRA_DEVICE_ID);

        setDisp();
    }

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

        mUtil.getView(mActivity, R.id.llPageRoot).setVisibility(View.VISIBLE);
        mUtil.getView(mActivity, R.id.llBtn1).setOnClickListener(this);
        mUtil.getView(mActivity, R.id.llBtn2).setOnClickListener(this);

        mUtil.getTextView(mActivity, R.id.tvDeviceName).setText(mDeviceName);
        mUtil.getTextView(mActivity, R.id.tvCompanyName).setText(mCompanyName);
        mUtil.setImage(mActivity, mDeviceImageId, R.id.ivIconDevice);

    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {

        super.onConfigurationChanged(newConfig);
    }

    @Override
    public void onClick(View view) {

        switch (view.getId()) {
            case R.id.llBtn2:
                onBackPressed();
                break;
            case R.id.llBtn1:
                mUtil.getView(mActivity, R.id.llBtn1).setEnabled(false);
                if (mAppConfig.getSelectCtrtGroupHomehubOnlineState(mDeviceId).equals("ON")) {
                    mUtil.getView(mActivity, R.id.llPageRoot).setVisibility(View.INVISIBLE);
                    mUtil.getView(mActivity, R.id.llBtn1).setEnabled(false);
                    mIntent = new Intent(mActivity, ThingsDeviceDelStep1.class);
                    mIntent.putExtra(Constants.EXTRA_DEVICE_NAME, mDeviceName);
                    mIntent.putExtra(Constants.EXTRA_COMPANY_NAME, mCompanyName);
                    mIntent.putExtra(Constants.EXTRA_DEVICE_DEL_DESC, mDeviceDelDesc);
                    mIntent.putExtra(Constants.EXTRA_DEVICE_ADD_DESC, mDeviceAddDesc);
                    mIntent.putExtra(Constants.EXTRA_DEVICE_IMAGE, mDeviceImageId);
                    mIntent.putExtra(Constants.EXTRA_DEVICE_IMAGE1, mDeviceImageId1);
                    mIntent.putExtra(Constants.EXTRA_DEVICE_ID, mDeviceId);
                    mIntent.putExtra(Constants.EXTRA_NODE_ID, mNodeId);
                    mUtil.callActivityForResultNoAnim(mActivity, mIntent, Constants.ACT_THINGS_DEVICE_DEL_STEP1);
                    mUtil.getView(mActivity, R.id.llBtn1).setEnabled(true);
                } else {
                    mUtil.setShowToast(mActivity, mUtil.getString(mActivity, R.string.MSG866), Toast.LENGTH_SHORT);
                    return;
                }
                mUtil.getView(mActivity, R.id.llBtn1).setEnabled(true);
                break;
            default:
                break;
        }
    }

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

        switch (msgId) {
//            case Constants.MSG998:
//                mUtil.callApp_Start(mActivity, "LOGOUT");
//                break;
            default:
                break;
        }
    }

    @Override
    public void onBackPressed() {

        mIntent = new Intent();
        mActivity.setResult(RESULT_CANCELED, mIntent);
        finish();
        return;
    }

    @Override
    public void onResume() {

        super.onResume();

    }

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

        super.onActivityResult(requestCode, resultCode, data);

        if (resultCode == mActivity.RESULT_OK) {
            switch (requestCode) {
                case Constants.ACT_THINGS_DEVICE_DEL_STEP1:
                    setResult(RESULT_OK, data);
                    finish();
                    break;
                default:
                    break;
            }
        } else {
            switch (requestCode) {
                case Constants.ACT_THINGS_DEVICE_DEL_STEP1:
                    Boolean isFlowEnd = data.getBooleanExtra(Constants.EXTRA_FLOW_END_FLAG, false);
                    Boolean isEnd = data.getBooleanExtra(Constants.EXTRA_END_FLAG, false);
//                    Boolean isTimeout = data.getBooleanExtra(Constants.EXTRA_TIMEOUT_FLAG, false);
                    if (isFlowEnd) {
                        mUtil.getView(mActivity, R.id.llPageRoot).setVisibility(View.VISIBLE);
                    } else if (isEnd) {
                        mActivity.setResult(RESULT_CANCELED, data);
                        finish();
                    } else {
                        mUtil.getView(mActivity, R.id.llPageRoot).setVisibility(View.VISIBLE);
//                        if (isTimeout) {
//                            mUtil.getView(mActivity, R.id.llBtn1).setVisibility(View.GONE);
//                            mUtil.getView(mActivity, R.id.llStart).setVisibility(View.GONE);
//                            mUtil.getView(mActivity, R.id.llReStart).setVisibility(View.VISIBLE);
//                        } else {
//                            mUtil.getView(mActivity, R.id.llBtn1).setVisibility(View.VISIBLE);
//                            mUtil.getView(mActivity, R.id.llStart).setVisibility(View.VISIBLE);
//                            mUtil.getView(mActivity, R.id.llReStart).setVisibility(View.GONE);
//                        }
                    }
                    break;
                default:
                    break;
            }
        }
    }


}

