package com.phlx.wool.data.local;



import com.phlx.wool.Configs;
import com.phlx.wool.db.DbUtil;
import com.phlx.wool.entity.DivisionEntity;
import com.phlx.wool.entity.TreatmentMethod;
import com.phlx.wool.entity.Variety;
import com.phlx.wool.entity.VillusBuyEntity;

import java.util.List;

import me.goldze.mvvmhabit.utils.SPUtils;


public class LocalDataSourceImpl implements LocalDataSource {
    private volatile static LocalDataSourceImpl INSTANCE = null;

    public static LocalDataSourceImpl getInstance() {
        if (INSTANCE == null) {
            synchronized (LocalDataSourceImpl.class) {
                if (INSTANCE == null) {
                    INSTANCE = new LocalDataSourceImpl();
                }
            }
        }
        return INSTANCE;
    }

    public static void destroyInstance() {
        INSTANCE = null;
    }

    @Override
    public void saveUserName(String userName) {
        SPUtils.getInstance().put(Configs.SP_USER_NAME, userName);
    }

    @Override
    public void savePassword(String password) {
        SPUtils.getInstance().put(Configs.SP_USER_PASSWORD, password);
    }

    @Override
    public void savePhone(String phone) {
        SPUtils.getInstance().put(Configs.SP_USER_PHONE, phone);
    }

    @Override
    public void saveEmail(String email) {
        SPUtils.getInstance().put(Configs.SP_USER_EMAIL, email);
    }

    @Override
    public void saveFrequencyConfig(String frequencyConfig) {
        SPUtils.getInstance().put(Configs.SP_FREQUENCY_CONFIG, frequencyConfig);
    }

    @Override
    public void saveBreed(String breed) {
        SPUtils.getInstance().put(Configs.SP_USER_UNIT, breed);
    }

    @Override
    public void saveToken(String token) {
        SPUtils.getInstance().put("token", token);
    }

    @Override
    public String getUserName() {
        return SPUtils.getInstance().getString(Configs.SP_USER_NAME, "");
    }

    @Override
    public String getPassword() {
        return SPUtils.getInstance().getString(Configs.SP_USER_PASSWORD, "");
    }

    @Override
    public String getPhone() {
        return null;
    }

    @Override
    public String getEmail() {
        return null;
    }

    @Override
    public String getFrequencyConfig() {
        return null;
    }

    @Override
    public String getBreed() {
        return null;
    }

    @Override
    public String getToken() {
        return SPUtils.getInstance().getString("token", "");
    }


    @Override
    public void saveVariety(List<Variety> varieties) throws Exception {
        DbUtil.getInstance()
//                .insertAsyncBatch(Variety.class, varieties);
                .insertOrReplaceInTx(varieties);
    }

    @Override
    public void saveTreatmentMethod(List<TreatmentMethod> treatmentMethods) throws Exception {
        DbUtil.getInstance()
//                .insertAsyncBatch(TreatmentMethod.class, treatmentMethods);
                .insertOrReplaceInTx(treatmentMethods);
    }

    @Override
    public List<Variety> getVariety() {
        return DbUtil.getInstance().queryAll(Variety.class);
    }

    @Override
    public List<DivisionEntity> getPertain(String parentId) {
        return DbUtil.getInstance().queryAll(DivisionEntity.class);
    }

    @Override
    public List<DivisionEntity> getPertainForCode(String Id) {
        return DbUtil.getInstance().queryAll(DivisionEntity.class);
    }

    @Override
    public List<VillusBuyEntity> getVillusBuy() {
        return DbUtil.getInstance().queryAll(VillusBuyEntity.class);
    }

    @Override
    public List<TreatmentMethod> getTreatmentMethod() {
        return DbUtil.getInstance().queryAll(TreatmentMethod.class);
    }


}
