package com.danding.yang800.Presenter;

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

import com.danding.yang800.Adapter.GiftListAdapter;
import com.danding.yang800.ChangeShopNameActivity;
import com.danding.yang800.Entity.GiftListInfo;
import com.danding.yang800.General.HttpCallBack;
import com.danding.yang800.GiftListActivity;
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.GiftListVO;
import com.google.gson.Gson;

import org.json.JSONObject;

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

public class GiftListActivityPresenter {
    private GiftListActivity context;
    SharedPreferences sp;
    GiftListAdapter adapter;
    public GiftListActivityPresenter(GiftListActivity context,GiftListAdapter adapter) {
        sp = context.getSharedPreferences("variable", Context.MODE_PRIVATE);
        this.context = context;
        this.adapter=adapter;
    }
    public void getInfo(String pageNo) {
        HashMap<String, String> map = new HashMap<>();
        map.put("pageNo",pageNo);
        map.put("pageSize", "6");
        new VolleyUtil(context).request(context,  "goods.giftBag.list", true,map, new HttpCallBack() {
            @Override
            public void onSuccess(JSONObject json) {
                try {
                    Gson gson = new Gson();
                    GiftListVO bean = gson.fromJson(json.toString(), GiftListVO.class);
                    GiftListVO.ResultBean  result = bean.getResult();
                    int pageTotal=result.getPageTotal();
                    List<GiftListVO.ResultBean.DataBean> data=result.getData();
                    for (int i = 0; i < data.size(); i++) {
                        String skuName=data.get(i).getSkuName();
                        String storeSkuSn=data.get(i).getStoreSkuSn();
                        String imageUrl=data.get(i).getImageUrl();
                        String content=data.get(i).getContent();
                        double marketPrice=data.get(i).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];
                        }
                        adapter.addData(new GiftListInfo(skuName,storeSkuSn,imageUrl,priceInt,price,content));
                    }
                    context.initData(pageTotal);
                }catch (Exception e){

                }

            }

            @Override
            public void onerrSuccess() {

            }

            @Override
            public void onCatch() {

            }

            @Override
            public void onFail() {

            }
        });
    }


}
