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

import cn.reghao.oss.api.constant.ObjectScope;
import cn.reghao.oss.api.constant.ObjectType;
import cn.reghao.oss.console.account.service.UserContext;
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.model.vo.UserNodeVO;
import cn.reghao.oss.console.app.service.UploadChannelService;
import cn.reghao.oss.console.app.service.UserNodeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * @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 {
        /*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);*/
        int loginUser = UserContext.getUserId();
        List<UploadChannelVo> list = uploadChannelService.getUploadChannels(loginUser);
        //model.addAttribute("page", page);
        model.addAttribute("list", list);
        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+"", "2MB"));
        sizeList.add(new KeyValue(1024L*1024*10+"", "10MB"));
        sizeList.add(new KeyValue(1024L*1024*100+"", "100MB"));
        sizeList.add(new KeyValue(1024L*1024*1024+"", "1GB"));
        sizeList.add(new KeyValue(1024L*1024*1024*10+"", "10GB"));
        sizeList.add(new KeyValue(1024L*1024*1024*20+"", "20GB"));

        int loginUser = UserContext.getUserId();
        List<UserNodeVO> storeNodes = userNodeService.getUserStoreNodes(loginUser);

        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";
    }
}
