package com.tenlionsoft.baselib.core.db;

import com.tenlionsoft.baselib.app.BaseAppContext;
import com.tenlionsoft.baselib.core.db.daos.FoodCanteenDao;
import com.tenlionsoft.baselib.core.db.daos.FoodFoodDao;
import com.tenlionsoft.baselib.core.db.daos.FoodPublishDao;
import com.tenlionsoft.baselib.core.db.daos.FoodPublishFoodDao;
import com.tenlionsoft.baselib.core.db.daos.FoodSubscribeDao;
import com.tenlionsoft.baselib.core.db.daos.FoodSubscribeFoodDao;
import com.tenlionsoft.baselib.core.db.daos.LocalMediaDao;
import com.tenlionsoft.baselib.core.db.daos.UserAccountDao;
import com.tenlionsoft.baselib.core.db.daos.UserAccountItemDao;
import com.tenlionsoft.baselib.core.db.daos.UserChargeDao;
import com.tenlionsoft.baselib.core.retrofit_net.conver.RxTransformer;
import com.tenlionsoft.baselib.utils.LogUtils;

import io.reactivex.rxjava3.annotations.NonNull;
import io.reactivex.rxjava3.core.Observable;
import io.reactivex.rxjava3.core.Observer;
import io.reactivex.rxjava3.disposables.Disposable;

/**
 * 作者: adam
 * 日期: 2022/3/31 - 10:30 上午
 * 邮箱: itgaojian@163.com
 * 描述: 数据
 */
public class DataCenter {
    private static volatile DataCenter mInstance;
    public FoodCanteenDao mFoodCanteenDao;//食堂信息表
    public FoodFoodDao mFoodFoodDao;//菜品表
    public FoodPublishDao mFoodPublishDao;//菜单表
    public FoodPublishFoodDao mFoodPublishFoodDao;//菜品菜单中间表
    public FoodSubscribeDao mFoodSubscribeDao;//订单表
    public FoodSubscribeFoodDao mFoodSubscribeFoodDao;//订单菜品中间表
    public UserAccountDao mUserAccountDao;//账户表
    public UserAccountItemDao mUserAccountItemDao;//账户流水表
    public UserChargeDao mUserChargeDao;//充值表
    public LocalMediaDao mLocalMediaDao;

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

    private DataCenter() {
        initDataCenter();
    }

    /**
     * 初始化
     */
    private void initDataCenter() {
        Observable.create(emitter -> {
            try {
                mFoodCanteenDao = AppDatabase.getInstance(BaseAppContext.getInstance()).getFoodCanteenDao();
                mFoodFoodDao = AppDatabase.getInstance(BaseAppContext.getInstance()).getFoodFoodDao();
                mFoodPublishDao = AppDatabase.getInstance(BaseAppContext.getInstance()).getFoodPublishDao();
                mFoodPublishFoodDao = AppDatabase.getInstance(BaseAppContext.getInstance()).getFoodPublishFoodDao();
                mFoodSubscribeDao = AppDatabase.getInstance(BaseAppContext.getInstance()).getFoodSubscribeDao();
                mFoodSubscribeFoodDao = AppDatabase.getInstance(BaseAppContext.getInstance()).getFoodSubscribeFoodDao();
                mUserAccountDao = AppDatabase.getInstance(BaseAppContext.getInstance()).getUserAccountDao();
                mUserAccountItemDao = AppDatabase.getInstance(BaseAppContext.getInstance()).getUserAccountItemDao();
                mUserChargeDao = AppDatabase.getInstance(BaseAppContext.getInstance()).getUserChargeDao();
                mLocalMediaDao = AppDatabase.getInstance(BaseAppContext.getInstance()).getLocalMediaDao();
                emitter.onNext("");
            } catch (Exception e) {
                emitter.onError(e);
            }
        })
            .compose(RxTransformer.getTransformer())
            .subscribe(new Observer<Object>() {
                @Override
                public void onSubscribe(@NonNull Disposable d) {

                }

                @Override
                public void onNext(@NonNull Object o) {
                    //初始化数据库成功
                    //更新数据
                    LogUtils.e("初始化数据成功");
                }

                @Override
                public void onError(@NonNull Throwable e) {
                    //初始化数据库失败
                    LogUtils.e("初始化数据库失败");
                    e.printStackTrace();

                }

                @Override
                public void onComplete() {

                }
            });
    }

}
