package com.sheep.gamegroup.view.fragment;

import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.sheep.gamegroup.absBase.IContentContainer;
import com.sheep.gamegroup.absBase.ISearch;
import com.sheep.gamegroup.model.api.ApiService;
import com.sheep.gamegroup.model.entity.BaseMessage;
import com.sheep.gamegroup.model.entity.HomePageSearch;
import com.sheep.gamegroup.util.ApiUtil;
import com.sheep.gamegroup.util.Jump2View;
import com.sheep.gamegroup.util.ListUtil;
import com.sheep.gamegroup.view.adapter.HomePageSearchAdapter;

import java.util.Locale;

import io.reactivex.Observable;

/**
 * Created by realicing on 2018/9/13.
 * realicing@sina.com
 */
public class FgtSearchAppOrTask extends BaseListFragment3<HomePageSearch> implements ISearch, BaseQuickAdapter.OnItemClickListener, IContentContainer<String> {

    @Override
    public int refreshDataType() {
        return -1;
    }

    private String search_content;

    @Override
    protected RecyclerView.Adapter getAdapter() {
        HomePageSearchAdapter adapter = new HomePageSearchAdapter(list);
        adapter.setContentContainer(this);
        adapter.setOnItemClickListener(this);
        return adapter;
    }

    @Override
    protected String getKey(int page, int per_page) {
        return String.format(Locale.CHINA, "app/top_search_statistics/home_page_search?search_content=%s", search_content);
    }

    @Override
    protected Observable<BaseMessage> getApi(ApiService apiService) {
        return apiService.getTopSearchStatisticsHomePageSearch(search_content);
    }

    @Override
    protected Class<HomePageSearch> getTClass() {
        return HomePageSearch.class;
    }

    public void clear() {
        list.clear();
    }

    @Override
    public void toSearch(String name) {
        this.search_content = name;
        refreshData();
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        HomePageSearch item = ListUtil.getItem(list, position);
        ApiUtil.postTopSearchStatisticsClickTopSearch(item);
        Jump2View.getInstance().goTaskOrAppDetail(item);
    }

    @Override
    public String getContent() {
        return search_content;
    }
}
