package org.dromara.system.controller;

import java.io.IOException;
import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.system.domain.vo.VsNavBottomIconVo;
import org.dromara.system.domain.bo.VsNavBottomIconBo;
import org.dromara.system.service.IVsNavBottomIconService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 底部图标导航表管理
 *
 * @author system
 * @date 2025-01-20
 */
@Slf4j
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/scenic/navBottomIcon")
public class VsNavBottomIconController extends BaseController {

    private final IVsNavBottomIconService vsNavBottomIconService;

    /**
     * 查询底部图标导航表列表
     */
    @SaCheckPermission("scenic:navBottomIcon:list")
    @GetMapping("/list")
    public TableDataInfo<VsNavBottomIconVo> list(VsNavBottomIconBo bo, PageQuery pageQuery) {
        return vsNavBottomIconService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出底部图标导航表列表
     */
    @SaCheckPermission("scenic:navBottomIcon:export")
    @Log(title = "底部图标导航表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(VsNavBottomIconBo bo, HttpServletResponse response) {
        try {
            List<VsNavBottomIconVo> list = vsNavBottomIconService.queryList(bo);
            ExcelUtil.exportExcel(list, "底部图标导航表", VsNavBottomIconVo.class, response);
        } catch (Exception e) {
            // 如果导出过程中出现异常，重置Content-Type并返回错误信息
            response.reset();
            response.setContentType("application/json;charset=UTF-8");
            response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            try {
                response.getWriter().write("{\"code\":500,\"msg\":\"导出失败：" + e.getMessage() + "\"}");
            } catch (IOException ex) {
                // 忽略写入错误
            }
        }
    }

    /**
     * 获取底部图标导航表详细信息
     *
     * @param iconId 主键
     */
    @SaCheckPermission("scenic:navBottomIcon:query")
    @GetMapping("/{iconId}")
    public R<VsNavBottomIconVo> getInfo(@NotNull(message = "主键不能为空")
                                        @PathVariable Long iconId) {
        return R.ok(vsNavBottomIconService.queryById(iconId));
    }

    /**
     * 新增底部图标导航表
     */
    @SaCheckPermission("scenic:navBottomIcon:add")
    @Log(title = "底部图标导航表", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody VsNavBottomIconBo bo) {
        return toAjax(vsNavBottomIconService.insertByBo(bo));
    }

    /**
     * 修改底部图标导航表
     */
    @SaCheckPermission("scenic:navBottomIcon:edit")
    @Log(title = "底部图标导航表", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody VsNavBottomIconBo bo) {
        return toAjax(vsNavBottomIconService.updateByBo(bo));
    }

    /**
     * 删除底部图标导航表
     *
     * @param ids 主键串
     */
    @SaCheckPermission("scenic:navBottomIcon:remove")
    @Log(title = "底部图标导航表", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(vsNavBottomIconService.deleteWithValidByIds(List.of(ids), true));
    }

    /**
     * 获取启用的底部图标导航列表
     */
    @GetMapping("/enabled")
    public R<List<VsNavBottomIconVo>> getEnabledIconList() {
        return R.ok(vsNavBottomIconService.getEnabledIconList());
    }

    /**
     * 根据业务类型获取图标导航列表
     */
    @GetMapping("/byBizType/{bizType}")
    public R<List<VsNavBottomIconVo>> getIconListByBizType(@PathVariable Integer bizType) {
        return R.ok(vsNavBottomIconService.getIconListByBizType(bizType));
    }

    /**
     * 保存颜色设置
     */
    @PostMapping("/colorSettings")
    public R<Void> saveColorSettings(@RequestBody ColorSettingsRequest request) {
        return toAjax(vsNavBottomIconService.saveColorSettings(request.getSelectedTextColor(), request.getUnselectedTextColor()));
    }

    /**
     * 获取颜色设置
     */
    @GetMapping("/colorSettings")
    public R<java.util.Map<String, String>> getColorSettings() {
        return R.ok(vsNavBottomIconService.getColorSettings());
    }

    /**
     * 颜色设置请求对象
     */
    public static class ColorSettingsRequest {
        private String selectedTextColor;
        private String unselectedTextColor;

        public String getSelectedTextColor() {
            return selectedTextColor;
        }

        public void setSelectedTextColor(String selectedTextColor) {
            this.selectedTextColor = selectedTextColor;
        }

        public String getUnselectedTextColor() {
            return unselectedTextColor;
        }

        public void setUnselectedTextColor(String unselectedTextColor) {
            this.unselectedTextColor = unselectedTextColor;
        }
    }

    /**
     * 颜色设置响应对象
     */
    public static class ColorSettingsResponse {
        private String selectedTextColor;
        private String unselectedTextColor;

        public ColorSettingsResponse(String selectedTextColor, String unselectedTextColor) {
            this.selectedTextColor = selectedTextColor;
            this.unselectedTextColor = unselectedTextColor;
        }

        public String getSelectedTextColor() {
            return selectedTextColor;
        }

        public void setSelectedTextColor(String selectedTextColor) {
            this.selectedTextColor = selectedTextColor;
        }

        public String getUnselectedTextColor() {
            return unselectedTextColor;
        }

        public void setUnselectedTextColor(String unselectedTextColor) {
            this.unselectedTextColor = unselectedTextColor;
        }
    }
}
