package com.danding.yang800.Presenter;

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

import com.danding.yang800.Adapter.MarketPlatAdapter;
import com.danding.yang800.Adapter.TicketUsedAdapter;
import com.danding.yang800.Entity.MarketPlatInfo;
import com.danding.yang800.Entity.TicketNotUseInfo;
import com.danding.yang800.Fragment.MarketPlatformFrag;
import com.danding.yang800.Fragment.MyTicketUsedFrag;
import com.danding.yang800.General.HttpCallBack;
import com.danding.yang800.Util.VolleyUtil;
import com.danding.yang800.Vo.MarketPlatformVO;
import com.danding.yang800.Vo.NotUseTicketVO;
import com.danding.yang800.Vo.UsedTicketVO;
import com.google.gson.Gson;

import org.json.JSONObject;

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

public class MyTicketUsedFragPresenter {
    private MyTicketUsedFrag context;
    SharedPreferences sp;
    TicketUsedAdapter adapter;

    public MyTicketUsedFragPresenter(MyTicketUsedFrag context, TicketUsedAdapter adapter) {
        sp = context.getContext().getSharedPreferences("variable", Context.MODE_PRIVATE);
        this.context = context;
        this.adapter = adapter;
    }

    public void getInfo() {
        HashMap<String, String> map = new HashMap<>();
        new VolleyUtil(context.getContext()).request(context.getContext(), "coupon.unuseList", true, map, new HttpCallBack() {
            @Override
            public void onSuccess(JSONObject json) {
                try {
                    Gson gson = new Gson();
                    UsedTicketVO bean = gson.fromJson(json.toString(), UsedTicketVO.class);
                    List<UsedTicketVO.ResultBean> result = bean.getResult();
                    if (adapter != null) {
                        adapter.clear();
                    }
                    for (int i = 0; i < result.size(); i++) {
                        int id = result.get(i).getPromotionId();
                        int discountFee = result.get(i).getDiscountFee();
                        String name = result.get(i).getName();
                        String condition = result.get(i).getCondition();
                        String userTimeRange = result.get(i).getUserTimeRange();
                        boolean canShare = result.get(i).isCanShare();
                        int userPromotionId = result.get(i).getUserPromotionId();
                        String userReceiveName = result.get(i).getUserReceiveName();
                        String userReceiveAvatar = result.get(i).getUserReceiveAvatar();
                        String userReceiveTime = result.get(i).getUserReceiveTime();
                        adapter.addData(new TicketNotUseInfo(id, discountFee, name, condition, userTimeRange, canShare, 0,
                                userPromotionId, userReceiveName, userReceiveAvatar, userReceiveTime));
//                        String endTime = result.get(i).getEndTime();
//                        String receiveRuleDesc = result.get(i).getReceiveRuleDesc();
//                        adapter.addData(new MarketPlatInfo(id, name, conditionFee, startTime, endTime, receiveRuleDesc));
                    }
                    context.updateData();
                } catch (Exception e) {
                    context.onLoad();
                }

            }

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

            @Override
            public void onCatch() {

            }

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

    public void check(String id, final int position, final int userPromotionId) {
        HashMap<String, String> map = new HashMap<>();
        map.put("promotionId", id);
        map.put("shopId", sp.getString("storeId", ""));
        map.put("userPromotionId", userPromotionId + "");
        new VolleyUtil(context.getContext()).request(context.getContext(), "user.promotion.share.check", true, map, new HttpCallBack() {
            @Override
            public void onSuccess(JSONObject json) {
                try {
                    String result = json.getString("result");
                    context.share(position, result, sp.getString("storeId", ""), userPromotionId);
                } catch (Exception e) {

                }
            }

            @Override
            public void onerrSuccess() {

            }

            @Override
            public void onCatch() {

            }

            @Override
            public void onFail() {

            }
        });
    }
}
