package com.phlx.anchorcollect.data;


import androidx.annotation.NonNull;

import com.phlx.anchorcollect.data.http.HttpDataSource;
import com.phlx.anchorcollect.data.local.LocalDataSource;
import com.phlx.anchorcollect.entity.BreedingRecordDetailEntity;
import com.phlx.anchorcollect.entity.BreedingRecordEntity;
import com.phlx.anchorcollect.entity.CattleResumeEntity;
import com.phlx.anchorcollect.entity.DictEntity;
import com.phlx.anchorcollect.entity.GenData;
import com.phlx.anchorcollect.entity.ImmuneBatchDetailEntity;
import com.phlx.anchorcollect.entity.ImmuneBatchEntity;
import com.phlx.anchorcollect.entity.LoginData;
import com.phlx.anchorcollect.entity.MedicalRecordEntity;
import com.phlx.anchorcollect.entity.PerformanceEntity;
import com.phlx.anchorcollect.entity.WeightManEntity;
import com.phlx.anchorcollect.params.BaseResponse;
import com.phlx.anchorcollect.params.CollectResponse;
import com.phlx.anchorcollect.params.ListResponse;

import java.util.Map;

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<BaseResponse<Double>> versionGet() {
        return mHttpDataSource.versionGet();
    }

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

    @Override
    public Observable<CollectResponse<LoginData>> loginPost(Map<String,String> map) {
        return mHttpDataSource.loginPost(map);
    }

    @Override
    public Observable<CollectResponse<GenData>> genGet() {
        return mHttpDataSource.genGet();
    }

    @Override
    public Observable<ListResponse<CattleResumeEntity>> downloadCattleList(Map<String, String> map) {
        return mHttpDataSource.downloadCattleList(map);
    }

    @Override
    public Observable<CollectResponse> downloadCattleListSync(RequestBody body) {
        return mHttpDataSource.downloadCattleListSync(body);
    }

    @Override
    public Observable<ListResponse<DictEntity>> downloadDict() {
        return mHttpDataSource.downloadDict();
    }

    @Override
    public Observable<ListResponse<PerformanceEntity>> downloadPerformanceList(Map<String, String> map) {
        return mHttpDataSource.downloadPerformanceList(map);
    }

    @Override
    public Observable<CollectResponse> downloadPerformanceListSync(RequestBody body) {
        return mHttpDataSource.downloadPerformanceListSync(body);
    }

    @Override
    public Observable<ListResponse<Long>> uploadPerformanceList(RequestBody body) {
        return mHttpDataSource.uploadPerformanceList(body);
    }

    @Override
    public Observable<ListResponse<BreedingRecordEntity>> downloadBreedingRecordList(Map<String, String> map) {
        return mHttpDataSource.downloadBreedingRecordList(map);
    }

    @Override
    public Observable<CollectResponse> downloadBreedingRecordListSync(RequestBody body) {
        return mHttpDataSource.downloadBreedingRecordListSync(body);
    }

    @Override
    public Observable<ListResponse<Long>> uploadBreedingRecordList(RequestBody body) {
        return mHttpDataSource.uploadBreedingRecordList(body);
    }

    @Override
    public Observable<ListResponse<BreedingRecordDetailEntity>> downloadBreedingRecordDetailList(Map<String, String> map) {
        return mHttpDataSource.downloadBreedingRecordDetailList(map);
    }

    @Override
    public Observable<CollectResponse> downloadBreedingRecordDetailListSync(RequestBody body) {
        return mHttpDataSource.downloadBreedingRecordDetailListSync(body);
    }

    @Override
    public Observable<ListResponse<Long>> uploadBreedingRecordDetailList(RequestBody body) {
        return mHttpDataSource.uploadBreedingRecordDetailList(body);
    }

    @Override
    public Observable<ListResponse<WeightManEntity>> downloadWeightManList(Map<String, String> map) {
        return mHttpDataSource.downloadWeightManList(map);
    }

    @Override
    public Observable<CollectResponse> downloadWeightManListSync(RequestBody body) {
        return mHttpDataSource.downloadWeightManListSync(body);
    }

    @Override
    public Observable<ListResponse<Long>> uploadWeightManList(RequestBody body) {
        return mHttpDataSource.uploadWeightManList(body);
    }

    @Override
    public Observable<ListResponse<MedicalRecordEntity>> downloadMedicalRecordList(Map<String, String> map) {
        return mHttpDataSource.downloadMedicalRecordList(map);
    }

    @Override
    public Observable<CollectResponse> downloadMedicalRecordListSync(RequestBody body) {
        return mHttpDataSource.downloadMedicalRecordListSync(body);
    }

    @Override
    public Observable<ListResponse<Long>> uploadMedicalRecordList(RequestBody body) {
        return mHttpDataSource.uploadMedicalRecordList(body);
    }

    @Override
    public Observable<ListResponse<ImmuneBatchEntity>> downloadImmuneBatchList(Map<String, String> map) {
        return mHttpDataSource.downloadImmuneBatchList(map);
    }

    @Override
    public Observable<CollectResponse> downloadImmuneBatchListSync(RequestBody body) {
        return mHttpDataSource.downloadImmuneBatchListSync(body);
    }

    @Override
    public Observable<ListResponse<Long>> uploadImmuneBatchList(RequestBody body) {
        return mHttpDataSource.uploadImmuneBatchList(body);
    }

    @Override
    public Observable<ListResponse<ImmuneBatchDetailEntity>> downloadImmuneBatchDetailList(Map<String, String> map) {
        return mHttpDataSource.downloadImmuneBatchDetailList(map);
    }

    @Override
    public Observable<CollectResponse> downloadImmuneBatchDetailListSync(RequestBody body) {
        return mHttpDataSource.downloadImmuneBatchDetailListSync(body);
    }

    @Override
    public Observable<ListResponse<Long>> uploadImmuneBatchDetailList(RequestBody body) {
        return mHttpDataSource.uploadImmuneBatchDetailList(body);
    }

    @Override
    public Observable<CollectResponse> uploadErrorData(RequestBody body) {
        return mHttpDataSource.uploadErrorData(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 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 getToken() {
        return mLocalDataSource.getToken();
    }


}
