/*-
 * ~~~~~~licensing~~~~~~
 * schema-soap
 * ==========
 * Copyright (C) 2020 - 2021 EmDev LLC
 * ==========
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ~~~~~~/licensing~~~~~~
 */
package ru.entaxy.esb.system.registry.schema.soap;

import ru.entaxy.esb.system.registry.schema.api.ResourceService;
import ru.entaxy.esb.system.registry.schema.api.entity.Resource;
import ru.entaxy.esb.system.registry.schema.api.entity.ResourceInfo;
import ru.entaxy.esb.system.registry.schema.soap.cxf.*;
import ru.entaxy.esb.system.registry.schema.soap.mapper.ResourceMapper;

import java.util.Optional;

public class RegistrySchemaServiceImpl implements RegistrySchemaService {

    private ResourceService resourceService;
    private ResourceMapper resourceMapper;

    @Override
    public String loadResource(LoadResourceType request, String header) {
        try {
            Resource resource = resourceMapper.toResource(request);
            resource.setCreatedBy(header);
            return String.valueOf(resourceService.loadResource(resource));
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage(), e);
        }
    }

    @Override
    public String reloadResource(ResourceType request, String header) {
        try {
            Resource resource = resourceMapper.toEditedResource(request);
            resource.setEditedBy(header);
            return String.valueOf(resourceService.reloadResource(resource));
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage(), e);
        }
    }

    @Override
    public String removeResource(long request) {
        try {
            resourceService.removeResource(request);
            return String.valueOf(request);
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage(), e);
        }
    }

    @Override
    public FullResourceType getResource(long id) {
        try {
            Optional<Resource> resource = resourceService.getResource(id);
            return resource.map(value -> resourceMapper.toFullResourceType(value)).orElse(null);
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage(), e);
        }
    }

    @Override
    public String loadResourceInfo(ResourceInfoType request, String header) {
        try {
            ResourceInfo resourceInfo = resourceMapper.toResourceInfo(request);
            resourceInfo.setCreatedBy(header);
            return String.valueOf(resourceService.loadResourceInfo(resourceInfo));
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage(), e);
        }
    }

    @Override
    public String editResourceInfo(EditedResourceInfoFullType request, String header) {
        try {
            ResourceInfo resourceInfo = resourceMapper.editedResourceInfoFullTypeResourceInfo(request);
            resourceInfo.setEditedBy(header);
            return String.valueOf(resourceService.editResourceInfo(resourceInfo));
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage(), e);
        }
    }

    @Override
    public String removeResourceInfo(String request) {
        try {
            resourceService.removeResourceInfo(Long.valueOf(request));
            return request;
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage(), e);
        }
    }

    @Override
    public ResourceInfoFullType getResourceInfo(String id) {
        try {
            Optional<ResourceInfo> resource = resourceService.getResourceInfo(Long.valueOf(id));
            return resource.map(value -> resourceMapper.toResourceInfoFullType(value)).orElse(null);
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage(), e);
        }
    }

    @Override
    public GetResourceInfoList getResourceInfoList(GetResourceInfoListRequest request) {
        try {
            GetResourceInfoList list = new GetResourceInfoList();
            for (ResourceInfo resourceInfo : resourceService.getListResourceInfo()) {
                list.getResourceInfoFullType().add(resourceMapper.toResourceInfoFullType(resourceInfo));
            }
            return list;
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage(), e);
        }
    }

    @Override
    public GetResourceInfoList getResourceInfoListByName(GetResourceInfoListByNameRequest request) {
        try {
            GetResourceInfoList list = new GetResourceInfoList();
            for (ResourceInfo resourceInfo : resourceService.getListResourceInfoByName(request.getName())) {
                list.getResourceInfoFullType().add(resourceMapper.toResourceInfoFullType(resourceInfo));
            }
            return list;
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage(), e);
        }
    }

    @Override
    public GetResourceInfoList getResourceInfoListByNamespace(GetResourceInfoListByNamespaceRequest request) {
        try {
            GetResourceInfoList list = new GetResourceInfoList();
            for (ResourceInfo resourceInfo : resourceService.getListResourceInfoByNamespace(request.getNamespace())) {
                list.getResourceInfoFullType().add(resourceMapper.toResourceInfoFullType(resourceInfo));
            }
            return list;
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage(), e);
        }
    }

    public void setResourceService(ResourceService resourceService) {
        this.resourceService = resourceService;
    }

    public void setResourceMapper(ResourceMapper resourceMapper) {
        this.resourceMapper = resourceMapper;
    }
}
