package com.phlx.wool.data;


import androidx.annotation.NonNull;


import com.phlx.wool.data.http.HttpDataSource;
import com.phlx.wool.data.local.LocalDataSource;
import com.phlx.wool.db.DbUtil;
import com.phlx.wool.entity.DivisionEntity;
import com.phlx.wool.entity.DivisionEntityDao;
import com.phlx.wool.entity.Harmless;
import com.phlx.wool.entity.Label;
import com.phlx.wool.entity.Marking;
import com.phlx.wool.entity.Quarantine;
import com.phlx.wool.entity.TreatmentMethod;
import com.phlx.wool.entity.Unit;
import com.phlx.wool.entity.User;
import com.phlx.wool.entity.Variety;
import com.phlx.wool.entity.Veterinary;
import com.phlx.wool.entity.VeterinaryDao;
import com.phlx.wool.entity.VillusBuyEntity;
import com.phlx.wool.entity.VillusBuyEntityDao;
import com.phlx.wool.entity.VillusGaugeEntity;
import com.phlx.wool.params.BasicParams;
import com.phlx.wool.params.CattleResponse;

import org.greenrobot.greendao.annotation.Id;
import org.greenrobot.greendao.query.QueryBuilder;

import java.util.List;

import io.reactivex.Observable;
import me.goldze.mvvmhabit.base.BaseModel;
import okhttp3.RequestBody;

/**
 *
 */
public class Repository extends BaseModel implements HttpDataSource, LocalDataSource {
    private volatile static Repository INSTANCE = null;
    private final HttpDataSource mHttpDataSource;

    private final LocalDataSource mLocalDataSource;

    private Repository(@NonNull HttpDataSource httpDataSource,
                       @NonNull LocalDataSource localDataSource) {
        this.mHttpDataSource = httpDataSource;
        this.mLocalDataSource = localDataSource;
    }

    public static Repository getInstance(HttpDataSource httpDataSource,
                                         LocalDataSource localDataSource) {
        if (INSTANCE == null) {
            synchronized (Repository.class) {
                if (INSTANCE == null) {
                    INSTANCE = new Repository(httpDataSource, localDataSource);
                }
            }
        }
        return INSTANCE;
    }

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


    @Override
    public Observable<CattleResponse<Double>> versionGet() {
        return mHttpDataSource.versionGet();
    }

    @Override
    public Observable<CattleResponse> DownloadApp() {
        return mHttpDataSource.DownloadApp();
    }

    @Override
    public Observable<CattleResponse<User>> loginPost(RequestBody body) {
        return mHttpDataSource.loginPost(body);
    }

    @Override
    public Observable<CattleResponse<BasicParams>> getBasicInfo(RequestBody body) {
        return mHttpDataSource.getBasicInfo(body);
    }

    @Override
    public Observable<CattleResponse<List<Label>>> getLabelList(RequestBody body) {
        return mHttpDataSource.getLabelList(body);
    }

    @Override
    public Observable<CattleResponse<List<Unit>>> getUnitList(RequestBody body) {
        return mHttpDataSource.getUnitList(body);
    }

    @Override
    public Observable<CattleResponse<List<Veterinary>>> getVeterinaryList(RequestBody body) {
        return mHttpDataSource.getVeterinaryList(body);
    }

    @Override
    public Observable<CattleResponse<List<VillusBuyEntity>>> getVillusBuyList(RequestBody body) {
        return mHttpDataSource.getVillusBuyList(body);
    }

    @Override
    public Observable<CattleResponse<List<DivisionEntity>>> getDivisionList(RequestBody body) {
        return mHttpDataSource.getDivisionList(body);
    }

    @Override
    public Observable<CattleResponse<List<Marking>>> submitMarking(RequestBody body) {
        return mHttpDataSource.submitMarking(body);
    }

    @Override
    public Observable<CattleResponse<List<VillusBuyEntity>>> submitVillusBuy(RequestBody body) {
        return mHttpDataSource.submitVillusBuy(body);
    }

    @Override
    public Observable<CattleResponse<List<VillusGaugeEntity>>> submitVillusGauge(RequestBody body) {
        return mHttpDataSource.submitVillusGauge(body);
    }

    @Override
    public Observable<CattleResponse<List<Quarantine>>> submitQuarantine(RequestBody body) {
        return mHttpDataSource.submitQuarantine(body);
    }

    @Override
    public Observable<CattleResponse<List<Harmless>>> submitHarmless(RequestBody body) {
        return mHttpDataSource.submitHarmless(body);
    }


    @Override
    public void saveUserName(String userName) {
        mLocalDataSource.saveUserName(userName);
    }

    @Override
    public void savePassword(String password) {
        mLocalDataSource.savePassword(password);
    }

    @Override
    public void savePhone(String phone) {
        mLocalDataSource.savePhone(phone);
    }

    @Override
    public void saveEmail(String email) {
        mLocalDataSource.saveEmail(email);
    }

    @Override
    public void saveFrequencyConfig(String frequencyConfig) {
        mLocalDataSource.saveFrequencyConfig(frequencyConfig);
    }

    @Override
    public void saveBreed(String breed) {
        mLocalDataSource.saveBreed(breed);
    }

    @Override
    public void saveToken(String token) {
        mLocalDataSource.saveToken(token);
    }

    @Override
    public String getUserName() {
        return mLocalDataSource.getUserName();
    }

    @Override
    public String getPassword() {
        return mLocalDataSource.getPassword();
    }

    @Override
    public String getPhone() {
        return mLocalDataSource.getPhone();
    }

    @Override
    public String getEmail() {
        return mLocalDataSource.getEmail();
    }

    @Override
    public String getFrequencyConfig() {
        return mLocalDataSource.getFrequencyConfig();
    }

    @Override
    public String getBreed() {
        return mLocalDataSource.getBreed();
    }

    @Override
    public String getToken() {
        return mLocalDataSource.getToken();
    }


    @Override
    public void saveVariety(List<Variety> varieties) throws Exception {
        mLocalDataSource.saveVariety(varieties);
    }

    @Override
    public void saveTreatmentMethod(List<TreatmentMethod> treatmentMethods) throws Exception {
        mLocalDataSource.saveTreatmentMethod(treatmentMethods);
    }


    @Override
    public List<Variety> getVariety() {
        return mLocalDataSource.getVariety();
    }

    @Override
    public List<DivisionEntity> getPertain(String parentId) {
        QueryBuilder<DivisionEntity> builder =
                DbUtil.getInstance().getQueryBuilder(DivisionEntity.class)
                        .where(DivisionEntityDao.Properties.ParentCode.eq(parentId));
        return DbUtil.getInstance().queryAll(DivisionEntity.class, builder);
    }

    @Override
    public  List<DivisionEntity> getPertainForCode(String Id) {
         QueryBuilder<DivisionEntity> builder =
                DbUtil.getInstance().getQueryBuilder(DivisionEntity.class)
                        .where(DivisionEntityDao.Properties.Code.eq(Id));
        return DbUtil.getInstance().queryAll(DivisionEntity.class, builder);
    }

    @Override
    public List<VillusBuyEntity> getVillusBuy() {
        QueryBuilder<VillusBuyEntity> builder =
                DbUtil.getInstance().getQueryBuilder(VillusBuyEntity.class)
                        .orderDesc(VillusBuyEntityDao.Properties.CreateTime)
                ;
        return DbUtil.getInstance().queryAll(VillusBuyEntity.class);
    }

    @Override
    public List<TreatmentMethod> getTreatmentMethod() {
        return mLocalDataSource.getTreatmentMethod();
    }

}
