package com.danding.yang800.Presenter;

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

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.danding.yang800.Adapter.MyShopAdapter;
import com.danding.yang800.Entity.SortListInfo;
import com.danding.yang800.General.SingleRequestQueue;
import com.danding.yang800.MainActivity;

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

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

/**
 * Created by Administrator on 2017/1/12.
 */
public class SortListActivityPresenter {
    private MainActivity sortlist;
    MyShopAdapter adapter;
    SharedPreferences sp;
    RequestQueue requestQueue;

    public SortListActivityPresenter(MainActivity sortlist, MyShopAdapter adapter) {
        sp = sortlist.getSharedPreferences("variable", Context.MODE_PRIVATE);
        this.sortlist = sortlist;
        this.adapter = adapter;
        requestQueue = SingleRequestQueue.getRequestQueue(sortlist);
    }
    public void getSortlist() {
        StringRequest stringRequest = new StringRequest(Request.Method.POST,
                "http://" + "www.hibigoo.com" + "/b2b2c/wap/good!getGoodsSearchList.do",
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        //System.out.println(response);
                        try {
                            JSONObject json = new JSONObject(response);
                            String status = json.getString("result");
                            if (status.equals("1")) {
                                JSONObject jsondata = json.getJSONObject("data");
                                int totalCount = jsondata.getInt("totalCount");
                                // JSONObject jsondata2 = jsondata
                                // .getJSONObject("cat_tree");
                                JSONArray jjArray = jsondata
                                        .getJSONArray("result");
                                for (int i = 0; i < jjArray.length(); i++) {
                                    JSONObject jsonsss = jjArray.getJSONObject(i);
                                    String name = jsonsss.getString("name");
                                    String url = jsonsss.getString("small");
                                    double priceDouble = jsonsss.getDouble("price");
                                    String price = new DecimalFormat("0.00").format(priceDouble);
//                                    DecimalFormat decimalFormat = new DecimalFormat("0.00");//构造方法的字符格式这里如果小数不足2位,会以0补足.
//                                    String price = decimalFormat.format(decimalBalance);
                                    int goods_id = jsonsss.getInt("goods_id");
                                    int bonded_type = jsonsss.getInt("bonded_type");
                                    String depot_name = jsonsss.getString("depot_name");
                                    String type;
                                    if (!depot_name.equals("")) {
                                        type = depot_name + "发货";
                                    } else {
                                        type = depot_name;
                                    }
//                                    if (bonded_type == 1) {
//                                        type = "杭州保税仓发货";
//                                    } else {
//                                        type = "海彼购发货";
//                                    }
//                                    adapter.addData(new SortListInfo(url,"","",true,"","","","",false,"","","","",""));
//								int cat_id = jsonsss.getInt("cat_id");
//								System.out.println(cat_id + "sdsdsdsds");
//								JSONArray jjArray2 = jsonsss
//										.getJSONArray("children");
                                    //System.out.println("dsw");
//								for (int j = 0; j < jjArray2.length(); j++) {
//									JSONObject jsonss = jjArray2
//											.getJSONObject(j);
//									String image = jsonss.getString("image");
//									//System.out.println(image + "image");
//								}
                                }
                            } else {
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }

                    }
                }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
            }
        }) {

            @Override
            protected HashMap<String, String> getParams() { //
                HashMap<String, String> map = new HashMap<String, String>();
                map.put("sort", "buynum_desc");
                map.put("search", "goods");
                map.put("cat", "");
                map.put("pageSize", "100");
                map.put("keyword", "");
                map.put("page", "1");
                return map;
            }


        };
        stringRequest.setTag("POST_TAG");
        requestQueue.add(stringRequest);
    }


}
