package com.android.chmo.ui.fragment;

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

import com.android.chmo.R;
import com.android.chmo.app.ChmoApplication;
import com.android.chmo.base.BaseFragment;
import com.android.chmo.constant.Constants;
import com.android.chmo.http.RequestCallback;
import com.android.chmo.http.response.FindListRes;
import com.android.chmo.http.service.FindService;
import com.android.chmo.model.FindInfo;
import com.android.chmo.ui.activity.find.FindDetailActivity;
import com.android.chmo.ui.adpater.FindAdapter;
import com.android.chmo.ui.view.EmptyView;
import com.android.chmo.ui.view.RefreshListView;
import com.google.gson.Gson;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

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

import butterknife.BindView;

/**
 * Created by Administrator on 2018/7/31.
 */

public class FindFragment extends BaseFragment implements RefreshListView.RefreshListener{
    @BindView(R.id.refreshList)
    RefreshListView refreshListView;

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

    private FindAdapter findAdapter;

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

    private int pageNo = 1;

    private boolean isInit = false;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(String event) {
        if(event.equals("findRefresh") || event.equals("followChange")) {
            onRefresh();
        }
    };

    @Override
    public int getLayoutRes() {
        return R.layout.page_find;
    }

    @Override
    public void initView() {
        refreshListView.setRefreshListener(this);
        findAdapter = new FindAdapter(getBaseActivity());
        refreshListView.setAdapter(findAdapter);

        refreshListView.getListView().setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(getBaseActivity(), FindDetailActivity.class);
                intent.putExtra("data", mList.get(position));
                startActivity(intent);
            }
        });
    }



    @Override
    public void onVisible() {
        if(!isInit) {
            if(refreshListView != null) {
                refreshListView.autoRefresh();
            }
            isInit = true;
        }
    }

    @Override
    public void onRefresh() {
        loadData(1);
    }

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



    private void loadData(final int page) {
        String pk = ChmoApplication.getApp().getLoginUser().pk;
        FindService.getFindList(pk, page, new RequestCallback() {
            @Override
            public void onSuccess(String result) {
                refreshListView.finishRefresh();
                FindListRes res = new Gson().fromJson(result, FindListRes.class);
                if(res.data != null) {
                    pageNo = page;
                    if(page == 1) {
                        mList.clear();
                    }
                    mList.addAll(res.data);
                    findAdapter.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();
            }
        });
    }


}
