package com.danding.yang800.Presenter;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.danding.yang800.BuildConfig;
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.RefundActivity;
import com.danding.yang800.Util.BitmapUtil;
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.JSONObject;

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

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

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

    public void refund(final JSONObject json) {
        LogUtil.d(json.toString());
        HashMap<String, String> map = new HashMap<>();
        map.put("pampasCall", "SafterSale.add");
        map.put("addSafterSale", json.toString());
        new VolleyUtil(context).request(context, "SafterSale.add", true, map, new HttpCallBack() {
            @Override
            public void onSuccess(JSONObject json) {
                ToastUtil.show(context, "保存成功");
                Intent intent = new Intent();
                context.setResult(0, intent);
                context.finish();
            }

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

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

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

    public void updateImg(final List<Bitmap> bitList, 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 imgList[] = new String[bitList.size()];
                        for (int i = 0; i < bitList.size(); i++) {
                            String imgUrl = jsonData.getString(i + ".jpg");
                            imgList[i] = imgUrl;
                        }
                        context.updataImgInfo(imgList);
                    } 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();
        for (int i = 0; i < bitList.size(); i++) {
            multi.addBinaryPart(type, BitmapUtil.getValue(bitList.get(i)), "", i + ".jpg");
        }

        requestQueue.add(multipartRequest);
    }
    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() {
            }
        });

    }
}
