package com.android.chmo.ui.fragment.home;

import android.os.Bundle;
import android.view.View;

import com.android.chmo.R;
import com.android.chmo.base.BaseFragment;
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.ModelInfo;
import com.android.chmo.ui.adpater.ModelAdapter;
import com.android.chmo.ui.view.BannerView;
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 ActorFragment extends BaseFragment implements RefreshListView.RefreshListener{
    @BindView(R.id.refreshList)
    RefreshListView refreshListView;

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

    private ModelAdapter modelAdapter;

    private BannerView bannerView;

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

    private int pageNo = 1;

    private boolean isInit = false;

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

    @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("modelChange")) {
            onRefresh();
        }
    };

    @Override
    public void initView() {
        refreshListView.setRefreshListener(this);
        refreshListView.getListView().addHeaderView(initHeaderView());

        modelAdapter = new ModelAdapter(getBaseActivity());
        refreshListView.setAdapter(modelAdapter);

    }

    private View initHeaderView() {
        if(bannerView == null) {
            bannerView = new BannerView(getActivity());
            bannerView.setFragmentManager(getChildFragmentManager());
            bannerView.startLoop();
        }
        return bannerView;
    }

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

        if (null != bannerView) {
            bannerView.startLoop();
        }
    }

    @Override
    public void onInvisible() {
        if (null != bannerView) {
            bannerView.stopLoop();
        }
    }

    @Override
    public void onPause() {
        super.onPause();
        if (null != bannerView) {
            bannerView.stopLoop();
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        if (null != bannerView && isVisible) {
            bannerView.startLoop();
        }
    }


    @Override
    public void onRefresh() {
        this.loadModelList(1);
        bannerView.loadBanner();
    }

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

    }



    private void loadModelList(final int page) {
        ModelService.getModelList(null,page, 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();
            }
        });
    }

}
