package com.phlx.wool.data;

import android.annotation.SuppressLint;
import android.app.Application;

import androidx.annotation.NonNull;
import androidx.annotation.VisibleForTesting;
import androidx.lifecycle.ViewModel;
import androidx.lifecycle.ViewModelProvider;

import com.phlx.wool.ui.inventory.InventoryVM;
import com.phlx.wool.ui.login.LoginVM;
import com.phlx.wool.ui.main.MainVM;
import com.phlx.wool.ui.splash.SplashVM;
import com.phlx.wool.ui.sync.vm.ClearVM;
import com.phlx.wool.ui.sync.vm.DownloadVM;
import com.phlx.wool.ui.sync.vm.UploadVM;
import com.phlx.wool.ui.work.harmless.HarmlessVM;
import com.phlx.wool.ui.work.mark.MarkingVM;
import com.phlx.wool.ui.work.quarantine.QuarantineVM;
import com.phlx.wool.ui.work.unit.UnitVM;


/**
 *
 */
public class VMFactory extends ViewModelProvider.NewInstanceFactory {
    @SuppressLint("StaticFieldLeak")
    private static volatile VMFactory INSTANCE;
    private final Application mApplication;
    private Repository repository;

    public static VMFactory getInstance(Application application) {
        if (INSTANCE == null) {
            synchronized (VMFactory.class) {
                if (INSTANCE == null) {
                    INSTANCE = new VMFactory(application, Injection.provideRepository());
                }
            }

        }
        return INSTANCE;
    }

    public void refreshRepository() {
        if (repository != null) {
            repository = null;
            Injection.destroyRepository();
            repository = Injection.provideRepository();
        }
    }

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

    private VMFactory(Application application, Repository repository) {
        this.mApplication = application;
        this.repository = repository;
    }

    @NonNull
    @Override
    public <T extends ViewModel> T create(@NonNull Class<T> modelClass) {
        if (modelClass.isAssignableFrom(LoginVM.class)) {
            return (T) new LoginVM(mApplication, repository);
        } else if (modelClass.isAssignableFrom(SplashVM.class)) {
            return (T) new SplashVM(mApplication, repository);
        }else if (modelClass.isAssignableFrom(MainVM.class)) {
            return (T) new MainVM(mApplication, repository);
        } else if (modelClass.isAssignableFrom(MarkingVM.class)) {
            return (T) new MarkingVM(mApplication, repository);
        } else if (modelClass.isAssignableFrom(QuarantineVM.class)) {
            return (T) new QuarantineVM(mApplication, repository);
        } else if (modelClass.isAssignableFrom(HarmlessVM.class)) {
            return (T) new HarmlessVM(mApplication, repository);
        } else if (modelClass.isAssignableFrom(InventoryVM.class)) {
            return (T) new InventoryVM(mApplication, repository);
        }else if (modelClass.isAssignableFrom(UnitVM.class)) {
            return (T) new UnitVM(mApplication, repository);
        }else if (modelClass.isAssignableFrom(ClearVM.class)) {
            return (T) new ClearVM(mApplication, repository);
        }else if (modelClass.isAssignableFrom(UploadVM.class)) {
            return (T) new UploadVM(mApplication, repository);
        }else if (modelClass.isAssignableFrom(DownloadVM.class)) {
            return (T) new DownloadVM(mApplication, repository);
        }
        throw new IllegalArgumentException("Unknown ViewModel class: " + modelClass.getName());
    }
}
