package com.ym.klccy;

import android.content.SharedPreferences;
import android.text.TextUtils;
import com.ym.modulecommon.AppliContext;
import com.ym.modulecommon.AdConstant;
import com.ym.modulecommon.utils.AdPreference;
import com.ym.modulecommon.utils.Utils;

/**
 * Created by gaoleichao on 2018/5/5.
 */
public class SettingPreference {

    private static final String USER_SETTING = "user_setting";
    private static final String USER_ID = "user_id";
    private static final String USER_INFO_KEY = "user_info_keys";

    public static String getUid() {
        String uid = AdConstant.param.uid;
        if (TextUtils.isEmpty(uid)) {
            SharedPreferences settings = AppliContext.get().getSharedPreferences(USER_SETTING, 0);
            return settings.getString(USER_ID, "");
        }
        return uid;
    }

    public static void setUid(String uid) {
        AdConstant.param.uid = TextUtils.isEmpty(uid) ? "" : uid;
        SharedPreferences settings = AppliContext.get().getSharedPreferences(USER_SETTING, 0);
        SharedPreferences.Editor editor = settings.edit();
        editor.putString(USER_ID, AdConstant.param.uid);
        editor.commit();
    }

    public static String getToken() {
        String token = AdConstant.param.token;
        if (TextUtils.isEmpty(token)) {
            SharedPreferences settings = AppliContext.get().getSharedPreferences(USER_SETTING, 0);
            return settings.getString(AdConstant.Device.token, "");
        }
        return token;
    }

    public static void setToken(String token) {
        AdConstant.param.token = token;
        SharedPreferences settings = AppliContext.get().getSharedPreferences(USER_SETTING, 0);
        SharedPreferences.Editor editor = settings.edit();
        editor.putString(AdConstant.Device.token, AdConstant.param.token);
        editor.commit();
    }

    // 保存登陆用户信息。
    public static void saveUserInfo(UserInfoEntity userInfoData) {
        if (userInfoData == null) {
            setUid("");
            AdPreference.setUid("");
        } else {
            setUid(userInfoData.getUid());
            AdPreference.setUid(userInfoData.getUid());
            AdPreference.setAgeDate(userInfoData.getAge());
            String gender;
            if (TextUtils.equals(userInfoData.getSex(), "1"))
                gender = "男";
            else {
                gender = "女";
            }
            AdPreference.setGenderDate(gender);
            AdPreference.setWeightDate(userInfoData.getWeight() + "kg");
            AdPreference.setHeightDate(userInfoData.getHeight() + "cm");
            setStepSettingType(userInfoData);
        }
        SharedPreferences settings = AppliContext.get().getSharedPreferences(USER_SETTING, 0);
        SharedPreferences.Editor editor = settings.edit();
        editor.putString(USER_INFO_KEY, null == userInfoData ? "" : Utils.obj2Str(userInfoData)).commit();
    }

    private static void setStepSettingType(UserInfoEntity userInfoData) {
        String type = "1";
        if (TextUtils.equals("0", userInfoData.getAge())) {
            type = "0";
        }
        if (TextUtils.equals("0", userInfoData.getWeight())) {
            type = "0";
        }
        if (TextUtils.equals("0", userInfoData.getHeight())) {
            type = "0";
        }
        if (TextUtils.equals("0", userInfoData.getSex())) {
            type = "0";
        }
        SharedPreferences sharedPre = AppliContext.get().getSharedPreferences(USER_SETTING, 0);
        SharedPreferences.Editor editor = sharedPre.edit();
        editor.putString("StepSettingType", type);
        editor.commit();
    }

    // 获取登陆用户信息。
    public static UserInfoEntity getUserInfoData() {
        SharedPreferences settings = AppliContext.get().getSharedPreferences(USER_SETTING, 0);
        String user = settings.getString(USER_INFO_KEY, "");
        if (TextUtils.isEmpty(user)) {
            return null;
        }
        return (UserInfoEntity) Utils.str2Obj(user, UserInfoEntity.class);
    }
}