import { LookDomType, UserTableListType } from '@/types'
import { MessageType } from '@/utils/message'

type DownImgType = {
  可见: '可见' | '不可见'
  图片: '原图和缩略图' | '缩略图'
}

// 初始化状态
const initState = {
  // 所有图片点击预览查看大图
  lookBigImg: {
    url: '',
    show: false
  },
  // 查看视频、音频、模型
  lookDom: {
    src: '',
    type: ''
  } as LookDomType,

  // antd轻提示(兼容360浏览器)
  message: {
    txt: '',
    type: 'info',
    duration: 3
  } as MessageType,
  // 上传文件点击取消
  closeUpFile: {
    fu: () => {},
    state: false
  },
  // 用户信息
  userInfo: {} as UserTableListType,
  // 修改密码
  passEditShow: false,
  // 下载图片的权限
  downImg: {
    可见: '不可见',
    图片: '缩略图'
  } as DownImgType
}

// 定义 action 类型
type LayoutActionType =
  | { type: 'layout/lookBigImg'; payload: { url: string; show: boolean } }
  | { type: 'layout/lookDom'; payload: LookDomType }
  | { type: 'layout/message'; payload: MessageType }
  | {
      type: 'layout/closeUpFile'
      payload: {
        fu: () => void
        state: boolean
      }
    }
  | { type: 'layout/userInfo'; payload: UserTableListType }
  | { type: 'layout/passEditShow'; payload: boolean }
  | { type: 'layout/downImg'; payload: DownImgType }

// 频道 reducer
export default function layoutReducer(state = initState, action: LayoutActionType) {
  switch (action.type) {
    // 所有图片点击预览查看大图
    case 'layout/lookBigImg':
      return { ...state, lookBigImg: action.payload }
    // 查看视频
    case 'layout/lookDom':
      return { ...state, lookDom: action.payload }

    // antd轻提示(兼容360浏览器)
    case 'layout/message':
      return { ...state, message: action.payload }
    // 上传文件点击取消
    case 'layout/closeUpFile':
      return { ...state, closeUpFile: action.payload }
    // 设置用户信息
    case 'layout/userInfo':
      return { ...state, userInfo: action.payload }
    // 修改密码
    case 'layout/passEditShow':
      return { ...state, passEditShow: action.payload }
    // 下载图片的权限
    case 'layout/downImg':
      return { ...state, downImg: action.payload }
    default:
      return state
  }
}
