package com.vetti.hotake.service.impl;

import com.vetti.common.core.service.BaseServiceImpl;
import com.vetti.common.enums.FillTypeEnum;
import com.vetti.hotake.domain.HotakeAccessibility;
import com.vetti.hotake.mapper.HotakeAccessibilityMapper;
import com.vetti.hotake.service.IHotakeAccessibilityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 用户无障碍设置Service业务层处理
 *
 * @author vetti
 * @date 2026-02-01
 */
@Service
public class HotakeAccessibilityServiceImpl extends BaseServiceImpl implements IHotakeAccessibilityService {

    @Autowired
    private HotakeAccessibilityMapper hotakeAccessibilityMapper;

    /**
     * 查询用户无障碍设置
     *
     * @param id 用户无障碍设置主键
     * @return 用户无障碍设置
     */
    @Transactional(readOnly = true)
    @Override
    public HotakeAccessibility selectHotakeAccessibilityById(Integer id) {
        return hotakeAccessibilityMapper.selectHotakeAccessibilityById(id);
    }

    /**
     * 查询用户无障碍设置列表
     *
     * @param hotakeAccessibility 用户无障碍设置
     * @return 用户无障碍设置
     */
    @Transactional(readOnly = true)
    @Override
    public List<HotakeAccessibility> selectHotakeAccessibilityList(HotakeAccessibility hotakeAccessibility) {
        return hotakeAccessibilityMapper.selectHotakeAccessibilityList(hotakeAccessibility);
    }

    /**
     * 新增用户无障碍设置
     *
     * @param hotakeAccessibility 用户无障碍设置
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public HotakeAccessibility insertHotakeAccessibility(HotakeAccessibility hotakeAccessibility) {
        fill(FillTypeEnum.INSERT.getCode(), hotakeAccessibility);
        hotakeAccessibilityMapper.insertHotakeAccessibility(hotakeAccessibility);
        return hotakeAccessibility;
    }

    /**
     * 修改用户无障碍设置
     *
     * @param hotakeAccessibility 用户无障碍设置
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public HotakeAccessibility updateHotakeAccessibility(HotakeAccessibility hotakeAccessibility) {
        fill(FillTypeEnum.UPDATE.getCode(), hotakeAccessibility);
        hotakeAccessibilityMapper.updateHotakeAccessibility(hotakeAccessibility);
        return hotakeAccessibility;
    }

    /**
     * 批量删除用户无障碍设置
     *
     * @param ids 需要删除的用户无障碍设置主键
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int deleteHotakeAccessibilityByIds(Integer[] ids) {
        return hotakeAccessibilityMapper.deleteHotakeAccessibilityByIds(ids);
    }

    /**
     * 删除用户无障碍设置信息
     *
     * @param id 用户无障碍设置主键
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int deleteHotakeAccessibilityById(Integer id) {
        return hotakeAccessibilityMapper.deleteHotakeAccessibilityById(id);
    }

    /**
     * 获取当前登录用户的无障碍设置
     * 如果不存在则自动创建默认设置
     *
     * @param username 用户名
     * @return 用户无障碍设置
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public HotakeAccessibility getCurrentUserAccessibility(String username) {
        // 查询当前用户的设置
        HotakeAccessibility query = new HotakeAccessibility();
        query.setCreateBy(username);
        List<HotakeAccessibility> list = hotakeAccessibilityMapper.selectHotakeAccessibilityList(query);
        
        // 如果存在，返回第一条
        if (list != null && !list.isEmpty()) {
            return list.get(0);
        }
        
        // 如果不存在，创建默认设置
        HotakeAccessibility defaultAccessibility = new HotakeAccessibility();
        // 设置默认值（根据数据库表定义）
        defaultAccessibility.setColorMode("light");  // 默认明亮模式
        defaultAccessibility.setHighContrastMode(0);  // 默认关闭
        defaultAccessibility.setReduceMotion(0);  // 默认关闭
        defaultAccessibility.setFontSize("medium");  // 默认中等字体
        defaultAccessibility.setScreenReaderSupport(1);  // 默认开启
        defaultAccessibility.setAudioDescriptions(1);  // 默认开启
        defaultAccessibility.setEnhancedKeyboardNavigation(0);  // 默认关闭
        defaultAccessibility.setFocusIndicators(0);  // 默认关闭
        defaultAccessibility.setLanguage("en");  // 默认英语
        
        // 保存并返回
        return insertHotakeAccessibility(defaultAccessibility);
    }
}
