package com.danding.yang800.Presenter;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Handler;
import android.os.Message;

import com.danding.yang800.Adapter.SelectGoodsAdapter;
import com.danding.yang800.Entity.GoodsSortInfo;
import com.danding.yang800.Entity.SortListInfo;
import com.danding.yang800.General.HttpCallBack;
import com.danding.yang800.SelectGoodsActivity;
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.util.ArrayList;
import java.util.HashMap;

/**
 * Created by Administrator on 2017/1/12.
 */
public class SelectGoodsActivityPresenter {
    private SelectGoodsActivity context;
    SharedPreferences sp;
    SelectGoodsAdapter goodsadapter;
    ArrayList<GoodsSortInfo> infos = new ArrayList<GoodsSortInfo>();

    public SelectGoodsActivityPresenter(SelectGoodsActivity context, SelectGoodsAdapter goodsadapter
    ) {
        sp = context.getSharedPreferences("variable", Context.MODE_PRIVATE);
        this.context = context;
        this.goodsadapter = goodsadapter;
    }
/*
* 获得商品种类
* */

    public void getcategory() {

        HashMap<String, String> map = new HashMap<String, String>();
//        new VolleyUtil(context).request(context, "http://" + BuildConfig.HOSTURL + "/xhr/goods/rocking/category", true, map, new HttpCallBack() {
        new VolleyUtil(context).request(context, "goods.rocking.category", true, map, new HttpCallBack() {
            @Override
            public void onSuccess(JSONObject json) {
                LogUtil.d(json.toString());
                try {
                    JSONArray jjArray = json
                            .getJSONArray("result");
                    for (int i = 0; i < jjArray.length(); i++) {
                        JSONObject jsondata = jjArray.getJSONObject(i);
                        String name = jsondata.getString("name");
                        int id = jsondata.getInt("id");
                        GoodsSortInfo goodsSortInfo;
                        if (i == 0) {
                            goodsSortInfo = new GoodsSortInfo(9999, "热卖单品", true);
                            infos.add(goodsSortInfo);
                            goodsSortInfo = new GoodsSortInfo(0, "全部", false);
                            infos.add(goodsSortInfo);
                            goodsSortInfo = new GoodsSortInfo(id, name, false);
                        } else {
                            goodsSortInfo = new GoodsSortInfo(id, name, false);
                        }
                        infos.add(goodsSortInfo);
                    }
                    context.upadateAdapter(infos);
                } catch (Exception e) {
                    e.printStackTrace();
                    onCatch();
                }

            }


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

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

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

    JSONObject json;

    /**
     * 获得热卖单品
     */
    public void gethotSale(final String storeId, final String name, final String page, final String sort, final boolean load) {
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("storeId", storeId);
        map.put("goodsName", name);
        map.put("sort", sort);
        map.put("pageIndex", page);
        map.put("limit", "8");
        LogUtil.d(page);
//        new VolleyUtil(context).request(context, "http://" + BuildConfig.HOSTURL + "/xhr/goods/rocking/hotSale", true, map, new HttpCallBack() {
        new VolleyUtil(context).request(context, "goods.rocking.hotSale", true, map, new HttpCallBack() {
            @Override
            public void onSuccess(final JSONObject json) {
                LogUtil.d(json.toString());
                try {
                    if (!load) {
                        if (goodsadapter != null) {
                            goodsadapter.clear();
                        }
                    }
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            try {
                                JSONObject jsonData = json.getJSONObject("result");
                                int totalCount = jsonData.getInt("pageTotal");
                                JSONArray jjArray = jsonData
                                        .getJSONArray("list");
                                String ifUp = "";
                                for (int i = 0; i < jjArray.length(); i++) {
                                    JSONObject jsondata = jjArray.getJSONObject(i);
                                    String goodsName = jsondata.getString("goodsName");
                                    String skuImageUrl = jsondata.getString("skuImageUrl");
                                    String goodsSubtitle = StringUtil.toString(jsondata.getString("subTitle"));
                                    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 homeShow = jsondata.getBoolean("storeHomeShow");
                                    String goodsSn = jsondata.getString("goodsSn");
                                    String skuSn = jsondata.getString("skuSn");
                                    String storeSkuSn = jsondata.getString("storeSkuSn");
                                    boolean taxFree ;
                                    boolean expFree ;
                                    try {
                                        taxFree=jsondata.getBoolean("taxFree");
                                        expFree= jsondata.getBoolean("expFree");
                                    }catch (Exception e){
                                        taxFree=false;
                                        expFree=false;
                                    }
                                    jsondata.getString("expFree");
                                    int sellStatus = jsondata.getInt("sellStatus");
                                    String stats = "1";
                                    if (sellStatus == 1 || skuMarketInventory <= 0) {
                                        stats = "3";
                                    }
                                    if (storeRocking) {
                                        ifUp = "已上架";
                                    } else {
                                        ifUp = "上架";
                                    }
                                    goodsadapter.addData(new SortListInfo(skuImageUrl, goodsName, ifUp, homeShow,
                                            skuMarketPrice, skuSupplyPrice, skuProfit, skuMarketInventory + "", storePriceChange
                                            , goodsSn, skuSn, storeSkuSn, stats, goodsSubtitle,taxFree,expFree));
                                }
                                Message message = new Message();
                                message.what = totalCount;
                                handler.sendMessage(message);

                            } catch (Exception e) {

                            }
                        }
                    }).start();

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

                }
            }


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

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

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

    Handler handler = new Handler() { // handle
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            context.upadateAdapter(msg.what);
        }
    };

    /*
    * 获得非热卖单品对应种类商品
    * */
    public void getSortShopInfo(final String storeId, final String name, final String category, final String page, final String sort
            , final boolean load) {
        LogUtil.d(sort);
        HashMap<String, String> map = new HashMap<>();
        map.put("storeId", storeId);
        map.put("goodsName", name);
        map.put("category", category);
        map.put("sort", sort);
        map.put("pageIndex", page);
        map.put("limit", "8");
        LogUtil.d(storeId + "goodsName");

//        new VolleyUtil(context).request(context, "http://" + BuildConfig.HOSTURL + "/xhr/goods/rocking/general", true, map, new HttpCallBack() {
        new VolleyUtil(context).request(context, "goods.rocking.general", true, map, new HttpCallBack() {
            @Override
            public void onSuccess(JSONObject json) {
                LogUtil.d(json.toString());
                try {
                    if (!load) {
                        if (goodsadapter != null) {
                            goodsadapter.clear();
                        }
                    }
                    JSONObject jsonData = json.getJSONObject("result");
                    int totalCount = jsonData.getInt("pageTotal");
                    JSONArray jjArray = jsonData
                            .getJSONArray("list");
                    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");
                        String skuSupplyPrice = StringUtil.doubleToString(jsondata, "skuSupplyPrice");
                        String skuProfit = StringUtil.doubleToString(jsondata, "storeProfit");
                        String goodsSubtitle = StringUtil.toString(jsondata.getString("subTitle"));
                        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 skuSn = jsondata.getString("skuSn");
                        String storeSkuSn = jsondata.getString("storeSkuSn");
                        boolean taxFree ;
                        boolean expFree ;
                        try {
                            taxFree=jsondata.getBoolean("taxFree");
                            expFree= jsondata.getBoolean("expFree");
                        }catch (Exception e){
                            taxFree=false;
                            expFree=false;
                        }
                        int sellStatus = jsondata.getInt("sellStatus");
                        String stats = "1";
                        if (sellStatus == 1 || skuMarketInventory <= 0) {
                            stats = "3";
                        }
                        if (storeRocking) {
                            ifUp = "已上架";
                        } else {
                            ifUp = "上架";
                        }
                        goodsadapter.addData(new SortListInfo(skuImageUrl, goodsName, ifUp, storeHomeShow,
                                skuMarketPrice, skuSupplyPrice, skuProfit, skuMarketInventory + "", storePriceChange
                                , goodsSn, skuSn, storeSkuSn, stats, goodsSubtitle,taxFree,expFree));
                    }

                    context.upadateAdapter(totalCount);
                } catch (Exception e) {
                    e.printStackTrace();
                    onCatch();
                }
            }


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

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

            @Override
            public void onFail() {
                LogUtil.d("fail");
                context.errData();
                context.measureHeight();
                context.onLoad();
                context.dismissDialog();
            }
        });
    }


    /**
     * 一键上架热卖单品
     */
    public void rockingByHotSale(final String storeId, String goodsName) {
        HashMap<String, String> map = new HashMap<>();
        map.put("storeId", storeId);
        map.put("goodsName", goodsName);
//        new VolleyUtil(context).request(context, "http://" + BuildConfig.HOSTURL + "/xhr/goods/rocking/rockingByHotSale", true, map, new HttpCallBack() {
        new VolleyUtil(context).request(context, "goods.rocking.rockingByHotSale", true, map, new HttpCallBack() {
            @Override
            public void onSuccess(JSONObject json) {
                try {
                    for (SortListInfo info :
                            goodsadapter.getLists()) {
                        info.setIfup("已上架");
                    }
                    goodsadapter.notifyDataSetChanged();
                    context.measureHeight();
                    context.onLoad();
                    context.dismissDialog();
                    ToastUtil.show(context, "一键上架成功");
                    //  context.upadateAdapter(totalCount);

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

                }
            }


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

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

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

    /**
     * 一件上架非热卖单品
     */
    public void rockingByCategory(final String storeId, final String category, String goodsName) {
        HashMap<String, String> map = new HashMap<>();
        map.put("storeId", storeId);
        map.put("category", category);
        map.put("goodsName", goodsName);
//        new VolleyUtil(context).request(context, "http://" + BuildConfig.HOSTURL + "/xhr/goods/rocking/rockingByCategory", true, map, new HttpCallBack() {
        new VolleyUtil(context).request(context, "goods.rocking.rockingByCategory", true, map, new HttpCallBack() {
            @Override
            public void onSuccess(JSONObject json) {
                try {
                    for (SortListInfo info :
                            goodsadapter.getLists()) {
                        info.setIfup("已上架");
                    }
                    goodsadapter.notifyDataSetChanged();
                    context.measureHeight();
                    context.onLoad();
                    context.dismissDialog();
                    ToastUtil.show(context, "一键上架成功");
                    //  context.upadateAdapter(totalCount);

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

                }
            }


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

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

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

    /**
     * 下架商品
     */
    public void rockingDown(final String storeId, final int position, final String goodsSn) {
        HashMap<String, String> map = new HashMap<>();
        map.put("storeId", storeId);
        map.put("goodsSn", goodsSn);
//        new VolleyUtil(context).request(context, "http://" + BuildConfig.HOSTURL + "/xhr/goods/rocking/rockingDown", true, map, new HttpCallBack() {
        new VolleyUtil(context).request(context, "goods.rocking.rockingDown", true, map, new HttpCallBack() {
            @Override
            public void onSuccess(JSONObject json) {
                LogUtil.d(sp.getString("Cookie", "") + "下架");
                try {
                    context.setifUp(position, "上架");
                    context.measureHeight();
                    context.onLoad();
                    //  context.upadateAdapter(totalCount);

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


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

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

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

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

    public void rockingByOne(final String storeId, final int position, final String goodsSn, final String skuSn, final String storePrice) {
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("storeId", storeId);
        map.put("goodsSn", goodsSn);
        map.put("skuSn", skuSn);
        map.put("storePrice", storePrice);
//        new VolleyUtil(context).request(context, "http://" + BuildConfig.HOSTURL + "/xhr/goods/rocking/rockingByOne", true, map, new HttpCallBack() {
        new VolleyUtil(context).request(context, "goods.rocking.rockingByOne", true, map, new HttpCallBack() {
            @Override
            public void onSuccess(JSONObject json) {
                LogUtil.d(sp.getString("Cookie", "") + "上架");
                try {
                    context.setifUp(position, "已上架");
                    context.measureHeight();
                    context.onLoad();
                    context.dismissDialog();
                    //  context.upadateAdapter(totalCount);

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


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

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

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

    /*
      * 商品首页显示
      * */
    public void homeShow(final String storeId, final int position, final String goodsSn, final String status) {
        context.initDialog();
        HashMap<String, String> map = new HashMap<>();
        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.measureHeight();
                    context.onLoad();
                    context.dismissDialog();
                    //  context.upadateAdapter(totalCount);

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


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

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

            @Override
            public void onFail() {
                context.measureHeight();
                context.onLoad();
                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, "http://" + BuildConfig.HOSTURL + "/xhr/goods/changePrice/getGoodsInfo", true, map, new HttpCallBack() {
        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 storeSkuSn = StringUtil.toString(jsondata.getString("storeSkuSn"));
                    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");
                    int sellStatus = jsondata.getInt("sellStatus");
                    String stats = "1";
                    if (sellStatus == 1 || skuMarketInventory <= 0) {
                        stats = "3";
                    }
                    if (storeRocking) {
                        ifUp = "已上架";
                    } else {
                        ifUp = "上架";
                    }
                    SortListInfo info = goodsadapter.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.setStoreSkuSn(storeSkuSn);
                    info.setStats(stats);
                    goodsadapter.notifyDataSetChanged();
                    // context.upadateAdapter(info, position);
                    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 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) {
                LogUtil.d(json.toString());
                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.onLoad();
            }
        });
    }

    public void getStoreName(final String storeId) {
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("storeId", storeId);
//        new VolleyUtil(context).request(context, "http://" + BuildConfig.HOSTURL + "/xhr/store/info", true, map, new HttpCallBack() {
        new VolleyUtil(context).request(context, "store.info", true, map, new HttpCallBack() {
            @Override
            public void onSuccess(JSONObject json) {
                try {
                    JSONObject jsonData = json.getJSONObject("result");
                    String name = jsonData.getString("name");
                    if (StringUtil.getString(name)) {
                        context.storeName = name;
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }


            @Override
            public void onerrSuccess() {

            }

            @Override
            public void onCatch() {

            }

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