package com.danding.yang800.Presenter;

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

import com.danding.yang800.Adapter.MyBaseAdapter;
import com.danding.yang800.BuildConfig;
import com.danding.yang800.Entity.OrderInfo;
import com.danding.yang800.General.HttpCallBack;
import com.danding.yang800.OrderManageActivity;
import com.danding.yang800.Util.VolleyUtil;

import org.json.JSONArray;
import org.json.JSONObject;

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

public class OrderActivityPresenter {
    OrderManageActivity context;
    SharedPreferences sp;
    MyBaseAdapter adapter;

    public OrderActivityPresenter(OrderManageActivity context, MyBaseAdapter adapter) {
        this.context = context;
        this.adapter = adapter;
        sp = context.getSharedPreferences("variable", Context.MODE_PRIVATE);
    }

    public void getOrderInfo(final String status, final String storeId, final String page, final boolean load) {
        HashMap<String, String> map = new HashMap<>();
        map.put("storeId", storeId);
        map.put("pageIndex", page);
        map.put("limit", "6");
        map.put("status", status);
//        new VolleyUtil(context).request(context,"http://" + BuildConfig.HOSTURL + "/xhr/order/list",true, map, new HttpCallBack() {
        new VolleyUtil(context).request(context, "order.list", true, map, new HttpCallBack() {
            @Override
            public void onSuccess(JSONObject json) {
                try {
                    if (!load) {
                        if (adapter != null) {
                            adapter.clear();
                        }
                    }
                    JSONObject jsondata = json.getJSONObject("result");
                    int pageTotal = jsondata.getInt("pageTotal");
                    JSONArray jjArray = jsondata
                            .getJSONArray("orderListVOs");
                    for (int j = 0; j < jjArray.length(); j++) {
                        JSONObject jsonOrder = jjArray.getJSONObject(j);
                        String orderSn = jsonOrder.getString("orderSn");
                        String packageSn = jsonOrder.getString("packageSn");
                        String statusDesc = jsonOrder.getString("statusDesc");
                        String createTime = jsonOrder.getString("createTime");
                        boolean showExpress = false;
                        try {
                            showExpress = jsonOrder.getBoolean("showExpress");
                        } catch (Exception e) {
                        }

                        String status = jsonOrder.getString("status");
                        Double amountDouble = jsonOrder.getDouble("amount");
                        Double totalTaxDouble = jsonOrder.getDouble("totalTax");
                        Double totalExpPriceDouble = jsonOrder.getDouble("totalExpPrice");
                        String amount = new DecimalFormat("0.00").format(amountDouble);
                        String totalTax = new DecimalFormat("0.00").format(totalTaxDouble);
                        String totalExpPrice = new DecimalFormat("0.00").format(totalExpPriceDouble);
                        JSONArray itemListArray = jsonOrder
                                .getJSONArray("itemList");
                        if (itemListArray.length() > 0) {
                            String[] url = new String[itemListArray.length()];
                            String[] orderContent = new String[itemListArray.length()];
                            String[] shopPrice = new String[itemListArray.length()];
                            String shopNumber[] = new String[itemListArray.length()];
                            for (int k = 0; k < itemListArray.length(); k++) {
                                JSONObject itemjson = itemListArray.getJSONObject(k);
                                orderContent[k] = itemjson.getString("itemName");
                                url[k] = itemjson.getString("imgUrl");
                                shopNumber[k] = itemjson.getInt("quantity") + "";
                                shopPrice[k] = new DecimalFormat("0.00").format(itemjson.getDouble("unitPrice"));
                            }
                            adapter.addData(new OrderInfo(orderSn, packageSn, statusDesc, status,
                                    amount, totalTax, totalExpPrice, createTime, url, orderContent, shopPrice, shopNumber, showExpress));
                        }
                    }
                    context.updateAdapter(pageTotal);
                    context.dismissDialog();
                } catch (Exception e) {
                    e.printStackTrace();
                    onCatch();
                }

            }


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

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

            @Override
            public void onFail() {
//                if (!load) {
//                    if (adapter != null) {
//                        adapter.clear();
//                    }
//                }
//                String[] url;
//                String[] orderContent;
//                String[] shopPrice;
//                String shopNumber[];
//                for (int i = 0; i < 6; i++) {
//                    if (i == 0) {
//                        url = new String[3];
//                        orderContent = new String[3];
//                        shopPrice = new String[3];
//                        shopNumber = new String[3];
//                        for (int k = 0; k < 3; k++) {
//                            orderContent[k] = k + "12";
//                            if (k == 0) {
//                                url[k] = "https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=3728071035,3644764679&fm=27&gp=0.jpg";
//                            } else if (k == 1) {
//                                url[k] = "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1507632054248&di=7ede431966eae4e9ebc69430950d9c89&imgtype=0&src=http%3A%2F%2Fpic20.nipic.com%2F20120418%2F3215476_123836974000_2.jpg";
//                            } else {
//                                url[k] = "https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=3602749526,2280059118&fm=27&gp=0.jpg";
//                            }
//                            shopNumber[k] = "";
//                            shopPrice[k] = "";
//                        }
//                    } else if (i == 1) {
//                        url = new String[1];
//                        orderContent = new String[1];
//                        shopPrice = new String[1];
//                        shopNumber = new String[1];
//                        for (int k = 0; k < 1; k++) {
//                            orderContent[k] = k + "12";
//                            if (k == 0) {
//                                url[k] = "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1507633428331&di=db47f1f99b21d63b5281cf3ca46ad4ff&imgtype=0&src=http%3A%2F%2Fy0.ifengimg.com%2Fa341863afb4b1373%2F2014%2F0909%2Frdn_540eb362661fd.jpg";
//                                shopNumber[k] = "";
//                                shopPrice[k] = "";
//                            }
//                        }
//                    } else if (i == 2) {
//                        url = new String[2];
//                        orderContent = new String[2];
//                        shopPrice = new String[2];
//                        shopNumber = new String[2];
//                        for (int k = 0; k < 2; k++) {
//                            orderContent[k] = k + "12";
//                            if (k == 0) {
//                                url[k] = "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1507633434176&di=fbc7d6a55025482f99c7df4081e90d57&imgtype=0&src=http%3A%2F%2Fimg.taopic.com%2Fuploads%2Fallimg%2F140708%2F240394-140FPA34547.jpg";
//                                shopNumber[k] = "";
//                                shopPrice[k] = "";
//                            } else {
//                                url[k] = "https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=3728071035,3644764679&fm=27&gp=0.jpg";
//                            }
//                        }
//                    } else if (i == 1) {
//                        url = new String[1];
//                        orderContent = new String[1];
//                        shopPrice = new String[1];
//                        shopNumber = new String[1];
//                        for (int k = 0; k < 1; k++) {
//                            orderContent[k] = k + "12";
//                            if (k == 0) {
//                                url[k] = "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1507633428331&di=db47f1f99b21d63b5281cf3ca46ad4ff&imgtype=0&src=http%3A%2F%2Fy0.ifengimg.com%2Fa341863afb4b1373%2F2014%2F0909%2Frdn_540eb362661fd.jpg";
//                                shopNumber[k] = "";
//                                shopPrice[k] = "";
//                            }
//                        }
//                    } else if (i == 3) {
//                        url = new String[2];
//                        orderContent = new String[2];
//                        shopPrice = new String[2];
//                        shopNumber = new String[2];
//                        for (int k = 0; k < 2; k++) {
//                            orderContent[k] = k + "12";
//                            if (k == 0) {
//                                url[k] = "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1507633434176&di=6f195ed50681f976fa2a9008fe517389&imgtype=0&src=http%3A%2F%2Fb.hiphotos.baidu.com%2Flvpics%2Fh%3D800%2Fsign%3D989cc5343bdbb6fd3a5be8263925aba6%2F8718367adab44aede735cf2bb11c8701a08bfb82.jpg";
//                                shopNumber[k] = "";
//                                shopPrice[k] = "";
//                            } else {
//                                url[k] = "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1507633434176&di=6648adb0186ee0337ec6fe6393ce47ab&imgtype=0&src=http%3A%2F%2Ff.hiphotos.baidu.com%2Flvpics%2Fw%3D1000%2Fsign%3Deadbacd3a1ec08fa260017a769de3c6d%2F14ce36d3d539b600272b07dbe850352ac75cb7f0.jpg";
//                            }
//                        }
//                    } else if (i == 4) {
//                        url = new String[1];
//                        orderContent = new String[1];
//                        shopPrice = new String[1];
//                        shopNumber = new String[1];
//                        for (int k = 0; k < 1; k++) {
//                            orderContent[k] = k + "12";
//                            if (k == 0) {
//                                url[k] = "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1507633434176&di=926c20fa761ea4a3745f20190ddce311&imgtype=0&src=http%3A%2F%2Fscimg.jb51.net%2Fallimg%2F130617%2F2-13061GA049547.jpg";
//                                shopNumber[k] = "";
//                                shopPrice[k] = "";
//                            } else {
//                                url[k] = "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1507633434176&di=6648adb0186ee0337ec6fe6393ce47ab&imgtype=0&src=http%3A%2F%2Ff.hiphotos.baidu.com%2Flvpics%2Fw%3D1000%2Fsign%3Deadbacd3a1ec08fa260017a769de3c6d%2F14ce36d3d539b600272b07dbe850352ac75cb7f0.jpg";
//                            }
//                        }
//                    } else if (i == 5) {
//                        url = new String[3];
//                        orderContent = new String[3];
//                        shopPrice = new String[3];
//                        shopNumber = new String[3];
//                        for (int k = 0; k < 1; k++) {
//                            orderContent[k] = k + "12";
//                            if (k == 0) {
//                                url[k] = "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1507633434176&di=ce17a7373ceccab299f83c45dd77df86&imgtype=0&src=http%3A%2F%2Fimgsrc.baidu.com%2Fimgad%2Fpic%2Fitem%2F472309f790529822fcb3aa5eddca7bcb0b46d4af.jpg";
//                                shopNumber[k] = "";
//                                shopPrice[k] = "";
//                            } else {
//                                url[k] = "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1507633434175&di=a51d0454cd75c89a3c02c97f7cd4e098&imgtype=0&src=http%3A%2F%2Fimgsrc.baidu.com%2Fimgad%2Fpic%2Fitem%2F21a4462309f790520f85e90707f3d7ca7bcbd575.jpg";
//                            }
//                        }
//                    } else {
//                        url = new String[3];
//                        orderContent = new String[3];
//                        shopPrice = new String[3];
//                        shopNumber = new String[3];
//                        for (int k = 0; k < 3; k++) {
//                            orderContent[k] = k + "12";
//                            if (k == 0) {
//                                url[k] = "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1507632054248&di=7ede431966eae4e9ebc69430950d9c89&imgtype=0&src=http%3A%2F%2Fpic20.nipic.com%2F20120418%2F3215476_123836974000_2.jpg";
//                            } else if (k == 1) {
//                                url[k] = "https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=3602749526,2280059118&fm=27&gp=0.jpg";
//                            } else {
//                                url[k] = "https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=3728071035,3644764679&fm=27&gp=0.jpg";
//                            }
//                            shopNumber[k] = "";
//                            shopPrice[k] = "";
//                        }
//                    }
//                    adapter.addData(new OrderInfo("", "", "", status,
//                            "", "", "", "", url, orderContent, shopPrice, shopNumber));
//                }
//                context.updateAdapter(6);
                context.dismissDialog();
            }
        });
    }


}
