package com.example.springip.lab2.service;

import com.example.springip.lab2.domains.OperationInterface;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;

@Service
public class OperationService {
    private final ApplicationContext applicationContext;

    public OperationService(ApplicationContext applicationContext) {
        this.applicationContext = applicationContext;
    }

    public Object sum(Object o, Object o2, String type){

        OperationInterface operationInterface = (OperationInterface) applicationContext.getBean(type);
        if (type.equals("num"))
            return operationInterface.sum(Integer.parseInt(o.toString()),Integer.parseInt(o2.toString()));
        return operationInterface.sum(o, o2);
    }

    public Object mul(Object o, Object o2, String type){

        OperationInterface operationInterface = (OperationInterface) applicationContext.getBean(type);
        if (type.equals("num"))
            return operationInterface.mul(Integer.parseInt(o.toString()), Integer.parseInt(o2.toString()));
        return operationInterface.mul(o, Integer.parseInt(o2.toString()));
    }

    public Object length(Object o, String type){

        OperationInterface operationInterface = (OperationInterface) applicationContext.getBean(type);
        if (type.equals("num"))
            return operationInterface.length(Integer.parseInt(o.toString()));
        return operationInterface.length(o);
    }

    public Object startsWith(Object o, Object o2, String type){

        OperationInterface operationInterface = (OperationInterface) applicationContext.getBean(type);
        if (type.equals("num"))
            return operationInterface.startsWith(Integer.parseInt(o.toString()), Integer.parseInt(o2.toString()));
        return operationInterface.startsWith(o,o2);
    }


}
