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

package com.ntels.onecable.view;

import com.ntels.onecable.R;
import com.ntels.onecable.common.AppConfig;
import com.ntels.onecable.common.Constants;
import com.ntels.onecable.common.util.Util;
import com.ntels.onecable.common.widget.CustomActionBarActivity;

import android.app.Activity;
import android.content.Intent;
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;

/**
 * <pre>
 * 1. ClassName :   JoinCheck
 * 2. FileName  :   JoinCheck.java
 * 3. Package   :   com.ntels.onecable.view
 * 4. Date      :   2016-11-28
 * 5. Author    :   aaron
 * 6. Comment   :   Initialize
 * </pre>
 */
public class JoinCheck extends CustomActionBarActivity implements View.OnClickListener {


    private Activity mActivity;
    private Intent mIntent;
    private AppConfig mAppConfig;
    private Util mUtil = new Util();

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        mActivity = JoinCheck.this;
        mAppConfig = (AppConfig) mActivity.getApplicationContext();

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

        setToolbar();

        displayListView();
    }

    @Override
    public void onBackPressed() {

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

    @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:
                    mIntent = new Intent();
                    setResult(RESULT_OK, mIntent);
                    finish();
                    break;
                default:
                    break;
            }
        } else {
            if (mAppConfig.getGoErr()) {
                mAppConfig.setGoErr(true);
                setResult(RESULT_CANCELED, new Intent());
                finish();
            } else {
                switch (requestCode) {
                    default:
                        break;
                }
            }
        }
    }

    @Override
    public void onClick(View view) {

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

                intent = new Intent(mActivity, Join.class);
                mUtil.callActivityForResult(mActivity, intent, Constants.ACT_JOIN);
                view.setEnabled(true);
                break;
            case R.id.llBtn2:
                onBackPressed();
                break;

            case R.id.llGotoServiceChk:
                if (view.isSelected()) {
                    view.setSelected(false);
                } else {
                    view.setSelected(true);
                }
                if (mUtil.getView(mActivity, R.id.llGotoServiceChk).isSelected()
                        && mUtil.getView(mActivity, R.id.llGotoPolicyChk).isSelected()) {
                    mUtil.getView(mActivity, R.id.llAllChk).setSelected(true);
                } else {
                    mUtil.getView(mActivity, R.id.llAllChk).setSelected(false);
                }
                break;
            case R.id.llGotoPolicyChk:
                if (view.isSelected()) {
                    view.setSelected(false);
                } else {
                    view.setSelected(true);
                }
                if (mUtil.getView(mActivity, R.id.llGotoServiceChk).isSelected()
                        && mUtil.getView(mActivity, R.id.llGotoPolicyChk).isSelected()) {
                    mUtil.getView(mActivity, R.id.llAllChk).setSelected(true);
                } else {
                    mUtil.getView(mActivity, R.id.llAllChk).setSelected(false);
                }
                break;
            case R.id.llAllChk:
                if (view.isSelected()) {
                    view.setSelected(false);
                    mUtil.getView(mActivity, R.id.llGotoServiceChk).setSelected(false);
                    mUtil.getView(mActivity, R.id.llGotoPolicyChk).setSelected(false);
                } else {
                    view.setSelected(true);
                    mUtil.getView(mActivity, R.id.llGotoServiceChk).setSelected(true);
                    mUtil.getView(mActivity, 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 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.btnToolbarDel).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_join_chk_01));
    }

    /**
     * UI display.
     */
    private void displayListView() {

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

    /**
     * 약관동의 확인.
     *
     * @return 약관동의 여부
     */
    private Boolean checkFormValId() {

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

}
