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

package com.ntels.onecable.view.join;

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.CustomActivity;

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.LinearLayout;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

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

    private static final String JOIN_CURRENT_STEP = "1";

    private Activity mActivity;
    private Intent mIntent;
    private AppConfig mAppConfig;
    private Util mUtil = new Util();
    private String mJoinType = "";
    @BindView(R.id.llGotoServiceChk) LinearLayout mGoToServiceChk;
    @BindView(R.id.llGotoPolicyChk) LinearLayout mGoToPolicyChk;
    @BindView(R.id.llAllChk) LinearLayout mAllChk;
    @BindView(R.id.llBtn1) LinearLayout mNext;

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

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

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

        if (null != getIntent()) {
            mJoinType = getIntent().getStringExtra(Constants.EXTRA_JOIN_TYPE);
        }

        setToolbar();
        displayView();
    }

    private void displayView() {
        mNext.setEnabled(false);
    }

    @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_STEP_2ND_MASTER:
                case Constants.ACT_JOIN_STEP_2ND_MEMBER:
                    mIntent = new Intent();
                    setResult(RESULT_OK, mIntent);
                    finish();
                    break;
                default:
                    break;
            }
        } else {
            onBackPressed();
        }
    }

    @OnClick({R.id.llBtn1, R.id.llBtn2, R.id.llAllChk, R.id.llGotoService, R.id.llGotoServiceChk, R.id.llGotoPolicy, R.id.llGotoPolicyChk})
    public void onClick(View view) {

        Intent intent;
        switch (view.getId()) {
            case R.id.llBtn1:
                if (mJoinType.equals(Constants.GRADE_CODE_MAS)) {
                    intent = new Intent(mActivity, JoinStep2ndMaster.class);
                    mUtil.callActivityForResult(mActivity, intent, Constants.ACT_JOIN_STEP_2ND_MASTER);
                } else {
                    intent = new Intent(mActivity, JoinStep2ndMember.class);
                    mUtil.callActivityForResult(mActivity, intent, Constants.ACT_JOIN_STEP_2ND_MEMBER);
                }
                break;
            case R.id.llBtn2:
                onBackPressed();
                break;

            case R.id.llGotoServiceChk:
                if (view.isSelected()) {
                    view.setSelected(false);
                } else {
                    view.setSelected(true);
                }
                checkFormValId();
                break;
            case R.id.llGotoPolicyChk:
                if (view.isSelected()) {
                    view.setSelected(false);
                } else {
                    view.setSelected(true);
                }
                checkFormValId();
                break;
            case R.id.llAllChk:
                if (view.isSelected()) {
                    view.setSelected(false);
                    mGoToServiceChk.setSelected(false);
                    mGoToPolicyChk.setSelected(false);
                } else {
                    view.setSelected(true);
                    mGoToServiceChk.setSelected(true);
                    mGoToPolicyChk.setSelected(true);
                }
                checkFormValId();
                break;
            case R.id.llGotoService:
                intent = new Intent(Intent.ACTION_VIEW);
                intent.addCategory(Intent.CATEGORY_DEFAULT);
                intent.setData(Uri.parse(Constants.URL_TERMS_OF_SERVICE));
                startActivity(intent);
                break;
            case R.id.llGotoPolicy:
                intent = new Intent(Intent.ACTION_VIEW);
                intent.addCategory(Intent.CATEGORY_DEFAULT);
                intent.setData(Uri.parse(Constants.URL_TERMS_OF_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.btnToolbarClose).setVisibility(View.GONE);
        mUtil.getTextView(toolbar, R.id.btnToolbarMore).setVisibility(View.GONE);
        mUtil.getView(toolbar, R.id.llJoinStep).setVisibility(View.VISIBLE);
        mUtil.getTextView(toolbar, R.id.tvCurrentStep).setText(JOIN_CURRENT_STEP);
        mUtil.getTextView(toolbar, R.id.tvTotalStep).setText(Constants.JOIN_TOTAL_STEP);

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

    /**
     * 약관동의 확인.
     */
    private void checkFormValId() {

        Boolean isCheck = true;
        isCheck &= mGoToServiceChk.isSelected();
        isCheck &= mGoToPolicyChk.isSelected();

        mNext.setEnabled(isCheck);
        mAllChk.setSelected(isCheck);
    }
}
