package com.danding.yang800.Presenter;

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

import com.android.volley.RequestQueue;
import com.danding.yang800.BuildConfig;
import com.danding.yang800.ChangePriceActivity;
import com.danding.yang800.General.HttpCallBack;
import com.danding.yang800.General.SingleRequestQueue;
import com.danding.yang800.Util.LogUtil;
import com.danding.yang800.Util.StringUtil;
import com.danding.yang800.Util.ToastUtil;
import com.danding.yang800.Util.VolleyUtil;

import org.json.JSONObject;

import java.util.HashMap;

public class ChangePriceActivityPresenter {
    private ChangePriceActivity context;
    SharedPreferences sp;
    RequestQueue requestQueue;

    public ChangePriceActivityPresenter(ChangePriceActivity context) {
        sp = context.getSharedPreferences("variable", Context.MODE_PRIVATE);
        this.context = context;
        requestQueue = SingleRequestQueue.getRequestQueue(context);
    }

    public void getGoodsInfo(final String storeId, final String goodsSn, final String skuSn) {
        HashMap<String, String> map = new HashMap<>();
        map.put("goodsSn", goodsSn);
        map.put("skuSn", skuSn);
        map.put("storeId", storeId);
        new VolleyUtil(context).request(context, "goods.changePrice.getGoodsInfo", true, map, new HttpCallBack() {
            @Override
            public void onSuccess(JSONObject json) {
                try {
                    JSONObject jsonData = json.getJSONObject("result");
                    String goodsName = jsonData.getString("goodsName");
                    String skuImageUrl = jsonData.getString("skuImageUrl");
                    String skuMarketPrice = StringUtil.doubleToString(jsonData, "skuMarketPrice");
                    double skuMarketPriceDouble = jsonData.getDouble("skuMarketPrice");
                    String skuSupplyPrice = StringUtil.doubleToString(jsonData, "skuSupplyPrice");
                    String storePrice = StringUtil.doubleToString(jsonData, "storePrice");
                    context.updateData(goodsName, skuImageUrl, skuMarketPrice, skuMarketPriceDouble, skuSupplyPrice, storePrice);
                    context.dismissDialog();
                } catch (Exception e) {
                    e.printStackTrace();
                    onCatch();
                }

            }

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

            @Override
            public void onCatch() {
                context.dismissDialog();
            }

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

    public void updatePrice(final String storeId, final String skuSn, final String price, final double MarketPrice) {

        if (price.equals("")) {
            ToastUtil.show(context, "价格不能为空");
            return;
        }
        double skuPrice = Double.parseDouble(price);
        if (skuPrice < MarketPrice) {
            ToastUtil.show(context, "代购价需不低于最低销售价");
            return;
        }
        context.initDialog();
        HashMap<String, String> map = new HashMap<>();
        map.put("skuSn", skuSn);
        map.put("price", price);
        map.put("storeId", storeId);
        LogUtil.d(skuSn + "skuSn");
        new VolleyUtil(context).request(context, "goods.changePrice.updatePrice", true, map, new HttpCallBack() {
            @Override
            public void onSuccess(JSONObject json) {
                try {
                    ToastUtil.show(context, "改价成功");
                    context.dismissDialog();
                    context.saveSuccess();
                } catch (Exception e) {
                    e.printStackTrace();
                    onCatch();
                }

            }


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

            @Override
            public void onCatch() {
                context.dismissDialog();
            }

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


}
