package com.danding.yang800.Presenter;

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

import com.danding.yang800.Adapter.MyBaseAdapter;
import com.danding.yang800.Entity.OrderInfo;
import com.danding.yang800.Entity.WaitProfitInfo;
import com.danding.yang800.General.HttpCallBack;
import com.danding.yang800.OrderManageActivity;
import com.danding.yang800.Util.StringUtil;
import com.danding.yang800.Util.VolleyUtil;
import com.danding.yang800.WaitProfitActivity;

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

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

public class WaitProfitActivityPresenter {
    WaitProfitActivity context;
    SharedPreferences sp;
    MyBaseAdapter adapter;

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

    public void getOrderInfo(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", "8");
//        new VolleyUtil(context).request(context,"http://" + BuildConfig.HOSTURL + "/xhr/order/list",true, map, new HttpCallBack() {
        new VolleyUtil(context).request(context, "finance.storeIncome.getWaitingSettlementPackage", true, map, new HttpCallBack() {
            @Override
            public void onSuccess(JSONObject json) {
                try {
                    if (!load) {
                        if (adapter != null) {
                            adapter.clear();
                        }
                    }
                    JSONObject jsondata = json.getJSONObject("result");
                    int totalCount = jsondata.getInt("totalCount");
                    JSONArray jjArray = jsondata
                            .getJSONArray("packageList");
                    for (int j = 0; j < jjArray.length(); j++) {
                        JSONObject jsonOrder = jjArray.getJSONObject(j);
                        String orderSn = jsonOrder.getString("orderSn");
                        String packageSn = jsonOrder.getString("orderPackageSn");
                        String statusDesc = jsonOrder.getString("statusDesc");
                        String createTime = jsonOrder.getString("orderCreateTime");
                        String status = jsonOrder.getString("status");
                        String amount = StringUtil.doubleToString(jsonOrder,"amount");
                        String totalTax = StringUtil.doubleToString(jsonOrder,"totalTax");
                        String totalExpPrice = StringUtil.doubleToString(jsonOrder,"totalExpPrice");
                        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()];
                            String income[] = 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") + "";
                                income[k] = StringUtil.doubleToString(itemjson,"income");
                                shopPrice[k] = StringUtil.doubleToString(itemjson,"unitPrice");

                            }
                            adapter.addData(new WaitProfitInfo(orderSn, packageSn, statusDesc, status,
                                    amount, totalTax, totalExpPrice, createTime, url, orderContent, shopPrice, shopNumber,income));
                        }
                    }
                    context.updateAdapter(totalCount);
                } catch (Exception e) {
                    e.printStackTrace();
                    onCatch();
                }

            }


            @Override
            public void onerrSuccess() {
            }

            @Override
            public void onCatch() {
            }

            @Override
            public void onFail() {
            }
        });
    }


}
