package com.example.tampopo_client.viewmodels;

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

import java.lang.reflect.InvocationTargetException;

public class ChatViewModelFactory implements ViewModelProvider.Factory {
    private final String userId;
    private final String token;
    private final String chatroomId;

    public ChatViewModelFactory(String userId, String token, String chatroomId) {
        this.userId = userId;
        this.token = token;
        this.chatroomId = chatroomId;
    }

    @NonNull
    @Override
    public <T extends ViewModel > T create(@NonNull Class<T> modelClass) {
        if (modelClass.isAssignableFrom(ChatViewModel.class)) {
            try {
                return modelClass.getConstructor(String.class, String.class).newInstance(userId, token);
            } catch (InvocationTargetException | IllegalAccessException | InstantiationException |
                     NoSuchMethodException e) {
                throw new RuntimeException(e);
            }
        }
        throw new IllegalStateException("作成するViewModelが異なります。");
    }
}

