package com.danding.yang800.Presenter;

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

import com.danding.yang800.BuildConfig;
import com.danding.yang800.General.HttpCallBack;
import com.danding.yang800.GoodsInfoActivity;
import com.danding.yang800.Util.LogUtil;
import com.danding.yang800.Util.StringUtil;
import com.danding.yang800.Util.VolleyUtil;

import org.json.JSONObject;

import java.util.HashMap;

public class GoodsInfoActivityPresenter {
    private GoodsInfoActivity context;
    SharedPreferences sp;

    public GoodsInfoActivityPresenter(GoodsInfoActivity context) {
        sp = context.getSharedPreferences("variable", Context.MODE_PRIVATE);
        this.context = 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);
        LogUtil.d(skuSn + "skuSn" + goodsSn);
        new VolleyUtil(context).request(context,  "goods.changePrice.getGoodsInfo", true, map, new HttpCallBack() {
            @Override
            public void onSuccess(JSONObject json) {
                LogUtil.d(json.toString());
                try {
                    JSONObject jsonData = json.getJSONObject("result");
                    String goodsName = jsonData.getString("goodsName");
                    String skuImageUrl = jsonData.getString("skuImageUrl");
                    String goodsType = jsonData.getString("goodsType");
                    int Inventory = jsonData.getInt("skuMarketInventory");
                    String skuMarketInventory = StringUtil.toString(jsonData.getString("skuMarketInventory"));
                    String countryName = StringUtil.toString(jsonData.getString("countryName"));
                    String depotName = StringUtil.toString(jsonData.getString("depotName"));
                    String storeProfit = StringUtil.doubleToString(jsonData, "storeProfit");
                    String weight = StringUtil.doubleToString(jsonData, "grossWeight");
                    boolean storePriceChange = jsonData.getBoolean("storePriceChange");
                    String goodsTax = StringUtil.doubleToString(jsonData, "goodsTax");
                    String profitArray[] = StringUtil.splitDouble(storeProfit);
                    int sellStatus = jsonData.getInt("sellStatus");
                    boolean taxFree ;
                    boolean expFree ;
                    try {
                        taxFree=jsonData.getBoolean("taxFree");
                        expFree= jsonData.getBoolean("expFree");
                    }catch (Exception e){
                        taxFree=false;
                        expFree=false;
                    }
                    String stats = "1";
                    if (sellStatus == 1 || Inventory <= 0) {
                        stats = "3";
                    }
                    String profitInt = "";
                    String profit = "";
                    if (profitArray.length == 2) {
                        profitInt = profitArray[0] + ".";
                        profit = profitArray[1];
                    }
                    String storePrice = StringUtil.doubleToString(jsonData, "storePrice");
                    String priceArray[] = StringUtil.splitDouble(storePrice);
                    String priceInt = "";
                    String price = "";
                    if (priceArray.length == 2) {
                        priceInt = priceArray[0] + ".";
                        price = priceArray[1];
                    }
                    String goodsContent = jsonData.getString("goodsContent");
                    String buyNoticeImg = StringUtil.toString(jsonData.getString("buyNoticeImg"));

                    context.updateData(goodsName, skuImageUrl, profitInt, profit, priceInt, price, goodsContent, goodsTax
                            , skuMarketInventory, storePriceChange, countryName, depotName, goodsType, weight, stats,buyNoticeImg,taxFree,expFree);
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void onerrSuccess() {

            }

            @Override
            public void onCatch() {

            }

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

}
