package cn.reghao.oss.console.app.controller.page;

import cn.reghao.oss.storeapi.constant.ObjectScope;
import cn.reghao.oss.storeapi.constant.ObjectType;
import cn.reghao.oss.console.app.model.po.UploadChannel;
import cn.reghao.oss.console.app.model.vo.KeyValue;
import cn.reghao.oss.console.app.model.vo.UploadChannelVo;
import cn.reghao.oss.console.app.service.UploadChannelService;
import cn.reghao.oss.console.app.service.UserNodeService;
import cn.reghao.oss.console.util.DefaultSetting;
import cn.reghao.oss.console.util.db.PageSort;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

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

/**
 * @author reghao
 * @date 2024-02-23 15:05:30
 */
@Slf4j
@Api(tags = "上传通道页面")
@Controller
@RequestMapping("/store/channel")
public class UploadChannelPageController {
    private final UploadChannelService uploadChannelService;
    private final UserNodeService userNodeService;

    public UploadChannelPageController(UploadChannelService uploadChannelService, UserNodeService userNodeService) {
        this.uploadChannelService = uploadChannelService;
        this.userNodeService = userNodeService;
    }

    @ApiOperation(value = "上传通道列表页面")
    @GetMapping(value = "/list")
    public String index(@RequestParam(value = "env", required = false) String env,
                        @RequestParam(value = "type", required = false) String type,
                        @RequestParam(value = "appName", required = false) String appName, Model model) throws Exception {
        if (env == null) {
            env = DefaultSetting.getDefaultAppEnv();
        }

        if (type == null) {
            type = DefaultSetting.getDefaultAppType();
        }

        PageRequest pageRequest = PageSort.pageRequest();
        int pageNumber = pageRequest.getPageNumber();
        int pageSize = pageRequest.getPageSize();

        Sort sort = Sort.by(Sort.Direction.ASC, "channelId");
        PageRequest pageRequest1 = PageRequest.of(pageNumber, pageSize, sort);
        Page<UploadChannelVo> page = uploadChannelService.getUploadChannels(pageRequest1);

        model.addAttribute("env", env);
        model.addAttribute("type", type);
        model.addAttribute("page", page);
        model.addAttribute("list", page.getContent());
        return "/channel/index";
    }

    @ApiOperation(value = "新增上传通道页面")
    @GetMapping("/add")
    public String addChannelPage(Model model) {
        List<KeyValue> objectTypes = new ArrayList<>();
        for (ObjectType objectType : ObjectType.values()) {
            if (objectType.getCode() == 1000) {
                continue;
            }
            objectTypes.add(new KeyValue(objectType.getCode()+"", objectType.name()));
        }

        List<KeyValue> objectScopes = new ArrayList<>();
        for (ObjectScope objectScope : ObjectScope.values()) {
            objectScopes.add(new KeyValue(objectScope.getCode()+"", objectScope.name()));
        }

        List<KeyValue> sizeList = new ArrayList<>();
        sizeList.add(new KeyValue(1024L*1024*2+"", "2MiB"));
        sizeList.add(new KeyValue(1024L*1024*10+"", "10MiB"));
        sizeList.add(new KeyValue(1024L*1024*100+"", "100MiB"));
        sizeList.add(new KeyValue(1024L*1024*1024+"", "1GiB"));
        sizeList.add(new KeyValue(1024L*1024*1024*10+"", "10GiB"));
        sizeList.add(new KeyValue(1024L*1024*1024*20+"", "20GiB"));

        List<KeyValue> storeNodes = userNodeService.getUserStoreNodes().stream()
                .map(storeNode -> {
                    int nodeId = storeNode.getId();
                    String domain = storeNode.getDomain();
                    return new KeyValue(nodeId+"", domain);
                }).collect(Collectors.toList());

        model.addAttribute("objectTypes", objectTypes);
        model.addAttribute("objectScopes", objectScopes);
        model.addAttribute("sizeList", sizeList);
        model.addAttribute("storeNodes", storeNodes);
        return "/channel/add";
    }

    @ApiOperation(value = "修改通道可见范围")
    @GetMapping("/edit/{id}")
    public String editChannelScopePage(@PathVariable("id") Integer id, Model model) {
        UploadChannel uploadChannel = uploadChannelService.getUploadChannel(id);
        int scope = uploadChannel.getScope();
        ObjectScope objectScope0 = ObjectScope.getByCode(scope);

        List<KeyValue> objectScopes = new ArrayList<>();
        for (ObjectScope objectScope : ObjectScope.values()) {
            objectScopes.add(new KeyValue(objectScope.getCode()+"", objectScope.name()));
        }

        model.addAttribute("objectScope", objectScope0.getName());
        model.addAttribute("objectScopes", objectScopes);
        return "/channel/edit";
    }

    @ApiOperation(value = "通道文件处理")
    @GetMapping("/update/{id}")
    public String editChannelScope1Page(@PathVariable("id") Integer id, Model model) {
        UploadChannel uploadChannel = uploadChannelService.getUploadChannel(id);
        List<KeyValue> list = new ArrayList<>();
        list.add(new KeyValue("1", "处理文件"));
        list.add(new KeyValue("0", "不处理文件"));

        model.addAttribute("id", uploadChannel.getId());
        model.addAttribute("list", list);
        return "/channel/edit1";
    }
}
