package com.danding.yang800.Presenter;

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

import com.danding.yang800.Adapter.MarketPlatAdapter;
import com.danding.yang800.Entity.MarketPlatInfo;
import com.danding.yang800.Fragment.MarketPlatformFrag;
import com.danding.yang800.Fragment.MyBaseFragment;
import com.danding.yang800.General.HttpCallBack;
import com.danding.yang800.GiftInfoActivity;
import com.danding.yang800.MarketActivity;
import com.danding.yang800.Util.StringUtil;
import com.danding.yang800.Util.VolleyUtil;
import com.danding.yang800.Vo.GiftInfoVO;
import com.danding.yang800.Vo.MarketPlatformVO;
import com.google.gson.Gson;

import org.json.JSONObject;

import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.List;

public class MarketPlatformFragPresenter {
    private MarketPlatformFrag context;
    SharedPreferences sp;
    MarketPlatAdapter adapter;

    public MarketPlatformFragPresenter(MarketPlatformFrag context, MarketPlatAdapter 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(), "user.promotion.platform", true, map, new HttpCallBack() {
            @Override
            public void onSuccess(JSONObject json) {
                try {
                    Gson gson = new Gson();
                    MarketPlatformVO bean = gson.fromJson(json.toString(), MarketPlatformVO.class);
                    List<MarketPlatformVO.ResultBean> result = bean.getResult();
                    if (adapter != null) {
                        adapter.clear();
                    }
                    for (int i = 0; i < result.size(); i++) {
                        int id = result.get(i).getId();
                        String name = result.get(i).getName();
                        String receiveFee = result.get(i).getReceiveFee();
                        String startTime = result.get(i).getStartTime();
                        String endTime = result.get(i).getEndTime();
                        String receiveRuleDesc = result.get(i).getReceiveRuleDesc();
                        boolean canShare = result.get(i).isCanShare();
                        adapter.addData(new MarketPlatInfo(id, name, receiveFee, startTime, endTime, receiveRuleDesc, canShare));
                    }
                    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) {
        HashMap<String, String> map = new HashMap<>();
        map.put("promotionId", id);
        map.put("shopId", sp.getString("storeId", ""));
        map.put("userPromotionId", "0");
        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", ""));
                } catch (Exception e) {

                }
            }

            @Override
            public void onerrSuccess() {

            }

            @Override
            public void onCatch() {

            }

            @Override
            public void onFail() {

            }
        });
    }


}
