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

package com.ntels.kneet.v3.view;

import android.app.Activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Toast;

import com.ntels.kneet.v3.R;
import com.ntels.kneet.v3.common.AppConfig;
import com.ntels.kneet.v3.common.Constants;
import com.ntels.kneet.v3.common.util.Util;
import com.ntels.kneet.v3.common.widget.CustomActionBarActivity;
import com.ntels.kneet.v3.view.dialog.CustomDialog;

public class Join_Chk extends CustomActionBarActivity implements View.OnClickListener,
                                                                 CustomDialog.OnMsgAlterToResult {


    private Toolbar toolbar;
    private Activity act;
    Intent intent;
    private AppConfig appConfig;
    private Util         util         = new Util();
    private CustomDialog customDialog = new CustomDialog();

    /**
     * <PRE>
     * 1. MethodName: onCreate
     * 2. ClassName :
     * 3. Comment   : onCreate
     * 4. Developer : 최진오
     * 5. Created   : 2015. 10.
     * </PRE>
     */
    @Override public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        act = Join_Chk.this;
        appConfig = (AppConfig) act.getApplicationContext();
        customDialog.setOnMsgAlterToResultEvent(this);

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

        setToolbar();

        displayListView();
    }

    private void setToolbar() {

        toolbar = (Toolbar) act.findViewById(R.id.toolbar);
        util.setKeyboardHide(act, toolbar);
        util.getTextView(toolbar, R.id.tvToolbarTitle1).setVisibility(View.VISIBLE);
        util.getTextView(toolbar, R.id.tvToolbarTitle2).setVisibility(View.GONE);
        util.getTextView(toolbar, R.id.tvToolbarTitleDivider).setVisibility(View.GONE);
        util.getTextView(toolbar, R.id.tvToolbarTitle3).setVisibility(View.GONE);
        util.getTextView(toolbar, R.id.tvToolbarTitle4).setVisibility(View.GONE);
        util.getTextView(toolbar, R.id.btnToolbarDel).setVisibility(View.GONE);
        util.getTextView(toolbar, R.id.btnToolbarClose).setVisibility(View.GONE);
        util.getTextView(toolbar, R.id.btnToolbarMore).setVisibility(View.GONE);

        util.getTextView(toolbar, R.id.tvToolbarTitle1).setText(util.getString(act, R.string.layout_join_chk_01));
    }

    private void displayListView() {

        util.getView(act, R.id.tvGotoService).setOnClickListener(this);
        util.getView(act, R.id.llGotoServiceChk).setOnClickListener(this);
        util.getView(act, R.id.tvGotoPolicy).setOnClickListener(this);
        util.getView(act, R.id.llGotoPolicyChk).setOnClickListener(this);
        util.getView(act, R.id.llAllChk).setOnClickListener(this);
        util.getView(act, R.id.llBtn1).setOnClickListener(this);
        util.getView(act, R.id.llBtn2).setOnClickListener(this);
    }

    @Override public void onConfigurationChanged(Configuration newConfig) {

        super.onConfigurationChanged(newConfig);
    }

    @Override public void onClick(View v) {

        Intent intent;
        switch (v.getId()) {
            case R.id.llBtn1:
                v.setEnabled(false);
                if ( !checkFormValid() ) {
                    v.setEnabled(true);
                    return;
                }

                intent = new Intent(act, Join.class);
                util.callActivityForResult(act, intent, Constants.ACT_JOIN);
                v.setEnabled(true);
                break;
            case R.id.llBtn2:
                onBackPressed();
                break;

            case R.id.llGotoServiceChk:
                if ( v.isSelected() ) {
                    v.setSelected(false);
                } else {
                    v.setSelected(true);
                }
                if ( util.getView(act, R.id.llGotoServiceChk).isSelected() && util.getView(act, R.id.llGotoPolicyChk).isSelected() ) {
                    util.getView(act, R.id.llAllChk).setSelected(true);
                } else {
                    util.getView(act, R.id.llAllChk).setSelected(false);
                }
                break;
            case R.id.llGotoPolicyChk:
                if ( v.isSelected() ) {
                    v.setSelected(false);
                } else {
                    v.setSelected(true);
                }
                if ( util.getView(act, R.id.llGotoServiceChk).isSelected() && util.getView(act, R.id.llGotoPolicyChk).isSelected() ) {
                    util.getView(act, R.id.llAllChk).setSelected(true);
                } else {
                    util.getView(act, R.id.llAllChk).setSelected(false);
                }
                break;
            case R.id.llAllChk:
                if ( v.isSelected() ) {
                    v.setSelected(false);
                    util.getView(act, R.id.llGotoServiceChk).setSelected(false);
                    util.getView(act, R.id.llGotoPolicyChk).setSelected(false);
                } else {
                    v.setSelected(true);
                    util.getView(act, R.id.llGotoServiceChk).setSelected(true);
                    util.getView(act, R.id.llGotoPolicyChk).setSelected(true);
                }
                break;
            case R.id.tvGotoService:
                intent = new Intent(Intent.ACTION_VIEW);
                intent.addCategory(Intent.CATEGORY_DEFAULT);
                intent.setData(Uri.parse("http://home-api.allofthings.com/guide/20001/service"));
                startActivity(intent);
                break;
            case R.id.tvGotoPolicy:
                intent = new Intent(Intent.ACTION_VIEW);
                intent.addCategory(Intent.CATEGORY_DEFAULT);
                intent.setData(Uri.parse("http://home-api.allofthings.com/guide/20001/policy"));
                startActivity(intent);
                break;
            default:
                break;
        }
    }

    private Boolean checkFormValid() {

        Boolean bCheck = true;
        if ( !util.getView(act, R.id.llGotoServiceChk).isSelected() ) {
            bCheck = false;
            util.setShowToast(act, util.getString(act, R.string.MSG838), Toast.LENGTH_SHORT);
            return bCheck;
        }
        if ( !util.getView(act, R.id.llGotoPolicyChk).isSelected() ) {
            bCheck = false;
            util.setShowToast(act, util.getString(act, R.string.MSG839), Toast.LENGTH_SHORT);
            return bCheck;
        }
        return bCheck;
    }

    /**
     * <PRE>
     * 1. MethodName: onBackPressed
     * 2. ClassName :
     * 3. Comment   : onBackPressed
     * 4. Developer : 최진오
     * 5. Created   : 2015. 10.
     * </PRE>
     */
    @Override public void onBackPressed() {

        intent = new Intent();
        act.setResult(RESULT_CANCELED, intent);
        finish();
        return;
    }

    /**
     * <PRE>
     * 1. MethodName: onResume
     * 2. ClassName :
     * 3. Comment   : onResume
     * 4. Developer : 최진오
     * 5. Created   : 2015. 10.
     * </PRE>
     */
    @Override public void onResume() {

        super.onResume();

    }

    @Override public void resultCustomDialogResponse(int iMsgID, int iBtnResult) {

        switch (iMsgID) {

            default:
                break;
        }
    }

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

        super.onActivityResult(requestCode, resultCode, data);

        if ( resultCode == RESULT_OK ) {
            switch (requestCode) {
                case Constants.ACT_JOIN:
                    intent = new Intent();
                    setResult(RESULT_OK, intent);
                    finish();
                    break;
                default:
                    break;
            }
        } else {
            if ( appConfig.getGoErr() ) {
                appConfig.setGoErr(true);
                setResult(RESULT_CANCELED, new Intent());
                finish();
            } else {
                switch (requestCode) {
                    default:
                        break;
                }
            }
        }
    }
}
