package com.danding.yang800.Presenter;

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

import com.danding.yang800.Adapter.GoodsSortAdapter;
import com.danding.yang800.BuildConfig;
import com.danding.yang800.Entity.GoodsInfo;
import com.danding.yang800.General.HttpCallBack;
import com.danding.yang800.General.Yang800App;
import com.danding.yang800.GoodsSortActivity;
import com.danding.yang800.Util.GeneralUtil;
import com.danding.yang800.Util.LogUtil;
import com.danding.yang800.Util.ToastUtil;
import com.danding.yang800.Util.VolleyUtil;

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

import java.util.HashMap;

public class GoodsSortActivityPresenter {
    private GoodsSortActivity context;
    SharedPreferences sp;
    GoodsSortAdapter adapter;


    public GoodsSortActivityPresenter(GoodsSortActivity context, GoodsSortAdapter adapter) {
        sp = context.getSharedPreferences("variable", Context.MODE_PRIVATE);
        this.context = context;
        this.adapter = adapter;
    }

    public void getGoodsInfo(final String id) {
        HashMap<String, String> map = new HashMap<>();
        map.put("storeId", id);
        new VolleyUtil(context).request(context, "store.goodsSort.getGoodsInfo", true, map, new HttpCallBack() {
            @Override
            public void onSuccess(JSONObject json) {
                try {
                    JSONArray jsonArray = json
                            .getJSONArray("result");
                    for (int i = 0; i < jsonArray.length(); i++) {
                        JSONObject jsonobj = jsonArray.getJSONObject(i);
                        int goodsId = jsonobj.getInt("goodsId");
                        String imgUrl = jsonobj.getString("imgUrl");
                        String goodsSn = jsonobj.getString("goodsSn");
                        int displaySequence = jsonobj.getInt("displaySequence");
                        adapter.addData(new GoodsInfo(goodsId, imgUrl, goodsSn, displaySequence));
                    }
                    context.updateDate();

                } catch (Exception e) {
                    e.printStackTrace();
                    onCatch();
                }

            }


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

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

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

    public void updateGoodsSort(final JSONObject json) {
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("appKey", "yang800_wd");
        map.put("pampasCall", "store.goodsSort.update.goodsSort");
        map.put("data", json.toString());
        String sign = "";
        try {
            sign = GeneralUtil.generateSignature(map, BuildConfig.SECRET);
        } catch (Exception e) {
        }
        map.put("sign", sign);
        //  String param="appKey=yang800_wd&pampasCall=store.goodsSort.update.goodsSort&data="+json.toString()+"&sign="+sign;
        JSONObject paramJsonObject = new JSONObject(map);
        new VolleyUtil(context).jsonRequest(context, paramJsonObject, new HttpCallBack() {
            @Override
            public void onSuccess(JSONObject json) {
                ToastUtil.show(context, "保存成功");
                context.finish();
                context.dismissDialog();
            }

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

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

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


}
