package com.danding.yang800.Presenter;

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

import com.danding.yang800.BuildConfig;
import com.danding.yang800.ChangeShopNameActivity;
import com.danding.yang800.General.HttpCallBack;
import com.danding.yang800.Util.ToastUtil;
import com.danding.yang800.Util.VolleyUtil;

import org.json.JSONObject;

import java.util.HashMap;

public class ChangeNameActivityPresenter {
    private ChangeShopNameActivity context;
    SharedPreferences sp;

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


    public void updateInfo(final String id, final boolean authentication, final String name) {
        if (name.equals("")) {
            ToastUtil.show(context, "名称不能为空");
            context.dismissDialog();
            return;
        }
        if (name.length() > 20) {
            ToastUtil.show(context, "名称不能超过20个字符");
            context.dismissDialog();
            return;
        }
        HashMap<String, String> map = new HashMap<>();
        map.put("id", id);
        map.put("name", name);
        map.put("authentication", authentication + "");
        new VolleyUtil(context).request(context,  "store.setting.update.storeInfo", true,map, new HttpCallBack() {
            @Override
            public void onSuccess(JSONObject json) {
                try {
                    ToastUtil.show(context, "修改成功");
                    context.finish();
                    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();
            }
        });
    }


}
