package com.android.chmo.ui.activity.model;

import android.text.TextUtils;
import android.view.View;

import com.android.chmo.R;
import com.android.chmo.base.BaseActivity;
import com.android.chmo.constant.Constants;
import com.android.chmo.http.RequestCallback;
import com.android.chmo.http.response.ModelListRes;
import com.android.chmo.http.service.ModelService;
import com.android.chmo.model.LabelInfo;
import com.android.chmo.model.ModelInfo;
import com.android.chmo.ui.adpater.ModelAdapter;
import com.android.chmo.ui.view.EmptyView;
import com.android.chmo.ui.view.RefreshListView;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;


public class SearchResultActivity extends BaseActivity implements RefreshListView.RefreshListener{

    @BindView(R.id.refreshList)
    RefreshListView refreshListView;

    @BindView(R.id.empty)
    EmptyView emptyView;

    private ModelAdapter modelAdapter;

    private List<ModelInfo> mList = new ArrayList<>();

    private int pageNo = 1;

    private String searchText;
    private LabelInfo label;

    @Override
    protected int getContentResId() {
        return R.layout.activity_search_result;
    }

    @Override
    public void initView() {
        if(getIntent().hasExtra("search")) {
            searchText = getIntent().getStringExtra("search");
        }else if(getIntent().hasExtra("label")) {
            label = (LabelInfo) getIntent().getSerializableExtra("label");
        }

        refreshListView.setRefreshListener(this);
        modelAdapter = new ModelAdapter(this);
        refreshListView.setAdapter(modelAdapter);

        refreshListView.autoRefresh();
    }


    @Override
    public void onRefresh() {
        this.search(1);
    }

    @Override
    public void onLoadMore() {
        refreshListView.postDelayed(new Runnable() {
            @Override
            public void run() {
                search(pageNo + 1);
            }
        }, 500);
    }



    private void search(final int page) {
        RequestCallback requestCallback = new RequestCallback() {
            @Override
            public void onSuccess(String result) {
                refreshListView.finishRefresh();
                ModelListRes res = new Gson().fromJson(result, ModelListRes.class);
                if(res.data != null) {
                    if(page == 1) {
                        mList.clear();
                    }
                    mList.addAll(res.data);
                    pageNo = page;
                    modelAdapter.setList(mList);

                    if(res.data.size() == Constants.PAGE_SIZE) {
                        refreshListView.setEnableLoadMore(true);
                    }else {
                        refreshListView.setNoMoreData();
                    }
                }
                emptyView.setVisibility(mList.size() ==0 ? View.VISIBLE : View.GONE);
            }

            @Override
            public void onFailure(String error) {
                refreshListView.finishRefresh();
            }
        };

        if(!TextUtils.isEmpty(searchText)) {
            ModelService.getModelList(searchText,page, requestCallback);
        }else if(label != null) {
            ModelService.getModelByLabel(label.pk,page, requestCallback);
        }

    }
}
