package com.danding.yang800.Presenter;

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

import com.danding.yang800.BuildConfig;
import com.danding.yang800.DocumentActivity;
import com.danding.yang800.Entity.ThumbViewInfo;
import com.danding.yang800.General.HttpCallBack;
import com.danding.yang800.Util.LogUtil;
import com.danding.yang800.Util.VolleyUtil;

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

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

public class DocumentActivityPresenter {
    private DocumentActivity context;
    SharedPreferences sp;
    private List<ThumbViewInfo> mThumbViewInfoList = new ArrayList<>();

    public DocumentActivityPresenter(DocumentActivity context) {
        sp = context.getSharedPreferences("variable", Context.MODE_PRIVATE);
        this.context = context;
    }


    public void query(final String id, final String goodsSn) {
        HashMap<String, String> map = new HashMap<>();
        map.put("id", id);
        map.put("goodsSn", goodsSn);
        new VolleyUtil(context).request(context, "goods.copywrite.query", true, map, new HttpCallBack() {
            @Override
            public void onSuccess(JSONObject json) {
                LogUtil.d(json.toString());
                try {
                    JSONArray jsonData = json.getJSONArray("result");
                    String title = "";
                    String content = "";
                    for (int i = 0; i < jsonData.length(); i++) {
                        JSONObject jsonObj = jsonData.getJSONObject(i);
                        if (i == 0) {
                            title = jsonObj.getString("title");
                            content = jsonObj.getString("content");
                        }
                        String imgUrl = jsonObj.getString("imgUrl");
                        mThumbViewInfoList.add(new ThumbViewInfo(imgUrl, i));
                    }
                    context.updateData(title, content, mThumbViewInfoList);
                    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();
                LogUtil.d("fail");
            }

        });
//        StringRequest stringRequest = new StringRequest(Request.Method.POST,
//                "http://" + BuildConfig.HOSTURL + "/xhr/goods/copywrite/query",
//                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")) {
//                                JSONArray jsonData = json.getJSONArray("data");
//                                String title = "";
//                                String content = "";
//                                for (int i = 0; i < jsonData.length(); i++) {
//                                    JSONObject jsonObj = jsonData.getJSONObject(i);
//                                    if (i == 0) {
//                                        title = jsonObj.getString("title");
//                                        content = jsonObj.getString("content");
//                                    }
//                                    String imgUrl = jsonObj.getString("imgUrl");
//                                    mThumbViewInfoList.add(new ThumbViewInfo(ConfigUtil.getImageUrl(imgUrl), i));
////                                    mThumbViewInfoList.add(new ThumbViewInfo("https://img20.360buyimg.com/da/jfs/t9466/201/1546408582/116954/173761ac/59c36194N9a1387f1.jpg", i));
////                                    mThumbViewInfoList.add(new ThumbViewInfo("https://img20.360buyimg.com/da/jfs/t9466/201/1546408582/116954/173761ac/59c36194N9a1387f1.jpg", i));
////                                    mThumbViewInfoList.add(new ThumbViewInfo("https://img20.360buyimg.com/da/jfs/t9466/201/1546408582/116954/173761ac/59c36194N9a1387f1.jpg", i));
//                                }
//                                context.updateData(title, content, mThumbViewInfoList);
//                            }
//                            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("id", id);
//                map.put("goodsSn", goodsSn);
//                return map;
//            }
//
//
//        };
//        stringRequest.setTag("POST_TAG");
//        requestQueue.add(stringRequest);
    }


}
