package com.danding.yang800.Presenter;

import android.content.Context;
import android.content.SharedPreferences;

import com.danding.yang800.Adapter.TicketNotUseAdapter;
import com.danding.yang800.Adapter.TicketUsedAdapter;
import com.danding.yang800.Entity.TicketNotUseInfo;
import com.danding.yang800.Fragment.MyTicketNotUseFrag;
import com.danding.yang800.Fragment.MyTicketUsedFrag;
import com.danding.yang800.General.HttpCallBack;
import com.danding.yang800.Util.VolleyUtil;
import com.danding.yang800.Vo.NotUseTicketVO;
import com.google.gson.Gson;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.List;

public class MyTicketNotUseFragPresenter {
    private MyTicketNotUseFrag context;
    SharedPreferences sp;
    TicketNotUseAdapter adapter;

    public MyTicketNotUseFragPresenter(MyTicketNotUseFrag context, TicketNotUseAdapter adapter) {
        sp = context.getContext().getSharedPreferences("variable", Context.MODE_PRIVATE);
        this.context = context;
        this.adapter = adapter;
    }

    public void getInfo(String page, final boolean load) {
        HashMap<String, String> map = new HashMap<>();
        map.put("page", page);
        map.put("size", "8");
        new VolleyUtil(context.getContext()).request(context.getContext(), "coupon.expireList", true, map, new HttpCallBack() {
            @Override
            public void onSuccess(JSONObject json) {
                try {
                    Gson gson = new Gson();
                    NotUseTicketVO bean = gson.fromJson(json.toString(), NotUseTicketVO.class);
                    NotUseTicketVO.ResultBeanX result = bean.getResult();
                    NotUseTicketVO.ResultBeanX.PaginationBean pagination = result.getPagination();
                    int totalPage = pagination.getTotalPage();
                    if (!load) {
                        if (adapter != null) {
                            adapter.clear();
                        }
                    }
                    List<NotUseTicketVO.ResultBeanX.ResultBean> resultList = result.getResult();
                    for (int i = 0; i < resultList.size(); i++) {
                        int id = resultList.get(i).getPromotionId();
                        int discountFee = resultList.get(i).getDiscountFee();
                        String name = resultList.get(i).getName();
                        String condition = resultList.get(i).getCondition();
                        String userTimeRange = resultList.get(i).getUserTimeRange();
                        int status = resultList.get(i).getStatus();
                        boolean canShare = resultList.get(i).isCanShare();
                        int userPromotionId = resultList.get(i).getUserPromotionId();
                        String userReceiveName = resultList.get(i).getUserReceiveName();
                        String userReceiveAvatar = resultList.get(i).getUserReceiveAvatar();
                        String userReceiveTime = resultList.get(i).getUserReceiveTime();
                        adapter.addData(new TicketNotUseInfo(id, discountFee, name, condition, userTimeRange, canShare, status
                                , userPromotionId, userReceiveName, userReceiveAvatar, userReceiveTime));
                    }
                    context.updateData(totalPage);
                } catch (Exception e) {
                    context.onLoad();
                }

            }

            @Override
            public void onerrSuccess() {
                context.onLoad();
            }

            @Override
            public void onCatch() {

            }

            @Override
            public void onFail() {
                context.onLoad();
            }
        });
    }


}
