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

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.base.BaseFragment;
import com.android.chmo.constant.Constants;
import com.android.chmo.http.RequestCallback;
import com.android.chmo.http.response.ActivitiesListRes;
import com.android.chmo.http.service.ActivitiesService;
import com.android.chmo.model.ActivitiesInfo;
import com.android.chmo.ui.activity.activities.ActivitiesDetailActivity;
import com.android.chmo.ui.adpater.ActivitiesAdapter;
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 ActivitiesFragment extends BaseFragment implements RefreshListView.RefreshListener{
    @BindView(R.id.refreshList)
    RefreshListView refreshListView;
    @BindView(R.id.empty)
    EmptyView emptyView;

    private ActivitiesAdapter activitiesAdapter;

    private int pageNo = 1;
    private List<ActivitiesInfo> mList = new ArrayList<>();
    private boolean isInit = false;



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


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

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

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

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

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

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

    }



    private void loadActivities(final int page) {
        ActivitiesService.getActivityList(page, new RequestCallback() {
            @Override
            public void onSuccess(String result) {
                refreshListView.finishRefresh();
                ActivitiesListRes res = new Gson().fromJson(result, ActivitiesListRes.class);
                if(res.data != null) {
                    if(page == 1) {
                        mList.clear();
                    }
                    mList.addAll(res.data);
                    pageNo = page;
                    activitiesAdapter.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();
            }
        });
    }
}
