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

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

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


public class HomehubUpdate 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 Toolbar mToolbar;
    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 int mCallType = 0;


    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

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

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

        Intent intent = getIntent();
        Dlog.v("getIntent() = "+ getIntent());
        mDeviceId = intent.getStringExtra(Constants.EXTRA_DEVICE_ID);
        mCallType = intent.getIntExtra(Constants.EXTRA_CALL_TYPE, Constants.HOMEHUB_FIRMWARE_UPDATE);

        setToolbar();
        setDisp();
    }

    /**
     * 툴바 셋팅.
     */
    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.btnToolbarModify).setVisibility(View.GONE);
        mUtil.getTextView(mToolbar, R.id.btnToolbarMore).setVisibility(View.GONE);

        mUtil.getTextView(mToolbar, R.id.tvToolbarTitle1).setText(mUtil.getString(mActivity, R.string.layout_homehub_update_01));

    }

    /**
     * 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:
                if (mAppConfig.getSelectCtrtGroupHomehubOnlineState(mDeviceId).equals("ON")) {
                    mUtil.getView(mActivity, R.id.llBtn1).setEnabled(false);
                    Intent intent = new Intent(mActivity, HomehubUpdateLoading.class);
                    intent.putExtra(Constants.EXTRA_DEVICE_ID, mDeviceId);
                    intent.putExtra(Constants.EXTRA_CALL_TYPE, mCallType);
                    mUtil.callActivityForResultNoAnim(mActivity, intent, Constants.ACT_HOMEHUB_UPDATE_LOADING);
                    mUtil.getView(mActivity, R.id.llBtn1).setEnabled(true);
                } 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;
            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_ADD_STEP1:
                    setResult(RESULT_OK, data);
                    finish();
                    break;
                default:
                    break;
            }
        } else {
            switch (requestCode) {
                case Constants.ACT_THINGS_DEVICE_ADD_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;
            }
        }
    }
}
