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

package com.ntels.onecable.view.mainmore;

import com.ntels.onecable.R;
import com.ntels.onecable.common.AppConfig;
import com.ntels.onecable.common.Constants;
import com.ntels.onecable.common.c_interface.ListInterface;
import com.ntels.onecable.common.util.Dlog;
import com.ntels.onecable.common.util.Util;
import com.ntels.onecable.common.widget.CustomActivity;
import com.ntels.onecable.view.HomeStart;
import com.ntels.onecable.view.dialog.CustomDialog;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.webkit.WebView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * <pre>
 * 1. ClassName :   FaqDetail
 * 2. FileName  :   FaqDetail.java
 * 3. Package   :   com.ntels.onecable.view.mainmore
 * 4. Date      :   2016-12-01
 * 5. Author    :   aaron
 * 6. Comment   :   Initialize
 * </pre>
 */
public class FaqDetail extends CustomActivity implements View.OnClickListener, CustomDialog.OnMsgAlterToResult, ListInterface {

    private static final int MSG_COM_G_FAQ_DETAIL = 1;

    Handler mComHandler = new Handler(new IncomingHandlerCallBack());
    private Util mUtil = new Util();
    private CustomDialog mCustomDialog = new CustomDialog();
    private AppConfig mAppConfig;
    private Intent mIntent;
    private Activity mActivity;
    private SwipeRefreshLayout mSwipeRefreshLayout;
    private GridLayoutManager mGlmList;
    private LinearLayoutManager mLlmList;
    private Toolbar mToolbar;
    private ArrayList<HashMap<String, Object>> mTotalList = new ArrayList<>();
    private boolean mLoading = false;
    private boolean mIsListHeader = false;
    private int mType = 0;
    private int mListColumnsCnt = 1;
    private boolean mIsMore = true;
//    private String mFaqSource;
    private String mFaqTitleStr;
    private String mFaqId;
    @BindView(R.id.tvFaqTitle) TextView mFaqTitle;
    @BindView(R.id.wvFaqDetail) WebView mFaqWebView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        mActivity = FaqDetail.this;
        mAppConfig = (AppConfig) mActivity.getApplicationContext();
        mCustomDialog.setOnMsgAlterToResultEvent(this);
        View viewToLoad = LayoutInflater.from(mActivity).inflate(R.layout.faq_detail, null);

        this.setContentView(viewToLoad);

        ButterKnife.bind(this);

        if (null != getIntent()) {
            mFaqId = getIntent().getStringExtra(Constants.EXTRA_FAQ_ID);
            mFaqTitleStr = getIntent().getStringExtra(Constants.EXTRA_FAQ_TITLE);
            Dlog.i("mFaqId = " + mFaqId + ", mFaqTitle = " + mFaqTitleStr);
        }

        setToolbar();
        displayListView();
    }

    /**
     * 툴바 셋팅.
     */
    private void setToolbar() {

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

        mUtil.getTextView(mToolbar, R.id.tvToolbarTitle1)
                .setText(mUtil.getString(mActivity, R.string.layout_faq_detail_01));
    }

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

        mUtil.getView(mActivity, R.id.llBtn1).setOnClickListener(this);
        mFaqTitle.setText(mFaqTitleStr);

        mSwipeRefreshLayout = (SwipeRefreshLayout) mActivity.findViewById(R.id.swiperefreshlayout);
        mSwipeRefreshLayout.setColorSchemeResources(android.R.color.holo_red_light,
                android.R.color.holo_green_light, android.R.color.holo_blue_bright,
                android.R.color.holo_orange_light);
        mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                mFaqWebView.loadUrl(mAppConfig.getUrl() + mUtil.getString(mActivity, R.string.API_VERSION)
                        + mUtil.getString(mActivity, R.string.SEND_G_faq_detail, mFaqId));
                mLoading = false;
                mSwipeRefreshLayout.setRefreshing(false);
            }
        });
        setRefresh(true);
        mFaqWebView.getSettings().setJavaScriptEnabled(true);
        mFaqWebView.getSettings().setDefaultTextEncodingName("UTF-8");
        mFaqWebView.setHorizontalScrollBarEnabled(false);
        mFaqWebView.setVerticalScrollBarEnabled(false);
        mFaqWebView.setBackgroundColor(0);
//        mFaqWebView.loadData(mFaqSource, "text/html; charset=UTF-8", null);
        Dlog.i("loadUrl = " + mAppConfig.getUrl() + mUtil.getString(mActivity, R.string.API_VERSION)
                + mUtil.getString(mActivity, R.string.SEND_G_faq_detail, mFaqId));
        mFaqWebView.loadUrl(mAppConfig.getUrl() + mUtil.getString(mActivity, R.string.API_VERSION)
                + mUtil.getString(mActivity, R.string.SEND_G_faq_detail, mFaqId));
    }

    /**
     * mSwipeRefreshLayout enable/disable.
     *
     * @param isEnable true or false
     */
    private void setRefresh(boolean isEnable) {

        if (isEnable) {
            mSwipeRefreshLayout.setEnabled(true);
        } else {
            mSwipeRefreshLayout.setEnabled(false);
        }
    }

    @Override
    public void onClick(View view) {

        switch (view.getId()) {
            case R.id.llBtn1:
                onBackPressed();
                break;
            default:
                break;
        }
    }

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

        switch (msgId) {

            case Constants.MSG998:
                mUtil.callApp_Start(mActivity, "LOGOUT");
                break;
            case Constants.MSG997:
                mUtil.callApp_Start(mActivity, "HOME_RELOAD");
                break;
            case Constants.MSG807:
                if (btnResult == 1) {
                    mComHandler.sendMessageDelayed(mComHandler.obtainMessage(mType), Constants.HANDLER_DELAY_MILLIS);
                } else {
                    mActivity.onBackPressed();
                }
                break;
            default:
                break;
        }
    }

    @Override
    public void onBackPressed() {

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

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

        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case Constants.ACT_INVITATION_REPLY:
                    mIntent = new Intent(mActivity, HomeStart.class);
                    mUtil.callActivityForResultNoAnim(mActivity, mIntent, Constants.ACT_HOME_START);
                    break;
                case Constants.ACT_HOME_START:
                    mIntent = new Intent();
                    mActivity.setResult(RESULT_OK, mIntent);
                    finish();
                    break;
                default:
                    break;
            }
        } else {
            if (mAppConfig.getGoErr()) {
                mActivity.setResult(RESULT_CANCELED, new Intent());
                finish();
            }
        }
    }

    @Override
    public void onListReload() {
    }

    @Override
    public void onLogout() {
    }

    @Override
    public void onCallAction(int callType, int position) {

        if (callType == 0) {
            onBackPressed();
        }
    }

    class IncomingHandlerCallBack implements Handler.Callback {

        @Override
        public boolean handleMessage(Message msg) {

            mType = msg.what;
            switch (msg.what) {
                case MSG_COM_G_FAQ_DETAIL:
//                    sendPostFaq();
                    break;

                default:
                    break;
            }
            return true;
        }
    }
}