package com.yami.shop.api.controller;

import cn.hutool.core.util.StrUtil;
import com.yami.shop.bean.vo.PopupConfigVO;
import com.yami.shop.service.SysConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 弹窗配置接口
 */
@RestController
@RequestMapping("/popup")
@Api(tags = "弹窗配置接口")
@AllArgsConstructor
public class PopupConfigController {

    private final SysConfigService sysConfigService;

    /**
     * 弹窗开关配置key
     */
    private static final String POPUP_SWITCH = "POPUP_SWITCH";

    /**
     * 弹窗图片地址配置key
     */
    private static final String POPUP_IMAGE_URL = "POPUP_IMAGE_URL";

    /**
     * 获取弹窗配置
     *
     * @return 弹窗配置信息
     */
    @GetMapping("/config")
    @ApiOperation(value = "获取弹窗配置", notes = "获取弹窗开关和图片地址配置")
    public ResponseEntity<PopupConfigVO> getPopupConfig() {
        PopupConfigVO vo = new PopupConfigVO();

        // 获取弹窗开关配置
        String switchValue = sysConfigService.getValue(POPUP_SWITCH);
        vo.setShowPopup(StrUtil.isNotBlank(switchValue) && Boolean.parseBoolean(switchValue));

        // 获取弹窗图片地址配置
        String imageUrl = sysConfigService.getValue(POPUP_IMAGE_URL);
        vo.setImageUrl(StrUtil.isBlank(imageUrl) ? "" : imageUrl);

        return ResponseEntity.ok(vo);
    }
}
