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

import cn.reghao.oss.web.app.db.repository.UploadChannelRepository;
import cn.reghao.oss.web.app.model.po.UploadChannel;
import cn.reghao.oss.web.app.model.vo.KeyValue;
import cn.reghao.oss.web.app.service.UserNodeService;
import cn.reghao.oss.web.util.DefaultSetting;
import cn.reghao.oss.web.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 StoreChannelPageController {
    private final UploadChannelRepository uploadChannelRepository;
    private final UserNodeService userNodeService;

    public StoreChannelPageController(UploadChannelRepository uploadChannelRepository, UserNodeService userNodeService) {
        this.uploadChannelRepository = uploadChannelRepository;
        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();
        Page<UploadChannel> page = uploadChannelRepository.findAll(pageRequest);

        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 deployPage(Model model) {
        List<KeyValue> objectTypes = new ArrayList<>();
        objectTypes.add(new KeyValue("application/octet-stream", "other"));
        objectTypes.add(new KeyValue("image/jpeg", "image"));
        objectTypes.add(new KeyValue("text/plain", "text"));
        objectTypes.add(new KeyValue("video/mp4", "video"));
        objectTypes.add(new KeyValue("audio/wav", "audio"));
        model.addAttribute("objectTypes", objectTypes);

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

        List<KeyValue> list = userNodeService.getUserStoreNodes().stream()
                .map(storeNode -> {
                    String domain = storeNode.getDomain();
                    return new KeyValue(domain, domain);
                }).collect(Collectors.toList());

        model.addAttribute("sizeList", list1);
        model.addAttribute("storeNodes", list);
        return "/channel/add";
    }
}
