package com.danding.yang800.Presenter;

import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.util.Log;

import com.android.volley.AuthFailureError;
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.BuildConfig;
import com.danding.yang800.Entity.GoodsInfo;
import com.danding.yang800.General.HttpCallBack;
import com.danding.yang800.General.MultipartEntity;
import com.danding.yang800.General.MultipartRequest;
import com.danding.yang800.General.SingleRequestQueue;
import com.danding.yang800.General.Yang800App;
import com.danding.yang800.PictureCheckActivity;
import com.danding.yang800.Util.BitmapUtil;
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;
import java.util.Map;

/**
 * Created by Administrator on 2017/1/12.
 */
public class PictureCheckActivityPresenter {
    private PictureCheckActivity context;
    SharedPreferences sp;
    RequestQueue requestQueue;

    public PictureCheckActivityPresenter(PictureCheckActivity context) {
        sp = context.getSharedPreferences("variable", Context.MODE_PRIVATE);
        this.context = context;
        requestQueue = SingleRequestQueue.getRequestQueue(context);
    }


    public void updateImg(Bitmap bitmap, String RequestURL, String type) {
        MultipartRequest multipartRequest = new MultipartRequest(
                RequestURL, new Response.Listener<String>() {
            @Override
            public void onResponse(String string) {
                //string为服务器返回的字符串
                LogUtil.d(string);
                try {
                    JSONObject json = new JSONObject(string);
                    String code = json.getString("code");
                    if (code.equals("success")) {
                        JSONObject jsonData = json.getJSONObject("data");
                        String imgUrl = jsonData.getString("myImg.jpg");
                        sendImgUrl(imgUrl);
                    } else {
                        String errorMsg = json.getString("errorMsg");
                        ToastUtil.show(context, errorMsg);
                        //  ToastUtil.show(context, "图片上传失败！");
                    }
                    context.dismissDialog();
                } catch (Exception e) {
                    e.printStackTrace();
                    context.dismissDialog();

                }

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                context.dismissDialog();
            }
        });
        // multipartRequest.addHeader("header-name", "value");
        MultipartEntity multi = multipartRequest.getMultiPartEntity();
        multi.addBinaryPart(type, BitmapUtil.getValue(bitmap), "", "myImg.jpg");
        multi.addBinaryPart(type, BitmapUtil.getValue(bitmap), "", "myImg2.jpg");
        requestQueue.add(multipartRequest);
    }

    public void sendImgUrl(final String img) {
        //  context.initDialog();
        HashMap<String, String> map = new HashMap<>();
        map.put("img", img);
        new VolleyUtil(context).request(context, "login.wxBindImg", true, map, new HttpCallBack() {
            @Override
            public void onSuccess(JSONObject json) {
                ToastUtil.show(context, "已提交，等待审核中");
                context.dismissDialog();
                context.finish();
            }
            @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/login/wxBindImg.json",
//                new Response.Listener<String>() {
//                    @Override
//                    public void onResponse(String response) {
//                        LogUtil.d(response);
//                        try {
//                            JSONObject json = new JSONObject(response);
//                            String code = json.getString("code");
//                            if (code.equals("success")) {
//                                ToastUtil.show(context, "已提交，等待审核中");
//                                context.dismissDialog();
//                                context.finish();
//                            } else {
//                                String errorMsg = json.getString("errorMsg");
//                                ToastUtil.show(context, errorMsg);
//                                // ToastUtil.show(context, "提交失败！");
//                            }
//                            context.dismissDialog();
//                        } catch (Exception e) {
//                            e.printStackTrace();
//                            context.dismissDialog();
//                        }
//
//                    }
//                }, new Response.ErrorListener() {
//
//            @Override
//            public void onErrorResponse(VolleyError error) {
//                LogUtil.d("fail");
//                context.dismissDialog();
//            }
//        }) {
//
//            @Override
//            protected HashMap<String, String> getParams() { //
//                HashMap<String, String> map = new HashMap<String, String>();
//                map.put("img", img);
//                return map;
//            }
//
//
//            @Override
//            public Map<String, String> getHeaders() throws AuthFailureError {
//                if (!sp.getString("Cookie", "").equals("")) {
//                    HashMap<String, String> headers = new HashMap<String, String>();
//                    headers.put("Cookie", sp.getString("Cookie", ""));
//                    return headers;
//                } else {
//                    return super.getHeaders();
//                }
//            }
//
//        };
//        stringRequest.setTag("POST_TAG");
//        requestQueue.add(stringRequest);
    }
    public void getUploadImgUrl() {
        // ToastUtil.show(loginActivity, code);
        //	Log.i("TAG", "result  url=" + url + "\t code =: " + code);、
        HashMap<String, String> map = new HashMap<>();
        new VolleyUtil(context).request(context, "store.getUploadImgUrl", true, map, new HttpCallBack() {
            @Override
            public void onSuccess(JSONObject json) {
                LogUtil.d(json.toString());
                try {
                    JSONObject jsonData = json.getJSONObject("result");
                    String uploadImgUrl = jsonData.getString("uploadImgUrl");
                    sp.edit().putString("uploadImgUrl", uploadImgUrl).commit();
                } catch (Exception e) {
                    e.printStackTrace();
                    onCatch();

                }

            }


            @Override
            public void onerrSuccess() {
            }

            @Override
            public void onCatch() {
            }

            @Override
            public void onFail() {
            }
        });

    }

}
