package com.danding.yang800.Presenter;

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

import com.danding.yang800.Adapter.SeeShopAdapter;
import com.danding.yang800.Adapter.SeeShopTwoAdapter;
import com.danding.yang800.BuildConfig;
import com.danding.yang800.Entity.SeeShopInfo;
import com.danding.yang800.General.HttpCallBack;
import com.danding.yang800.General.Yang800App;
import com.danding.yang800.SeeActivity;
import com.danding.yang800.Util.ToastUtil;
import com.danding.yang800.Util.VolleyUtil;

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

import java.util.HashMap;

/**
 * Created by Administrator on 2017/1/12.
 */
public class SeeActivityPresenter {
    private SeeActivity context;
    SharedPreferences sp;
    SeeShopAdapter adapter;
    SeeShopTwoAdapter twoAdapter;

    public SeeActivityPresenter(SeeActivity context, SeeShopAdapter adapter, SeeShopTwoAdapter twoAdapter) {
        sp = context.getSharedPreferences("variable", Context.MODE_PRIVATE);
        this.context = context;
        this.adapter = adapter;
        this.twoAdapter = twoAdapter;
    }

    public void getGoodsInfo(final String id) {
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("storeId", id);
        new VolleyUtil(context).request(context,  "store.preview.getSotreGoodsPreview",true, map, new HttpCallBack() {
            @Override
            public void onSuccess(JSONObject json) {
                try {
                    JSONObject jsondata = json.getJSONObject("result");
                    String style = jsondata.getString("style");
                    boolean authentication = jsondata.getBoolean("authentication");
                    JSONObject jsonShop = jsondata.getJSONObject("storeVO");
                    String name = jsonShop.getString("name");
                    String des = jsonShop.getString("des");
                    String avatar = jsonShop.getString("avatar");
                    String img = jsonShop.getString("img");
                    JSONArray jsonArray = jsondata
                            .getJSONArray("storeGoodsPreviewVOList");
                    for (int i = 0; i < jsonArray.length(); i++) {
                        JSONObject jsonobj = jsonArray.getJSONObject(i);
                        String goodsName = jsonobj.getString("goodsName");
                        String goodsImgUrl = jsonobj.getString("goodsImgUrl");
                        int sellStatus = jsonobj.getInt("sellStatus");
                        int skuMarketInventory = jsonobj.getInt("skuMarketInventory");
                        String price = jsonobj.getString("price");
                        String stats = "1";
                        boolean taxFree ;
                        boolean expFree ;
                        try {
                            taxFree=jsonobj.getBoolean("taxFree");
                            expFree= jsonobj.getBoolean("expFree");
                        }catch (Exception e){
                            taxFree=false;
                            expFree=false;
                        }
                        if (sellStatus == 1 || skuMarketInventory <= 0) {
                            stats = "3";
                        }
                        if (style.equals("1")) {
                            twoAdapter.addData(new SeeShopInfo(goodsName, goodsImgUrl, price, stats,taxFree,expFree));
                        } else {
                            adapter.addData(new SeeShopInfo(goodsName, goodsImgUrl, price, stats,taxFree,expFree));
                        }
                    }
                    context.updateDate(style, authentication, name, des, avatar, img);
                    context.dismissDialog();

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

            }


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

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

            @Override
            public void onFail() {
                context.dismissDialog();
            }
        });
//        StringRequest stringRequest = new StringRequest(Request.Method.POST,
//                "http://" + BuildConfig.HOSTURL + "/xhr/store/preview/getSotreGoodsPreview",
//                new Response.Listener<String>() {
//                    @Override
//                    public void onResponse(String response) {
//                        Log.d("LoginActivity", response);
//                        //System.out.println(response);
//                        try {
//                            JSONObject json = new JSONObject(response);
//                            String code = json.getString("code");
//                            if (code.equals("success")) {
//                                JSONObject jsondata = json.getJSONObject("data");
//                                String style = jsondata.getString("style");
//                                boolean authentication = jsondata.getBoolean("authentication");
//                                JSONObject jsonShop=jsondata.getJSONObject("storeVO");
//                                String name=jsonShop.getString("name");
//                                String des=jsonShop.getString("des");
//                                String avatar=jsonShop.getString("avatar");
//                                String img=jsonShop.getString("img");
//                                JSONArray jsonArray = jsondata
//                                        .getJSONArray("storeGoodsPreviewVOList");
//                                for (int i = 0; i < jsonArray.length(); i++) {
//                                    JSONObject jsonobj = jsonArray.getJSONObject(i);
//                                    String goodsName = jsonobj.getString("goodsName");
//                                    String goodsImgUrl = jsonobj.getString("goodsImgUrl");
//                                    int sellStatus = jsonobj.getInt("sellStatus");
//                                    int skuMarketInventory = jsonobj.getInt("skuMarketInventory");
//                                    String price = jsonobj.getString("price");
//                                    String stats="1";
//                                    if (sellStatus==1||skuMarketInventory<=0){
//                                        stats="3";
//                                    }
//                                    if (style.equals("1")) {
//                                        twoAdapter.addData(new SeeShopInfo(goodsName, goodsImgUrl, price,stats));
//                                    } else {
//                                        adapter.addData(new SeeShopInfo(goodsName, goodsImgUrl, price,stats));
//                                    }
//                                }
//                                context.updateDate(style, authentication,name,des,avatar,img);
//                            } else {
//
//                            }
//                            context.dismissDialog();
//
//                        } catch (Exception e) {
//                            e.printStackTrace();
//                            context.dismissDialog();
//                        }
//
//                    }
//                }, new Response.ErrorListener() {
//
//            @Override
//            public void onErrorResponse(VolleyError error) {
//                context.dismissDialog();
//            }
//        }) {
//            @Override
//            protected HashMap<String, String> getParams() { //
//                HashMap<String, String> map = new HashMap<String, String>();
//                map.put("storeId", id);
//                return map;
//            }
//
//
//        };
//        stringRequest.setTag("POST_TAG");
//        requestQueue.add(stringRequest);
    }


}
