package com.eshore.gringotts.web.domain.controller;

import cn.hutool.core.util.StrUtil;
import com.eshore.gringotts.web.configuration.HostConfiguration;
import com.eshore.gringotts.web.configuration.amis.AmisResponse;
import com.eshore.gringotts.web.domain.base.controller.SimpleControllerSupport;
import com.eshore.gringotts.web.domain.base.entity.SimpleListItem;
import com.eshore.gringotts.web.domain.base.entity.SimpleSaveItem;
import com.eshore.gringotts.web.domain.entity.Ware;
import com.eshore.gringotts.web.domain.service.DataFileService;
import com.eshore.gringotts.web.domain.service.DataResourceService;
import com.eshore.gringotts.web.domain.service.WareService;
import com.fasterxml.jackson.core.JsonProcessingException;
import java.time.LocalDateTime;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.collections.api.list.ImmutableList;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author lanyuanxiaoyao
 * @version 2024-12-13
 */
@Slf4j
@RestController
@RequestMapping("ware")
public class WareController extends SimpleControllerSupport<Ware, WareController.SaveItem, WareController.ListItem, WareController.DetailItem> {
    private final HostConfiguration hostConfiguration;
    private final WareService wareService;
    private final DataResourceService dataResourceService;
    private final DataFileService dataFileService;

    public WareController(HostConfiguration hostConfiguration, WareService service, DataResourceService dataResourceService, DataFileService dataFileService) {
        super(service);
        this.hostConfiguration = hostConfiguration;
        this.wareService = service;
        this.dataResourceService = dataResourceService;
        this.dataFileService = dataFileService;
    }

    @GetMapping(LIST + "_public")
    public AmisResponse<ImmutableList<ListItem>> listPublic() {
        return AmisResponse.responseSuccess(wareService.listPublic().collect(this::toListItem));
    }

    @GetMapping("/submit/{id}")
    public AmisResponse<Object> submit(@PathVariable Long id) throws JsonProcessingException {
        wareService.submit(id);
        return AmisResponse.responseSuccess();
    }

    @GetMapping("/retract/{id}")
    public AmisResponse<Object> retract(@PathVariable Long id) {
        wareService.retract(id);
        return AmisResponse.responseSuccess();
    }

    @Override
    protected Ware fromSaveItem(SaveItem saveItem) {
        Ware ware = new Ware();
        ware.setId(saveItem.getId());
        ware.setResource(dataResourceService.detailOrThrow(saveItem.getResourceId()));
        ware.setName(saveItem.getName());
        ware.setDescription(saveItem.getDescription());
        ware.setIcon(dataFileService.detailOrThrow(saveItem.getIconId()));
        ware.setContent(saveItem.getContent());
        return ware;
    }

    @Override
    protected ListItem toListItem(Ware entity) {
        ListItem item = new ListItem();
        item.setId(entity.getId());
        item.setName(entity.getName());
        item.setDescription(entity.getDescription());
        item.setIcon(StrUtil.format("{}/upload/download/{}", hostConfiguration.getPrefix(), entity.getIcon().getId()));
        item.setState(entity.getState().name());
        item.setResourceId(entity.getResource().getId());
        item.setCreatedTime(entity.getCreatedTime());
        item.setCreatedUsername(entity.getCreatedUser().getUsername());
        return item;
    }

    @Override
    protected DetailItem toDetailItem(Ware entity) {
        DetailItem item = new DetailItem();
        item.setId(entity.getId());
        item.setResourceId(entity.getResource().getId());
        item.setResourceName(entity.getResource().getName());
        item.setName(entity.getName());
        item.setDescription(entity.getDescription());
        item.setIconId(entity.getIcon().getId());
        item.setContent(entity.getContent());
        item.setIcon(StrUtil.format("{}/upload/download/{}", hostConfiguration.getPrefix(), entity.getIcon().getId()));
        item.setCreatedTime(entity.getCreatedTime());
        item.setCreatedUsername(entity.getCreatedUser().getUsername());
        item.setModifiedTime(entity.getModifiedTime());
        item.setModifiedUsername(entity.getModifiedUser().getUsername());
        return item;
    }

    @Data
    @EqualsAndHashCode(callSuper = true)
    public static class SaveItem extends SimpleSaveItem<Ware> {
        private Long resourceId;
        private String name;
        private String description;
        private Long iconId;
        private String content;
    }

    @Data
    @EqualsAndHashCode(callSuper = true)
    public static class ListItem extends SimpleListItem<Ware> {
        private String name;
        private String description;
        private String state;
        private String icon;
        private Long resourceId;
    }

    @Data
    @EqualsAndHashCode(callSuper = true)
    public static class DetailItem extends SaveItem {
        private String icon;
        private String resourceName;
        private LocalDateTime createdTime;
        private String createdUsername;
        private LocalDateTime modifiedTime;
        private String modifiedUsername;
    }
}
