package com.ewei.demo.article;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;

import com.ewei.demo.R;
import com.ewei.demo.article.adatper.ArticleListAdatper;
import com.ewei.sdk.base.BaseActivity;
import com.ewei.sdk.constants.NetWorkValue;
import com.ewei.sdk.entity.Article;
import com.ewei.sdk.entity.ArticlePageModel;
import com.ewei.sdk.entity.HttpErrorInfo;
import com.ewei.sdk.entity.Page;
import com.ewei.sdk.http.ArticleHttp;
import com.ewei.sdk.listener.RequestListener;
import com.ewei.sdk.other.ArticleDetailWebBrowserActivity;
import com.ewei.sdk.widget.NetWorkList;

import java.util.List;


/**
 * 文章列表
 *
 * @author Jay
 * @date 2016/3/3
 */
public class RecommendListActivity extends BaseActivity implements AdapterView.OnItemClickListener, NetWorkList.OnLoadListener {

    private NetWorkList mArList;
    private ArticleListAdatper mArticleListAdapter;

    private int mPage = 1;
    private int mCount = 15;
    private boolean isGetData = false;

    private static final String TAG = RecommendListActivity.class.getSimpleName();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recommend);

        initControl();
        requestArticleList();
    }


    /**
     * 初始化基本控件
     */
    private void initControl() {

        initTitle("推荐文章");

        mArList = (NetWorkList) findViewById(R.id.ptr_article_list);
        mArticleListAdapter = new ArticleListAdatper(this);
        mArList.setAdapter(mArticleListAdapter);
        mArList.setOnItemClickListener(this);
        mArList.setOnLoadListener(this);
    }


    private void requestArticleList() {

        if (isGetData) {
            return;
        }
        isGetData = true;

        ArticleHttp.getInstance().listRecommendArticle(new Page(mPage, mCount), null, new RequestListener<ArticlePageModel>() {
            @Override
            public void onSuccess(ArticlePageModel resultData, boolean isSuccess) {
                isGetData = false;
                mArList.stopLoad();
                if (mPage == 1) {
                    if (!isSuccess || resultData == null) {
                        mArList.showNoNetWork();
                    } else {
                        if (resultData.recordCount == 0) {
                            mArList.showNoData(NetWorkValue.NETWORK_NODATA_TICKET, "当前没有帮助文档");
                        } else {
                            mArList.hideNetWork();
                            resolveData(resultData.list);
                        }
                    }
                } else if (resultData != null && resultData.list != null && resultData.list.size() > 0) {
                    resolveData(resultData.list);
                }
            }

            @Override
            public void onFailure(HttpErrorInfo errorInfo) {
                mArList.stopLoad();
                isGetData = false;
                if (mPage == 1) {
                    mArList.showNoNetWork();
                }
            }
        });
    }


    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Article qs = (Article) parent.getAdapter().getItem(position);
        if (qs != null) {
            ArticleDetailWebBrowserActivity.startActivity(this, qs.title, qs.topic.type, qs.id);
        }
    }


    private void resolveData(List<Article> articles) {

        //防止底部分割线变粗
        if (articles != null && articles.size() < mCount) {
            mArList.setPullLoadEnable(false);
        } else {
            mArList.setPullLoadEnable(true);
        }
        if (mPage == 1) {
            mArticleListAdapter.addList(articles);
        } else {
            mArticleListAdapter.appendList(articles);
        }

        mPage += 1;
    }

    @Override
    public void onLoadMore() {
        requestArticleList();
    }

    @Override
    public void onRefresh() {
        mPage = 1;
        requestArticleList();
    }
}
