package com.danding.yang800.Presenter;

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

import com.danding.yang800.Adapter.DetailsAdapter;
import com.danding.yang800.BuildConfig;
import com.danding.yang800.Entity.DetailsInfo;
import com.danding.yang800.General.HttpCallBack;
import com.danding.yang800.General.Yang800App;
import com.danding.yang800.OrderDetailActivity;
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 com.danding.yang800.Vo.MarketPlatformVO;
import com.danding.yang800.Vo.OrderDetailVO;
import com.google.gson.Gson;

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

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

/**
 * Created by Administrator on 2017/1/12.
 */
public class OrderDetailActivityPresenter {
    private OrderDetailActivity context;
    SharedPreferences sp;
    DetailsAdapter adapter;

    public OrderDetailActivityPresenter(OrderDetailActivity context, DetailsAdapter adapter) {
        sp = context.getSharedPreferences("variable", Context.MODE_PRIVATE);
        this.context = context;
        this.adapter = adapter;
    }


    public void getInfo(final String orderSn, final String packageSn, final String status) {
        context.initDialog();
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("orderSn", orderSn);
        map.put("packageSn", packageSn);
        map.put("status", status);
//        new VolleyUtil(context).request(context, "http://" + BuildConfig.HOSTURL + "/xhr/order/detail", true,map, new HttpCallBack() {
        new VolleyUtil(context).request(context, "order.detail", true, map, new HttpCallBack() {
            @Override
            public void onSuccess(JSONObject json) {
                LogUtil.d(json.toString());
                try {
//                    Gson gson = new Gson();
//                    OrderDetailVO bean = gson.fromJson(json.toString(), OrderDetailVO.class);
//                    OrderDetailVO.ResultBean result = bean.getResult();
//                    String statusDesc=result.getStatusDesc();
//                    int safterSaleStatusCode=result.getSafterSaleStatusCode();
//                    int showExpDetail=result.getShowExpDetail();
//                    int status=result.getStatus();
//                    String orderSn=result.getOrderSn();
//                    String payTime=result.getPayTime();
//                    String payWayDesc=result.getPayWayDesc();
//                    String expTime=result.getExpTime();

                    JSONObject jsondata = json.getJSONObject("result");
                    String statusDesc = jsondata.getString("statusDesc");
                    String safterSaleStatusCode = jsondata.getString("safterSaleStatusCode");
                    String showExpDetail = jsondata.getString("showExpDetail");
                    String status = jsondata.getString("status");
                    String orderSn = jsondata.getString("viewSn");
                    String payTime = jsondata.getString("payTime");
                    String payWayDesc = jsondata.getString("payWayDesc");
                    String expTime = jsondata.getString("expTime");
                    String amount = StringUtil.doubleToString(jsondata, "amount");
                    String totalTax = StringUtil.doubleToString(jsondata, "totalTax");
                    String totalExpPrice = StringUtil.doubleToString(jsondata, "totalExpPrice");
                    String totalGoodsPrice = StringUtil.doubleToString(jsondata, "totalGoodsPrice");
                    String discountPrice = StringUtil.doubleToString(jsondata, "discountPrice");
                    String couponDiscount = StringUtil.getDouble(jsondata, "couponDiscount");
                    JSONObject jsonAddress = jsondata.getJSONObject("addressVO");
                    String name = jsonAddress.getString("name");
                    String mobile = jsonAddress.getString("mobile");
                    String provinceName = jsonAddress.getString("provinceName");
                    String cityName = jsonAddress.getString("cityName");
                    String districtName = jsonAddress.getString("districtName");
                    String address = jsonAddress.getString("address");
                    JSONArray jsonitemList = jsondata.getJSONArray("itemList");
                    for (int i = 0; i < jsonitemList.length(); i++) {
                        JSONObject jsonShop = jsonitemList.getJSONObject(i);
                        String itemName = jsonShop.getString("itemName");
                        String imgUrl = jsonShop.getString("imgUrl");
                        String quantity = jsonShop.getString("quantity");
                        String id = jsonShop.getString("id");
                        String unitPrice = StringUtil.doubleToString(jsonShop, "unitPrice");
                        adapter.addData(new DetailsInfo(itemName, imgUrl, unitPrice, quantity, id));
                    }
                    context.updateData(statusDesc, orderSn, payTime, payWayDesc, expTime,
                            amount, totalTax, totalGoodsPrice, discountPrice, name, mobile,
                            provinceName + cityName + districtName + address, safterSaleStatusCode, status, showExpDetail,
                            totalExpPrice,couponDiscount);
                    context.dismissDialog();
                } catch (Exception e) {
                    e.printStackTrace();

                    onCatch();
                }

            }

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

            @Override
            public void onCatch() {
                LogUtil.d("异常");
                context.dismissDialog();
            }

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


}
