package co.yixiang.yshop.module.system.controller.app.dict;

import co.yixiang.yshop.framework.common.pojo.CommonResult;
import co.yixiang.yshop.framework.dict.core.util.DictFrameworkUtils;
import co.yixiang.yshop.module.system.controller.admin.dict.vo.data.DictDataRespVO;
import co.yixiang.yshop.module.system.enums.DistributorDictEnum;
import co.yixiang.yshop.module.system.service.dict.DictDataService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.annotation.security.PermitAll;
import javax.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.io.PrintWriter;

import static co.yixiang.yshop.framework.common.pojo.CommonResult.success;


@Tag(name = "用户 APP - 数据字典")
@RestController
@RequestMapping("/system/dict-data")
@Validated
public class AppDictDataController {

    @Resource
    private DictDataService dictDataService;

    @GetMapping("/agreement/{type}")
    @Operation(summary = "获得协议", description = "包括用户协议和隐私协议 弃用")
    // 无需添加权限认证，因为前端全局都需要
    @PermitAll
    public void getAgreement(@PathVariable Integer type, HttpServletResponse response) throws IOException {
        response.setHeader("Content-Type", "text/html;charset=UTF-8");
        DictDataRespVO dict = dictDataService.getAgreement(type);
        PrintWriter writer = response.getWriter();
        writer.write(dict.getValue());
        writer.flush();
        // return success(dictDataService.getAgreement(type));
    }

    @GetMapping("/get-stream")
    @Operation(summary = "获取字典流", description = "包括用户协议和隐私协议")
    // 无需添加权限认证，因为前端全局都需要
    @PermitAll
    public void getStream(@RequestParam("dictType") String dictType, @RequestParam("label") String label, HttpServletResponse response) throws IOException {
        response.setHeader("Content-Type", "text/html;charset=UTF-8");
        String value = DictFrameworkUtils.parseDictDataValue(dictType, label);
        PrintWriter writer = response.getWriter();
        writer.write(value);
        writer.flush();
    }

    @GetMapping("/get-value")
    @Operation(summary = "获取字典")
    // 无需添加权限认证，因为前端全局都需要
    @PermitAll
    public CommonResult<String> getValue(@RequestParam("dictType") String dictType, @RequestParam("label") String label, HttpServletResponse response) throws IOException {
        String value = DictFrameworkUtils.parseDictDataValue(dictType, label);
        return success(value);
    }
}
