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

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

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.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.SignAdapter;
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 MySignActivity extends BaseActivity implements RefreshListView.RefreshListener{
    @BindView(R.id.empty)
    EmptyView emptyView;
    @BindView(R.id.refreshList)
    RefreshListView refreshListView;

    private List<ActivitiesInfo> mList = new ArrayList<>();
    private SignAdapter signAdapter;
    private int pageNo = 1;

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

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

        signAdapter = new SignAdapter(this);
        refreshListView.getListView().setAdapter(signAdapter);

        refreshListView.autoRefresh();

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

    @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) {
        ActivitiesService.getMyActivities(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;
                    signAdapter.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();
            }
        });
    }
}
