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.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.ShopDesignActivity;
import com.danding.yang800.Util.BitmapUtil;
import com.danding.yang800.Util.LogUtil;
import com.danding.yang800.Util.VolleyUtil;

import org.json.JSONObject;

import java.util.HashMap;

public class ShopDesignActivityPresenter {
    private ShopDesignActivity context;
    SharedPreferences sp;
    RequestQueue requestQueue;

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

    public void getShopInfo(final String id) {
        HashMap<String, String> map = new HashMap<>();
        map.put("id", id);
//        new VolleyUtil(context).request(context, "http://" + BuildConfig.HOSTURL + "/xhr/store/setting/update/getStoreInfo", true,map, new HttpCallBack() {
        new VolleyUtil(context).request(context,  "store.setting.update.getStoreInfo", true,map, new HttpCallBack() {
            @Override
            public void onSuccess(JSONObject json) {
                try {
                    JSONObject jsondata = json
                            .getJSONObject("result");
                    String name = jsondata.getString("name");
                    String des = jsondata.getString("des");
                    String headImg = jsondata.getString("avatar");
                    String backImg = jsondata.getString("img");
                    boolean authentication = jsondata.getBoolean("authentication");
                    String style = jsondata.getString("style");
                    context.initData(name, headImg, backImg, authentication, style, des);
                    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();
            }
        });
//
    }

    public void updateInfo(final String id, final boolean authentication, final String style) {
        HashMap<String, String> map = new HashMap<>();
        map.put("id", id);
        if (!style.equals("")) {
            map.put("style", style);
            map.put("authentication", authentication + "");
        } else {
            map.put("authentication", authentication + "");
        }
//        new VolleyUtil(context).request(context, "http://" + BuildConfig.HOSTURL + "/xhr/store/setting/update/storeInfo",true, map, new HttpCallBack() {
        new VolleyUtil(context).request(context, "store.setting.update.storeInfo",true, map, new HttpCallBack() {
            @Override
            public void onSuccess(JSONObject json) {
                try {
                    if (!style.equals("")) {
                        context.updateType(style);
                    } else {
                        context.updateShow(authentication);
                    }
                    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();
            }
        });
//
    }


    public void updateImg(Bitmap bitmap, String RequestURL, final String storeId, final boolean head) {
        MultipartRequest multipartRequest = new MultipartRequest(
                RequestURL, new Response.Listener<String>() {
            @Override
            public void onResponse(String string) {
                //string为服务器返回的字符串
                Log.d("LoginActivity", 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");
                        // context.UpdateImg();
                        updateImageInfo(storeId, imgUrl, head);
                    } else {
                        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.addStringPart("id", storeId);
        multi.addBinaryPart("files", BitmapUtil.getValue(bitmap), "", "myImg.jpg");
        requestQueue.add(multipartRequest);
    }

    public void updateImageInfo(final String storeId, final String imageUrl, final boolean head) {
        HashMap<String, String> map = new HashMap<>();
        map.put("storeId", storeId);
        if (head) {
            map.put("avatarImgUrl", imageUrl);
        } else {
            map.put("storeImgUrl", imageUrl);
        }
//        new VolleyUtil(context).request(context, "http://" + BuildConfig.HOSTURL + "/xhr/store/setting/update/storeImg",true, map, new HttpCallBack() {
        new VolleyUtil(context).request(context,  "store.setting.update.storeImg",true, map, new HttpCallBack() {
            @Override
            public void onSuccess(JSONObject json) {
                try {
                    context.UpdateImg();
                    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();
            }
        });
//
    }
    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() {
            }
        });

    }
}
