package com.ycgis.macall.personalcenter.v.viewmodel;

import android.os.Message;
import android.util.Log;

import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.MediatorLiveData;
import androidx.lifecycle.Observer;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.rs.macall.androidx.basemodel.base.BaseViewModel;
import com.rs.macall.androidx.basemodel.utils.LogUtils;
import com.rs.macall.androidx.basemodel.utils.StringUtil;
import com.rs.macall.androidx.basemodel.utils.TypConversion;
import com.ycgis.macall.personalcenter.m.adapterbean.BaseBean;
import com.ycgis.macall.personalcenter.p.app.AppCache;
import com.ycgis.macall.personalcenter.p.app.RuanseeApplication;
import com.ycgis.macall.personalcenter.p.app.UserData;
import com.ycgis.macall.personalcenter.p.callback.BaseRequestCallback;
import com.ycgis.macall.personalcenter.p.request.ApiModel;
import com.ycgis.macall.personalcenter.p.request.RetrofitService;

import org.json.JSONException;
import org.json.JSONObject;

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

/**
 * created by:   Macall
 * create time:  2023/11/22 11:46
 * copyright:    @ruansee.com
 * Describe:
 */
public class MyDetailsViewModel extends BaseViewModel {
    private MediatorLiveData<BaseBean> updateLiveData = new MediatorLiveData<>();

    public void addUpdateLiveDataObserve(LifecycleOwner owner, Observer<BaseBean> observer) {
        addLiveDataObserve(owner, updateLiveData, observer);
    }

    public void sendUpdate(int mun, final String value, String imsi, String imei) {
        final Map<String, String> param = new HashMap<>();
        AppCache cacheData = RuanseeApplication.getAppCache();
        param.put("token", cacheData.getToken());
        param.put("clientType", "MOBILE");//SERVER
        param.put("imsi1", imsi);
        param.put("deviceFlags", imei);
        param.put("sysCode", cacheData.getSysmCode());
        if (mun == 1) {
            param.put("workPhone", value);
        } else if (mun == 2) {
            param.put("phoneNum", value);
        }
        ApiModel.request("updateUser",
                RetrofitService.getBaseInstance().getLoginType("http://20.90.2.2/UniAuth/api/sso/v1/updateUserInfo", param),
                new BaseRequestCallback<JsonObject>() {
                    @Override
                    public void onRequestFailure(String msg) {
                        LogUtils.e(TAG, msg);
                        BaseBean b = new BaseBean() {
                        };
                        b.setBaseType(-1);
                        b.setBaseMessage(msg);
                        updateLiveData.postValue(b);
                    }

                    @Override
                    public void onRequestSuccess(JsonObject result) {
                        LogUtils.w(TAG, result.toString());
                        ApiModel.uploadOperationLog("3","3","用户修改统一用户手机号码");
                        try {
                            JSONObject object = new JSONObject(result.toString());
                            int code = TypConversion.getJsonInt(object, "code", -1);
                            if (code == 200) {
                                List<Object> payloads = getPayloads();
                                BaseBean b = new BaseBean() {
                                };
                                if (payloads!=null&&!payloads.isEmpty()){
                                    b.setBaseType((Integer) payloads.get(0));
                                    b.setBaseMessage((String) payloads.get(1));
                                }
                                updateLiveData.postValue(b);
                            }else {
                                BaseBean b = new BaseBean() {
                                };
                                b.setBaseType(-1);
                                b.setBaseMessage(StringUtil.get(TypConversion.getJsonStr(object,"msg"),TypConversion.getJsonStr(object,"content")));
                                updateLiveData.postValue(b);
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                            BaseBean b = new BaseBean() {
                            };
                            b.setBaseType(-1);
                            b.setBaseMessage(e.getMessage());
                            updateLiveData.postValue(b);
                        }
                    }
                }
                .addPayload(mun)
                .addPayload(value));
//        BaseRequest.postRequest("phone/updateUser", json, new SyncCallBack() {
//            @Override
//            public void onSucceed(int responseCode, String message) {
//                Log.w(TAG, message);
//                try {
//                    JSONObject object = new JSONObject(message);
//                    String code = TypConversion.getJsonStr(object, "code");
//                    if (StringUtil.hasContent(code) && code.equals("0")) {
//                        if (position == 7) {
//                            MyApplication.getAppCacheData().setPhone1(value);
//                        } else if (position == 8) {
//                            MyApplication.getAppCacheData().setPhone2(value);
//                        } else if (position == 9) {
//
//                        }
//                        myHandler.sendEmptyMessage(200);
//                        return;
//                    }
//                    if (StringUtil.isNullOrEmpty(code) || code.equals("-1")) {
//                        // 必传参数为空
//                        Message message1 = myHandler.obtainMessage(-140, "缺少必传参数！");
//                        myHandler.sendMessage(message1);
//                        return;
//                    }
//                    if (code.equals("-2")) {
//                        // 修改失败
//                        Message message1 = myHandler.obtainMessage(-140, "修改失败,请联系管理员！");
//                        myHandler.sendMessage(message1);
//                        return;
//                    }
//                    if (code.equals("500")) {
//                        // 修改失败
//                        Message message1 = myHandler.obtainMessage(-500, "服务错误,请联系管理员！");
//                        myHandler.sendMessage(message1);
//                        return;
//                    }
//                    isUpdata = false;
//                } catch (JSONException e) {
//                    e.printStackTrace();
//                    Message message1 = myHandler.obtainMessage(-140, "解析失败,请联系管理员！");
//                    myHandler.sendMessage(message1);
//                }
//            }
//
//            @Override
//            public void onFail(int responseCode, String message) {
//                Log.e(TAG, message);
//                Message message1 = myHandler.obtainMessage(responseCode, message);
//                myHandler.sendMessage(message1);
//            }
//        });
    }


}
