package com.danding.yang800.Presenter;

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

import com.danding.yang800.Adapter.MarketInfoAdapter;
import com.danding.yang800.Entity.MarketShopInfo;
import com.danding.yang800.Fragment.MarketPlatformFrag;
import com.danding.yang800.General.HttpCallBack;
import com.danding.yang800.MaketShopInfoActivity;
import com.danding.yang800.MarketPlatFormInfoActivity;
import com.danding.yang800.Util.LogUtil;
import com.danding.yang800.Util.VolleyUtil;
import com.danding.yang800.Vo.PlatInfoVO;
import com.google.gson.Gson;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.List;

public class MarketPlatFormInfoActivityPresenter {
    private MarketPlatFormInfoActivity context;
    SharedPreferences sp;
    MarketInfoAdapter adapter;

    public MarketPlatFormInfoActivityPresenter(MarketPlatFormInfoActivity context, MarketInfoAdapter adapter) {
        sp = context.getSharedPreferences("variable", Context.MODE_PRIVATE);
        this.context = context;
        this.adapter = adapter;
    }

    public void getInfo(String id, String shopId, final String page) {
        LogUtil.d(shopId + "shopId");
        HashMap<String, String> map = new HashMap<>();
        map.put("promotionId", id);
        map.put("shopId", shopId);
        map.put("pageIndex", page);
        map.put("pageSize", "6");
        new VolleyUtil(context).request(context, "user.promotion.receive.info", true, map, new HttpCallBack() {
            @Override
            public void onSuccess(JSONObject json) {
                try {
                    Gson gson = new Gson();
                    PlatInfoVO bean = gson.fromJson(json.toString(), PlatInfoVO.class);
                    PlatInfoVO.ResultBean result = bean.getResult();
                    if (page.equals("1")) {
                        if (adapter != null) {
                            adapter.clear();
                        }
                        String name = result.getName();
//                    String availableQuantity = result.getAvailableQuantity();
                        String receivedQuantity = result.getReceivedQuantity();
                        String usedQuantity = result.getUsedQuantity();
                        String conditionFee = result.getReceiveFee();
                        context.initData(name, conditionFee, receivedQuantity, usedQuantity);
                    }
                    PlatInfoVO.ResultBean.PaginationBean pageBean = result.getPagination();
                    int totalPage = pageBean.getTotalPage();
                    List<PlatInfoVO.ResultBean.UserPromotionVOListBean> listBean = result.getUserPromotionVOList();
                    for (int i = 0; i < listBean.size(); i++) {
                        String userAvatar = listBean.get(i).getUserAvatar();
                        String userName = listBean.get(i).getUserName();
                        String updateTime = listBean.get(i).getUpdateTime();
                        String statusName = listBean.get(i).getStatusName();
                        adapter.addData(new MarketShopInfo(userAvatar, userName, updateTime, statusName));
                    }
                    context.updateData(totalPage);
//                    double marketPrice = result.getMarketPrice();
//                    String priceStr = new DecimalFormat("0.00").format(marketPrice);
//                    String priceArray[] = StringUtil.splitDouble(priceStr);
//                    String priceInt = "";
//                    String price = "";
//                    if (priceArray.length == 2) {
//                        priceInt = priceArray[0] + ".";
//                        price = priceArray[1];
//                    }
                } catch (Exception e) {
                    context.onLoad();
                }

            }

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

            @Override
            public void onCatch() {

            }

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


}
