package com.ewei.demo.article;

import android.content.Intent;
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.TopicListAdatper;
import com.ewei.sdk.base.BaseActivity;
import com.ewei.sdk.constants.QuestionValue;
import com.ewei.sdk.entity.HttpErrorInfo;
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 java.io.Serializable;


/**
 * 论坛目录列表
 *
 * @author Jay
 * @date 2016/3/3
 */
public class QuestionTopicListActivity extends BaseActivity implements AdapterView.OnItemClickListener {

    private ListView mLvTopicList;
    private TopicListAdatper topicListAdatper;

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

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

        initControl();
        requestTopicList();
    }

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

        initTitle("论坛目录");

        mLvTopicList = (ListView) findViewById(R.id.ptr_topic_list);
        topicListAdatper = new TopicListAdatper(this);
        mLvTopicList.setAdapter(topicListAdatper);
        mLvTopicList.setOnItemClickListener(this);
    }


    private void requestTopicList() {
        ArticleHttp.getInstance().listTopics(QuestionValue.LanguageType.ZH_CN, false, 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);
                        }
                    }
                }
            }

            @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) {
        Topic topic = (Topic) parent.getAdapter().getItem(position);
        if (topic != null) {
            Intent intent = new Intent(QuestionTopicListActivity.this, ArticleForTopicListActivity.class);
            intent.putExtra("topicInfo", topic);
            startActivity(intent);
        }
    }
}
