/*
 * 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.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.AbsoluteSizeSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

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

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

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

    @BindView(R.id.tvComplete1) TextView mCompleteMsg;

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

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

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

        if (null != getIntent()) {
            mLoginId = getIntent().getStringExtra(Constants.EXTRA_LOGIN_ID);
        }

        setToolbar();
        displayView();
    }

    private void displayView() {
        String completeMsg = getString(R.string.layout_join_complete_02, mLoginId);
        SpannableStringBuilder sps = new SpannableStringBuilder(completeMsg);
        sps.setSpan(new AbsoluteSizeSpan(((int) getResources().getDimension(R.dimen.size_font_08)), false), 4, (4 + mLoginId.length()),
                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        mCompleteMsg.append(sps);
    }

    @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_1ST:
                    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;
                }
            }
        }
    }

    @OnClick(R.id.llBtn1)
    public void onClick(View view) {

        switch (view.getId()) {
            case R.id.llBtn1:
                onBackPressed();
                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.GONE);

        mUtil.getTextView(toolbar, R.id.tvToolbarTitle1)
                .setText(R.string.layout_join_complete_01);
    }

}
