package com.kfzs.duanduan.fragment;

import android.app.Activity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.sheep.gamegroup.model.entity.BaseMessage;
import com.sheep.gamegroup.model.entity.TaskReleaseEty;
import com.sheep.gamegroup.model.util.SheepSubscriber;
import com.sheep.gamegroup.util.CommonUtil;
import com.sheep.gamegroup.util.Jump2View;
import com.sheep.gamegroup.view.adapter.CreditCardTaskAdp;
import com.sheep.gamegroup.view.customview.RefreshLayout;
import com.sheep.jiuyan.samllsheep.R;
import com.sheep.jiuyan.samllsheep.SheepApp;
import com.sheep.jiuyan.samllsheep.base.BaseFragment;
import com.sheep.jiuyan.samllsheep.utils.G;

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

import butterknife.BindView;
import butterknife.ButterKnife;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by realicing on 2018/4/24.
 * realicing@sina.com
 */
public class FgtCreditCardTaskList extends BaseFragment {
    @BindView(R.id.credit_card_task_list)
    ListView credit_card_task_list;
    @BindView(R.id.swipe_container)
    RefreshLayout swipe_container;
    @BindView(R.id.empty_view)
    View empty_view;
    @Override
    public int getLayoutId() {
        return R.layout.fgt_credit_card_task_list;
    }

    private List<TaskReleaseEty> list = new ArrayList<>();
    private CreditCardTaskAdp adapter;
    private Activity activity;
    @Override
    public void onViewCreated() {
        ButterKnife.bind(this, mView);
        activity = getActivity();
        swipe_container.setFooterView(activity, credit_card_task_list, R.layout.listview_footer);
        adapter = new CreditCardTaskAdp(activity, list);
        credit_card_task_list.setAdapter(adapter);
        credit_card_task_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                try {
                    onItemClickTask(list.get(i));
                } catch (Exception ignore) {
                    G.showToast(R.string.unknown_error);
                }
            }
        });
        swipe_container.setOnRefreshListener(new RefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                initData();
            }
        });
        initData();
    }

    private void onItemClickTask(TaskReleaseEty taskReleaseEty) {
        taskReleaseEty.getTask().setBonus(taskReleaseEty.getBonusText());
        taskReleaseEty.getTask().setName(taskReleaseEty.getName());
        Jump2View.getInstance().goCreditCardTaskDetail(activity, taskReleaseEty.getId());
    }

    private void initData() {
        list.clear();
        adapter.notifyDataSetChanged();
        empty_view.setVisibility(View.INVISIBLE);
        SheepApp.getInstance().getNetComponent().getApiService().getReleaseTaskCreditCard()
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(new SheepSubscriber<BaseMessage>(SheepApp.getInstance()) {
                            @Override
                            public void onNext(BaseMessage baseMessage) {
                                List<TaskReleaseEty> newList = baseMessage.getDatas(TaskReleaseEty.class);
                                list.addAll(newList);
                                notifyDataSetChanged();
                            }

                            @Override
                            public void onError(BaseMessage baseMessage) {
                                notifyDataSetChanged();
                            }
                        });
    }
    private void notifyDataSetChanged() {
        CommonUtil.getInstance().updateEmptyView(empty_view, list.isEmpty());
        adapter.notifyDataSetChanged();
        swipe_container.setRefreshing(false);
    }
}
