package com.danding.yang800.Presenter;

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

import com.danding.yang800.EditMarketActivity;
import com.danding.yang800.General.HttpCallBack;
import com.danding.yang800.GiftInfoActivity;
import com.danding.yang800.Util.StringUtil;
import com.danding.yang800.Util.ToastUtil;
import com.danding.yang800.Util.VolleyUtil;
import com.danding.yang800.Vo.GiftInfoVO;
import com.google.gson.Gson;

import org.json.JSONObject;

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

public class EditMarketActivityPresenter {
    private EditMarketActivity context;
    SharedPreferences sp;

    public EditMarketActivityPresenter(EditMarketActivity context) {
        sp = context.getSharedPreferences("variable", Context.MODE_PRIVATE);
        this.context = context;
    }

    public void create(String shopId, String name, String promotionId, String defId, String receiveFee, String conditionFee,
                       String availableQuantity, String receiveStartTime, String receiveEndTime,
                       String startTime, String endTime) {
        if (name.equals("")) {
            ToastUtil.show(context, "活动名称不能为空！");
            return;
        }
        if (receiveFee.equals("")) {
            ToastUtil.show(context, "金额不能为空！");
            return;
        }
        if (availableQuantity.equals("")) {
            ToastUtil.show(context, "数量不能为空！");
            return;
        }
        if (receiveEndTime.equals("")) {
            ToastUtil.show(context, "领取时限不能为空！");
            return;
        }
        if (conditionFee.equals("")) {
            ToastUtil.show(context, "使用条件不能为空！");
            return;
        }
        if (startTime.equals("")) {
            ToastUtil.show(context, "使用开始时间不能为空！");
            return;
        }
        if (endTime.equals("")) {
            ToastUtil.show(context, "使用结束时间不能为空！");
            return;
        }
        if (!StringUtil.isDateOneBigger(receiveStartTime, receiveEndTime)) {
            ToastUtil.show(context, "领取时限开始时间不能大于结束时间！");
            return;
        }
        if (!StringUtil.isDateOneBigger(startTime, endTime)) {
            ToastUtil.show(context, "使用时限开始时间不能大于结束时间！");
            return;
        }
        HashMap<String, String> map = new HashMap<>();
        map.put("shopId", shopId);
        map.put("promotionId", promotionId);
        map.put("defId", defId);
        map.put("receiveFee", receiveFee);
        map.put("conditionFee", conditionFee);
        map.put("availableQuantity", availableQuantity);
        map.put("receiveStartTime", receiveStartTime);
        map.put("receiveEndTime", receiveEndTime);
        map.put("startTime", startTime);
        map.put("endTime", endTime);
        new VolleyUtil(context).request(context, "goods.giftBag.info", true, map, new HttpCallBack() {
            @Override
            public void onSuccess(JSONObject json) {
                try {
//                    Gson gson = new Gson();
//                    GiftInfoVO bean = gson.fromJson(json.toString(), GiftInfoVO.class);
//                    GiftInfoVO.ResultBean result = bean.getResult();
//                    String skuName = result.getSkuName();
//                    String imageUrl = result.getImageUrl();
//                    String content = result.getContent();
//                    double marketPrice = result.getMarketPrice();
//                    String priceStr = new DecimalFormat("0.00").format(marketPrice);
//                    String priceArray[] = StringUtil.splitDouble(priceStr);
//                    String priceInt = "";
//                    String price = "";
//                    if (priceArray.length == 2) {
//                        priceInt = priceArray[0] + ".";
//                        price = priceArray[1];
//                    }
//                    context.initData(skuName, imageUrl, content, priceInt, price);
                } catch (Exception e) {

                }

            }

            @Override
            public void onerrSuccess() {

            }

            @Override
            public void onCatch() {

            }

            @Override
            public void onFail() {

            }
        });
    }

    public void getInfo(String shopId, String promotionId) {

        HashMap<String, String> map = new HashMap<>();
        map.put("shopId", shopId);
        map.put("promotionId", promotionId);

        new VolleyUtil(context).request(context, "user.promotion.edit", true, map, new HttpCallBack() {
            @Override
            public void onSuccess(JSONObject json) {
                try {
//                    Gson gson = new Gson();
//                    GiftInfoVO bean = gson.fromJson(json.toString(), GiftInfoVO.class);
//                    GiftInfoVO.ResultBean result = bean.getResult();
//                    String skuName = result.getSkuName();
//                    String imageUrl = result.getImageUrl();
//                    String content = result.getContent();
//                    double marketPrice = result.getMarketPrice();
//                    String priceStr = new DecimalFormat("0.00").format(marketPrice);
//                    String priceArray[] = StringUtil.splitDouble(priceStr);
//                    String priceInt = "";
//                    String price = "";
//                    if (priceArray.length == 2) {
//                        priceInt = priceArray[0] + ".";
//                        price = priceArray[1];
//                    }
//                    context.initData(skuName, imageUrl, content, priceInt, price);
                } catch (Exception e) {

                }

            }

            @Override
            public void onerrSuccess() {

            }

            @Override
            public void onCatch() {

            }

            @Override
            public void onFail() {

            }
        });
    }

    public void getType() {
        HashMap<String, String> map = new HashMap<>();
        new VolleyUtil(context).request(context, "user.promotion.defs", true, map, new HttpCallBack() {
            @Override
            public void onSuccess(JSONObject json) {
                try {
//                    Gson gson = new Gson();
//                    GiftInfoVO bean = gson.fromJson(json.toString(), GiftInfoVO.class);
//                    GiftInfoVO.ResultBean result = bean.getResult();
//                    String skuName = result.getSkuName();
//                    String imageUrl = result.getImageUrl();
//                    String content = result.getContent();
//                    double marketPrice = result.getMarketPrice();
//                    String priceStr = new DecimalFormat("0.00").format(marketPrice);
//                    String priceArray[] = StringUtil.splitDouble(priceStr);
//                    String priceInt = "";
//                    String price = "";
//                    if (priceArray.length == 2) {
//                        priceInt = priceArray[0] + ".";
//                        price = priceArray[1];
//                    }
//                    context.initData(skuName, imageUrl, content, priceInt, price);
                } catch (Exception e) {

                }

            }

            @Override
            public void onerrSuccess() {

            }

            @Override
            public void onCatch() {

            }

            @Override
            public void onFail() {

            }
        });
    }


}
