package com.ewei.demo.article;

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

import com.ewei.demo.R;
import com.ewei.demo.article.adatper.ArticleListAdatper;
import com.ewei.demo.article.adatper.TopicListAdatper;
import com.ewei.demo.widget.HorizontalListView;
import com.ewei.sdk.base.BaseActivity;
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.entity.Topic;
import com.ewei.sdk.entity.TopicPageModel;
import com.ewei.sdk.http.ArticleHttp;
import com.ewei.sdk.listener.RequestListener;
import com.ewei.sdk.utility.LogUtils;
import com.ewei.sdk.utility.ToastUtils;
import com.ewei.sdk.other.ArticleDetailWebBrowserActivity;


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

    private ListView mArList;
    private ArticleListAdatper mArticleListAdapter;
    private HorizontalListView horizontalListView;
    private TopicListAdatper topicListAdatper;
    private boolean isArticle;

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

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

        isArticle = getIntent().getBooleanExtra("isArticle", true);
        initControl();
        requestTopicList();
    }


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

        initTitle("帮助与反馈");

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

        horizontalListView = (HorizontalListView) findViewById(R.id.article_lhorizontal_listview);
        topicListAdatper = new TopicListAdatper(this);
        horizontalListView.setAdapter(topicListAdatper);
        horizontalListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Topic itemTopic = (Topic) parent.getAdapter().getItem(position);
                if (itemTopic != null) {
                    requestArticleList(itemTopic.id);
                }
            }
        });
    }


    private void requestTopicList() {
        ArticleHttp.getInstance().listTopics(null, isArticle, new RequestListener<TopicPageModel>() {
            @Override
            public void onSuccess(final TopicPageModel resultData, boolean isSuccess) {
                if (resultData != null && resultData.list != null) {
                    topicListAdatper.removeAll();
                    for (Topic parTopic : resultData.list) {
                        if (parTopic.childrens != null && parTopic.childrens.size() > 0) {
                            topicListAdatper.appendList(parTopic.childrens);
                        }
                    }
                    Topic firstTopic = (Topic) topicListAdatper.getItem(0);
                    if (firstTopic != null) {
                        requestArticleList(firstTopic.getId());
                    }
                }

            }

            @Override
            public void onFailure(HttpErrorInfo errorInfo) {
                LogUtils.error(TAG, errorInfo);
            }
        });
    }

    private void requestArticleList(Integer topicId) {
        ArticleHttp.getInstance().listArticleByTopic(new Page(1, Integer.MAX_VALUE), topicId, null, new RequestListener<ArticlePageModel>() {
            @Override
            public void onSuccess(final ArticlePageModel resultData, boolean isSuccess) {
                if (resultData != null && resultData.list != null) {
                    mArticleListAdapter.addList(resultData.list);
                } else {
                    ToastUtils.showToast("该目录下未找到相应文章");
                }
            }

            @Override
            public void onFailure(HttpErrorInfo errorInfo) {
                LogUtils.error(TAG, errorInfo);
                ToastUtils.showToast(errorInfo == null ? "获取文章列表失败" : errorInfo.error_description);
            }
        });
    }

    @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);
        }
    }
}
