/*
 * 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.Constants;
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 :   ThingsHomehubInput
 * 2. FileName  :   ThingsHomehubInput.java
 * 3. Package   :   com.ntels.onecable.admin.view.things
 * 4. Date      :   2016-11-24
 * 5. Author    :   aaron
 * 6. Comment   :   Initialize
 * </pre>
 */
public class ThingsHomehubInput extends CustomActivity implements View.OnClickListener,
        CustomDialog.OnMsgAlterToResult {

    /*공통*/
    private Activity mActivity;
    private Intent mIntent;
    private Util mUtil = new Util();
    private CustomDialog mCustomDialog = new CustomDialog();

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        mActivity = ThingsHomehubInput.this;
        mCustomDialog.setOnMsgAlterToResultEvent(this);

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

        mUtil.getLinearLayout(mActivity, R.id.llBtn1).setOnClickListener(this);
        mUtil.getLinearLayout(mActivity, R.id.llBtn2).setOnClickListener(this);
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {

        super.onConfigurationChanged(newConfig);
    }

    @Override
    public void onClick(View view) {

        switch (view.getId()) {
            case R.id.llBtn1:
                String activationCode = mUtil.getEditTextVal(mActivity, R.id.edtsActivation_Code);
                String pass = mUtil.getEditTextVal(mActivity, R.id.edtPass);

                if ("".equals(activationCode)) {
                    mUtil.setShowToast(mActivity,
                            mUtil.getString(mActivity, R.string.layout_things_homehub_02)
                                    + mUtil.getString(mActivity, R.string.MSG901),
                            Toast.LENGTH_SHORT);
                    return;
                }

                if ("".equals(pass)) {
                    mUtil.setShowToast(mActivity, mUtil.getString(mActivity,
                            R.string.layout_things_homehub_03)
                            + mUtil.getString(mActivity, R.string.MSG901), Toast.LENGTH_SHORT);
                    return;
                }

                if (activationCode.contains("|*_AC_*|")) {
                    mCustomDialog.msgAlert(mActivity, Constants.MSG861, "");
                    return;
                }

                mIntent = new Intent();
                mIntent.putExtra(Constants.EXTRA_ACTIVATION_CODE, activationCode);
                mIntent.putExtra(Constants.EXTRA_PASSWORD, pass);
                mActivity.setResult(RESULT_OK, mIntent);
                finish();
                break;
            case R.id.llBtn2:
                onBackPressed();
                break;
            default:
                break;
        }
    }

    @Override
    public void onBackPressed() {

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

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

        switch (msgId) {

            case Constants.MSG861:
                // just dismiss
                break;
            default:
                break;
        }
    }
}
