/*
 * 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.ProgressBar;
import android.widget.Toast;

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

    private static final String TAG = ThingsDeviceAddStep4.class.getSimpleName();
    ProgressBar mProgressBar;
    /*통신 관련*/
    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 mName = "";
    private String mCompanyName = "";
    private String mDeviceDelDesc = "";
    private String mDeviceDelDesc1 = "";
    private String mDeviceAddDesc = "";
    private String mDeviceAddDesc1 = "";
    private int mDeviceImageId = 0;
    private int mDeviceImageId1 = 0;
    private String mDeviceId = "";
    private String mNodeId = "";
    private Boolean mIsResult = false;


    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

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

        setIsWebSocket(true);

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

        Intent intent = 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);
        mName = intent.getStringExtra(Constants.EXTRA_NAME);
        mIsResult = intent.getBooleanExtra(Constants.EXTRA_RESULT, false);

        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.setImage(mActivity, mDeviceImageId, R.id.ivIconDevice);
        if (mIsResult) {
            mUtil.getTextView(mActivity, R.id.tvTitle).setText(mUtil.getString(mActivity, R.string.layout_things_device_add_step4_01));
            mUtil.getTextView(mActivity, R.id.tvDeviceDesc).setVisibility(View.VISIBLE);
            mUtil.getImageView(mActivity, R.id.ivIconFail).setVisibility(View.GONE);
            mUtil.getTextView(mActivity, R.id.tvBtn2).setText(mUtil.getString(mActivity, R.string.layout_later));
            mUtil.getTextView(mActivity, R.id.tvBtn1).setText(mUtil.getString(mActivity, R.string.layout_things_device_add_step4_04));
        } else {
            mUtil.getTextView(mActivity, R.id.tvTitle).setText(mUtil.getString(mActivity, R.string.layout_things_device_add_step4_02));
            mUtil.getTextView(mActivity, R.id.tvDeviceDesc).setVisibility(View.GONE);
            mUtil.getImageView(mActivity, R.id.ivIconFail).setVisibility(View.VISIBLE);
            mUtil.getTextView(mActivity, R.id.tvBtn2).setText(mUtil.getString(mActivity, R.string.layout_cancel));
            mUtil.getTextView(mActivity, R.id.tvBtn1).setText(mUtil.getString(mActivity, R.string.layout_retry));
        }

    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {

        super.onConfigurationChanged(newConfig);
    }

    @Override
    public void onClick(View view) {

        switch (view.getId()) {
            case R.id.llBtn2:
                if (mIsResult) {
                    mIntent = new Intent();
                    mIntent.putExtra(Constants.EXTRA_RELOAD_FLAG, true);
                    mActivity.setResult(RESULT_OK, mIntent);
                    finish();
                } else {
                    onBackPressed();
                }
                break;
            case R.id.llBtn1:
                if (mAppConfig.getSelectCtrtGroupHomehubOnlineState(mDeviceId).equals("ON")) {
                    if (mIsResult) {
                        Intent intent = new Intent(mActivity, ThingsNameChange.class);
                        intent.putExtra(Constants.EXTRA_DEVICE_ID, mDeviceId);
                        intent.putExtra(Constants.EXTRA_DEVICE_NAME, mName);
                        intent.putExtra(Constants.EXTRA_NODE_ID, mNodeId);
                        mUtil.callActivityForResultNoAnim(mActivity, intent, Constants.ACT_THINGS_NAME_CHANGE);
                    } else {
                        // 여기서 이전 화면으로 다시 돌아간다
                        onBackPressedRetry();
                    }
                } else {
                    mUtil.setShowToast(mActivity, mUtil.getString(mActivity, R.string.MSG866), Toast.LENGTH_SHORT);
                    return;
                }
                break;
            default:
                break;
        }
    }

    @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(mType), Constants.HANDLER_DELAY_MILLIS);
//                }
//                break;
            default:
                break;
        }
    }

    private void onBackPressedRetry(){
        mIntent = new Intent();
        mIntent.putExtra(Constants.EXTRA_RETRAY_FLAG, true);
        mActivity.setResult(RESULT_CANCELED, mIntent);
        finish();
    }


    private void onBackPressedFlowEnd(){
        mIntent = new Intent();
        mIntent.putExtra(Constants.EXTRA_FLOW_END_FLAG, true);
        mActivity.setResult(RESULT_CANCELED, mIntent);
        finish();
    }

    private void onBackPressedEnd(){
        mIntent = new Intent();
        mIntent.putExtra(Constants.EXTRA_END_FLAG, true);
        mActivity.setResult(RESULT_CANCELED, mIntent);
        finish();
    }

    @Override
    public void onBackPressed() {

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

    @Override
    public void onResume() {

        super.onResume();

    }

    @Override
    protected void onDestroy() {

        super.onDestroy();
        Dlog.v("onDestroy()");
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @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_NAME_CHANGE:
                    mActivity.setResult(RESULT_OK, data);
                    finish();
                    break;
                default:
                    break;
            }
        } else {
            switch (requestCode) {
                case Constants.ACT_THINGS_NAME_CHANGE:
//                    mActivity.setResult(RESULT_OK, data);
//                    finish();
                    break;
                default:
                    break;
            }
        }
    }


}

