package com.jambotronGroup.jambotron.system;

import com.jambotronGroup.jambotron.JambotronApplication;
import com.jambotronGroup.jambotron.controllers.SystemController;
import com.jambotronGroup.jambotron.model.Bean;
import com.jambotronGroup.jambotron.model.NameValueItem;
import com.jambotronGroup.jambotron.utils.EntityLoggingConsumer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ai.image.ImagePrompt;
import org.springframework.ai.image.ImageResponse;
import org.springframework.ai.zhipuai.ZhiPuAiImageModel;
import org.springframework.ai.zhipuai.api.ZhiPuAiImageApi;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.context.ApplicationContext;
import org.springframework.core.env.Environment;
import org.springframework.data.web.config.SpringDataWebSettings;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.function.Consumer;
import java.util.stream.Collectors;

@Service
public class SystemServiceImpl implements SystemService {

    @Autowired
    private ApplicationContext applicationContext;
    @Autowired
    private Environment environment;

    @Autowired
    DataSourceProperties _dataSourceProperties;

    @Autowired
    ZhiPuAiImageModel imageClient;

    private static final Logger logger = LoggerFactory.getLogger(SystemServiceImpl.class);



    @Override
    public List<NameValueItem> getDataSourceProperties(){
        List<NameValueItem> returnValue = new ArrayList<NameValueItem>();

        returnValue.add(new NameValueItem("Username", _dataSourceProperties.getUsername()));
        returnValue.add(new NameValueItem("Password", _dataSourceProperties.getPassword()));
        returnValue.add(new NameValueItem("Url", _dataSourceProperties.getUrl()));


        return returnValue;
    }

    @Override
    public List<NameValueItem> getImage(){
        List<NameValueItem> returnValue = new ArrayList<NameValueItem>();

        ZhiPuAiImageApi api = new ZhiPuAiImageApi("628447c8c65845a48a7226391464a2ea.Dw8ci6TiW0BRF5LI");

        ZhiPuAiImageModel imageClient = new ZhiPuAiImageModel(api);

        //imageClient.


        //imageClient.getDefaultOptions().setUser("44621746535994937");

        ImageResponse imageResponse = imageClient.call(new ImagePrompt("Ukr zaliznicya station in the style cubisme"));



        returnValue.add(new NameValueItem("response", "imageResponse."));

        return returnValue;
    }

    @Override
    public List<Bean> getLodedBeans() {
        // Get all bean names from the application context and map them to Bean objects
        List<Bean> beans = Arrays.stream(applicationContext.getBeanDefinitionNames()).map(
                name -> new Bean(name, applicationContext.getType(name).getName(),applicationContext.getApplicationName()))

                .toList();
        beans.forEach(new EntityLoggingConsumer(logger));
        return beans;
    }

    @Override
    public List<Bean> getLodedCustomBeans() {
        List<Bean> beans = getLodedBeans();

        beans = beans.stream()
                .filter(bean -> bean.getName() != null &&
                        bean.getType().contains(
                                environment.getRequiredProperty("spring.application.name")
                        )
                )
                .sorted((o1, o2) -> o1.getName().compareTo(o2.getName()))
                .collect(Collectors.toList());

        return beans;
    }
}