package com.zhongshu.card.server.core.controller.setting;

import com.github.microservice.auth.security.annotations.ResourceAuth;
import com.github.microservice.auth.security.type.AuthType;
import com.github.microservice.net.ResultContent;
import com.google.common.collect.Lists;
import com.zhongshu.card.client.model.setting.PlatFormBasicInfo;
import com.zhongshu.card.client.model.setting.PlatFormLoginConfig;
import com.zhongshu.card.client.type.setting.SettingType;
import com.zhongshu.card.server.core.domain.setting.PlatFormConfigInfo;
import com.zhongshu.card.server.core.service.setting.PlatFormConfigInfoService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * 系统设置
 */
@RestController
@RequestMapping("/setting")
@Tag(name = "系统设置")
public class PlatFormConfigInfoController {

    @Autowired
    private PlatFormConfigInfoService platFormConfigInfoService;

    @ResourceAuth(value = "user", type = AuthType.User)
    @Operation(summary = "保存平台基本信息", description = "保存平台基本信息")
    @RequestMapping(value = "saveBasicInfo", method = {RequestMethod.POST})
    public ResultContent saveBasicInfo(@RequestBody @Valid PlatFormBasicInfo param) {
        platFormConfigInfoService.saveEntity(param, SettingType.BasicInfo);
        return ResultContent.buildSuccess();
    }

    @Operation(summary = "得到平台基本信息", description = "得到平台基本信息")
    @RequestMapping(value = "getBasicInfo", method = {RequestMethod.GET})
    public ResultContent getBasicInfo() {
        PlatFormConfigInfo entity = platFormConfigInfoService.getEntity(SettingType.BasicInfo);
        PlatFormBasicInfo basicInfo = null;
        if (ObjectUtils.isNotEmpty(entity)) {
            Object info = entity.getInfo();
            if (info != null) {
                basicInfo = (PlatFormBasicInfo) info;
            }
        }
        return ResultContent.buildSuccess(basicInfo);
    }

    @ResourceAuth(value = "user", type = AuthType.User)
    @Operation(summary = "保存登录注册", description = "保存登录注册")
    @RequestMapping(value = "saveLoginConfig", method = {RequestMethod.POST})
    public ResultContent saveLoginConfig(@RequestBody @Valid PlatFormLoginConfig param) {
        platFormConfigInfoService.saveEntity(param, SettingType.LoginConfig);
        return ResultContent.buildSuccess();
    }

    @ResourceAuth(value = "user", type = AuthType.User)
    @Operation(summary = "得到登录注册", description = "得到登录注册")
    @RequestMapping(value = "getLoginConfig", method = {RequestMethod.GET})
    public ResultContent getLoginConfig() {
        PlatFormConfigInfo entity = platFormConfigInfoService.getEntity(SettingType.LoginConfig);
        PlatFormLoginConfig basicInfo = null;
        if (ObjectUtils.isNotEmpty(entity)) {
            Object info = entity.getInfo();
            if (info != null) {
                basicInfo = (PlatFormLoginConfig) info;
            }
        }
        return ResultContent.buildSuccess(basicInfo);
    }

}
