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

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


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.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;

/**
 * <pre>
 * 1. ClassName :   ThingsDeviceAdd1st
 * 2. FileName  :   ThingsDeviceAdd1st.java
 * 3. Package   :   com.ntels.onecable.view.things
 * 4. Date      :   2016-11-25
 * 5. Author    :   aaron
 * 6. Comment   :   Initialize
 * </pre>
 */
public class HomehubBleStep1 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();


    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

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


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

        setToolbar();
        setDisp();

    }

    /**
     * UI display.
     */
    private void setDisp() {
        mUtil.getView(mActivity, R.id.llBtn1).setOnClickListener(this);
        mUtil.getView(mActivity, R.id.llBtn2).setOnClickListener(this);
    }


    /**
     * 툴바 셋팅.
     */
    private void setToolbar() {
        Toolbar toolbar = (Toolbar) mActivity.findViewById(R.id.toolbar);
        mUtil.setKeyboardHide(mActivity, toolbar);
        mUtil.getTextView(toolbar, R.id.tvToolbarTitle1).setVisibility(View.VISIBLE);
        mUtil.getTextView(toolbar, R.id.tvToolbarTitle2).setVisibility(View.GONE);
        mUtil.getTextView(toolbar, R.id.tvToolbarTitleDivider).setVisibility(View.GONE);
        mUtil.getTextView(toolbar, R.id.tvToolbarTitle3).setVisibility(View.GONE);
        mUtil.getTextView(toolbar, R.id.tvToolbarTitle4).setVisibility(View.GONE);
        mUtil.getTextView(toolbar, R.id.btnToolbarClose).setVisibility(View.GONE);
        mUtil.getTextView(toolbar, R.id.btnToolbarMore).setVisibility(View.GONE);

        mUtil.getTextView(toolbar, R.id.tvToolbarTitle1).setText(mUtil.getString(mActivity, R.string.layout_homehub_ble_step1_01));
    }


    @Override
    public void onConfigurationChanged(Configuration newConfig) {

        super.onConfigurationChanged(newConfig);
    }

    @Override
    public void onBackPressed() {
        onBackPressedEnd();
        return;
    }
    @Override
    public void onClick(View view) {

        switch (view.getId()) {
            case R.id.llBtn1:
                Intent intent = new Intent(mActivity, HomehubBleStep2.class);
                mUtil.callActivityForResultNoAnim(mActivity, intent, Constants.ACT_HOMEHUB_BLE_STEP2);
                break;
            case R.id.llBtn2:
                onBackPressed();
                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 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_HOMEHUB_BLE_STEP2:
                    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 {

                    }
                    break;
                default:
                    break;
            }
        }
    }




}

