package com.danding.yang800.Presenter;

import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;

import com.danding.yang800.Adapter.MyShopAdapter;
import com.danding.yang800.BuildConfig;
import com.danding.yang800.Entity.SortListInfo;
import com.danding.yang800.General.HttpCallBack;
import com.danding.yang800.General.Yang800App;
import com.danding.yang800.MyShopActivity;
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.JSONArray;
import org.json.JSONObject;

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

/**
 * Created by Administrator on 2017/1/12.
 */
public class MyshopActivityPresenter {
    private MyShopActivity context;
    SharedPreferences sp;
    MyShopAdapter adapter;

    public MyshopActivityPresenter(MyShopActivity context, MyShopAdapter adapter) {
        sp = context.getSharedPreferences("variable", Context.MODE_PRIVATE);
        this.context = context;
        this.adapter = adapter;
    }

    String name;
    String des;
    String avatar;
    String img;

    public void getSortShopInfo(final String storeId, final String userId, final String page, final boolean design, final boolean load) {
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("storeId", storeId);
        map.put("userId", userId);
        map.put("pageIndex", page);
        map.put("limit", "8");
        new VolleyUtil(context).request(context, "store.index.getStoreInfo", true, map, new HttpCallBack() {
            @Override
            public void onSuccess(JSONObject json) {
                LogUtil.d(json.toString());
                try {
                    JSONObject jsonOData = json.getJSONObject("result");
                    int totalCount = jsonOData.getInt("goodsTotal");

                    if (page.equals("1")) {
                        JSONObject jsonObject = jsonOData.getJSONObject("storeIo");
                        name = jsonObject.getString("name");
                        des = jsonObject.getString("des");
                        avatar = jsonObject.getString("avatar");
                        img = jsonObject.getString("img");
                    }
                    if (design) {
                        context.upadateShop(name, des, avatar, img);
                        return;
                    }
                    if (!load) {
                        if (adapter != null) {
                            adapter.clear();
                        }
                    }
                    JSONArray jjArray = jsonOData
                            .getJSONArray("goodsIo");
                    String ifUp = "";
                    for (int i = 0; i < jjArray.length(); i++) {
                        JSONObject jsondata = jjArray.getJSONObject(i);
                        String goodsName = jsondata.getString("goodsName");
                        String skuMarketPrice = StringUtil.doubleToString(jsondata, "storePrice");
//                        double skuSupplyPriceDouble = jsondata.getDouble("skuSupplyPrice");
                        String goodsSubtitle = StringUtil.toString(jsondata.getString("subTitle"));
                        String skuSupplyPrice = StringUtil.doubleToString(jsondata, "skuSupplyPrice");
                        String skuProfit = StringUtil.doubleToString(jsondata, "storeProfit");
                        int skuMarketInventory = jsondata.getInt("skuMarketInventory");
                        String skuImageUrl = jsondata.getString("skuImageUrl");
                        boolean storeRocking = jsondata.getBoolean("storeRocking");
                        boolean storePriceChange = jsondata.getBoolean("storePriceChange");
                        boolean storeHomeShow = jsondata.getBoolean("storeHomeShow");
                        String goodsSn = jsondata.getString("goodsSn");
                        String storeSkuSn = jsondata.getString("storeSkuSn");
                        String skuSn = jsondata.getString("skuSn");
                        int sellStatus = jsondata.getInt("sellStatus");
                        String stats = "1";
                        boolean taxFree ;
                        boolean expFree ;
                        try {
                            taxFree=jsondata.getBoolean("taxFree");
                            expFree= jsondata.getBoolean("expFree");
                        }catch (Exception e){
                            taxFree=false;
                            expFree=false;
                        }
                        if (sellStatus == 1 || skuMarketInventory <= 0) {
                            stats = "3";
                        }
                        if (storeRocking) {
                            ifUp = "已上架";
                        } else {
                            ifUp = "上架";
                        }
                        adapter.addData(new SortListInfo(skuImageUrl, goodsName, ifUp, storeHomeShow,
                                skuMarketPrice, skuSupplyPrice, skuProfit, skuMarketInventory + "", storePriceChange
                                , goodsSn, skuSn, storeSkuSn, stats, goodsSubtitle,taxFree,expFree));
                    }
                    if (page.equals("1")) {
                        context.upadateAdapter(totalCount, name, des, avatar, img, false);
                    } else {
                        context.upadateAdapter(totalCount, name, des, avatar, img, true);
                    }


                } catch (Exception e) {
                    e.printStackTrace();
                    onFail();
                }


            }

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

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

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

    public void getGoodsInfo(final String storeId, final String goodsSn, final String skuSn, final int position) {
        context.initDialog();
        HashMap<String, String> map = new HashMap<String, String>();
        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 {
                    String ifUp;
                    JSONObject jsondata = json.getJSONObject("result");
                    String goodsName = jsondata.getString("goodsName");
                    String skuMarketPrice = StringUtil.doubleToString(jsondata, "storePrice");
                    String skuSupplyPrice = StringUtil.doubleToString(jsondata, "skuSupplyPrice");
                    String skuProfit = StringUtil.doubleToString(jsondata, "storeProfit");
                    int skuMarketInventory = jsondata.getInt("skuMarketInventory");
                    boolean storeRocking = jsondata.getBoolean("storeRocking");
                    boolean storePriceChange = jsondata.getBoolean("storePriceChange");
                    boolean storeHomeShow = jsondata.getBoolean("storeHomeShow");
                    String goodsSn = jsondata.getString("goodsSn");
                    if (storeRocking) {
                        ifUp = "已上架";
                    } else {
                        ifUp = "上架";
                    }
                    SortListInfo info = adapter.getLists().get(position);
                    info.setName(goodsName);
                    info.setIfup(ifUp);
                    info.setShouyexian(storeHomeShow);
                    info.setSkuMarketPrice(skuMarketPrice);
                    info.setSkuSupplyPrice(skuSupplyPrice);
                    info.setSkuProfit(skuProfit);
                    info.setSkuMarketInventory(skuMarketInventory + "");
                    info.setStorePriceChange(storePriceChange);
                    info.setGoodsSn(goodsSn);
//                        info.setSkuSn(skuSn);
//                        info.setStoreSkuSn(storeSkuSn);
//                        info.setStats(stats);
                    adapter.notifyDataSetChanged();
                    context.dismissDialog();

                } catch (Exception e) {
                    e.printStackTrace();
                    onCatch();
                    //context.dismissDialog();
                }


            }


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

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

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

    /**
     * 下架商品
     */
    public void rockingDown(final String storeId, final int position, final String goodsSn) {
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("storeId", storeId);
        map.put("goodsSn", goodsSn);
        new VolleyUtil(context).request(context, "goods.rocking.rockingDown", true, map, new HttpCallBack() {
            @Override
            public void onSuccess(JSONObject json) {
                LogUtil.d(json.toString());
                try {
                    context.setifUp(position, "上架");
                    context.onLoad();
                    context.dismissDialog();
                    //  context.upadateAdapter(totalCount);
                } catch (Exception e) {
                    e.printStackTrace();
                    onFail();
                }

            }


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

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

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

    /**
     * 上架单个商品
     */

    public void rockingByOne(final String storeId, final int position, final String goodsSn, final String skuSn) {
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("storeId", storeId);
        map.put("goodsSn", goodsSn);
        map.put("skuSn", skuSn);
        new VolleyUtil(context).request(context, "goods.rocking.rockingByOne", true, map, new HttpCallBack() {
            @Override
            public void onSuccess(JSONObject json) {
                try {
                    context.setifUp(position, "已上架");
                    context.onLoad();
                    context.dismissDialog();
                    //  context.upadateAdapter(totalCount);

                } catch (Exception e) {
                    e.printStackTrace();
                    onCatch();

                }

            }

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

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

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

    /*
      * 商品首页显示
      * */
    public void homeShow(final String storeId, final int position, final String goodsSn, final String status) {
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("storeId", storeId);
        map.put("goodsSn", goodsSn);
        map.put("status", status);
//        new VolleyUtil(context).request(context, "http://" + BuildConfig.HOSTURL + "/xhr/goods/rocking/updateHomeShow", true,map, new HttpCallBack() {
        new VolleyUtil(context).request(context, "goods.rocking.updateHomeShow", true, map, new HttpCallBack() {
            @Override
            public void onSuccess(JSONObject json) {
                LogUtil.d(json.toString());
                try {
                    if (status.equals("1")) {
                        context.setShouye(position, true);
                    } else {
                        context.setShouye(position, false);
                    }

                    context.onLoad();
                    context.dismissDialog();

                } catch (Exception e) {
                    e.printStackTrace();
                    onCatch();
                }

            }


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

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

            @Override
            public void onFail() {

                LogUtil.d("fail");
                context.dismissDialog();
            }
        });
    }

    public void getShareUrl() {
        HashMap<String, String> map = new HashMap<String, String>();
//        new VolleyUtil(context).request(context, "http://" + BuildConfig.HOSTURL + "/xhr/store/getShopConfig", true, map, new HttpCallBack() {
        new VolleyUtil(context).request(context, "store.getShopConfig", true, map, new HttpCallBack() {
            @Override
            public void onSuccess(JSONObject json) {
                try {
                    JSONObject jsonData = json.getJSONObject("result");
                    String goodsDetailUrl = jsonData.getString("goodsDetailUrl");
                    String homeIndexUrl = jsonData.getString("homeIndexUrl");
                    if (!StringUtil.getString(context.storeUrl) || !StringUtil.getString(context.goodsUrl)) {
                        context.storeUrl = homeIndexUrl;
                        context.goodsUrl = goodsDetailUrl;
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }


            @Override
            public void onerrSuccess() {

            }

            @Override
            public void onCatch() {

            }

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

}
