package com.slodon.b2b2c.controller.video.front;

import com.slodon.b2b2c.core.controller.BaseController;
import com.slodon.b2b2c.core.response.JsonResult;
import com.slodon.b2b2c.core.response.SldResponse;
import com.slodon.b2b2c.core.util.AssertUtil;
import com.slodon.b2b2c.model.video.VideoSettingModel;
import com.slodon.b2b2c.video.pojo.VideoSetting;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;

@Api(tags = "front-短视频配置")
@RestController
@RequestMapping("v3/video/front/video/setting")
public class FrontVideoSettingController extends BaseController {

    @Resource
    private VideoSettingModel videoSettingModel;

    @ApiOperation("获取配置信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "str", value = "要查询的键组合，以逗号分割", required = true, paramType = "query")
    })
    @GetMapping("getSettingList")
    public JsonResult<List<VideoSetting>> getSettingList(String str) {
        AssertUtil.isTrue(StringUtils.isEmpty(str), "请输入正确的参数");

        List<VideoSetting> list = getSetting(str);
        AssertUtil.notNull(list, "请输入正确的参数");
        return SldResponse.success(list);
    }

    /**
     * 获取Setting参数
     *
     * @param str 以逗号分割要查询的键值name
     * @return
     */
    private List<VideoSetting> getSetting(String str) {
        if (StringUtils.isEmpty(str)) {
            return null;
        }
        List<VideoSetting> list = new LinkedList<>();
        String[] split = str.split(",");
        for (String s : split) {
            if (StringUtils.isEmpty(s)) {
                continue;
            }
            VideoSetting setting = videoSettingModel.getVideoSettingBySettingName(s);
            if (setting == null) {
                continue;
            }
            list.add(setting);
        }
        return list;
    }

}
