
import Taro from '@tarojs/taro'
import { UserInfoActionType } from '../constants/userInfo'

export interface UserParam {
  department_code?: string
  department_id?: string
  department_name?: string
  duty_name?: string
  physical_warehouse?: number
  physical_warehouse_name?: string
  token?: string
  user_code?: number
  user_id?: string
  user_name?: number
  avatar_url?: string
  phone?: string
}

export interface UserAdminParam {
  avatar_url?: string
  open_id?: string
  physical_warehouse?: number
  physical_warehouse_name?: string
  union_id?: string
  user_code?: string
  user_id?: number
  user_name?: string
  wechat_user_open_id?: number
  is_authorize_name?: boolean
  is_authorize_phone?: boolean
  phone?: string
  authentication_status?: number
  authentication_status_name: string
  company_id?: number
  company_name?: string
  founder_user_id?: string
  founder_user_name?: string
  order_access_status?: number
  order_access_status_name?: string
  purchaser_type?: number
  purchaser_type_name?: string
}

export interface SortCodeParam {
  shareShortDetail?: { title: string; code: string; img: string } // 详情分享页面短码
  shareShortPage?: { title: string; code: string; img: string } // 右上角分享页面短码
}

export interface DataParam {
  token?: string
  session_key?: string
  userInfo: UserParam
  adminUserInfo: UserAdminParam
  sort_code: SortCodeParam
  logingStatus?: boolean // 登录状态 true 正在登录
}

export interface UserInfoAction {
  type: UserInfoActionType
  data?: DataParam
}

const INIT_USER = {
  userInfo: Taro.getStorageSync('userInfo') ? JSON.parse(Taro.getStorageSync('userInfo')) : null,
  adminUserInfo: Taro.getStorageSync('adminUserInfo') ? JSON.parse(Taro.getStorageSync('adminUserInfo')) : null,
  token: Taro.getStorageSync('token') || '',
  session_key: Taro.getStorageSync('session_key') || '',
  sort_code: Taro.getStorageSync('sort_code') ? JSON.parse(Taro.getStorageSync('sort_code')) : null,
  logingStatus: false,
}

export default (state = INIT_USER, action: UserInfoAction) => {
  const { type, data } = action
  switch (type) {
    case UserInfoActionType.SET_USERINFO:
      Taro.setStorageSync('userInfo', JSON.stringify(data?.userInfo))
      return { ...state, ...data }
    case UserInfoActionType.SET_ADMINUSERINFO:
      Taro.setStorageSync('adminUserInfo', JSON.stringify(data?.adminUserInfo))
      return { ...state, ...data }
    case UserInfoActionType.SET_TOKEN:
      Taro.setStorageSync('token', data?.token)
      return { ...state, ...data }
    case UserInfoActionType.SET_SESSIONKEY:
      Taro.setStorageSync('session_key', data?.session_key)
      return { ...state, ...data }
    case UserInfoActionType.SET_SORTCODE:
      Taro.setStorageSync('sort_code', JSON.stringify(data?.sort_code))
      return { ...state, ...data }
    case UserInfoActionType.CLEAR_TOKEN:
      Taro.removeStorageSync('token')
      return { ...state, token: '' }
    case UserInfoActionType.CLEAR_SESSIONKEY:
      Taro.removeStorageSync('session_key')
      return { ...state, session_key: '' }
    case UserInfoActionType.CLEAR_USERINFO:
      Taro.removeStorageSync('userInfo')
      return { ...state, userInfo: null }
    case UserInfoActionType.CLEAR_ADMINUSERINFO:
      Taro.removeStorageSync('adminUserInfo')
      return { ...state, adminUserInfo: null }
    case UserInfoActionType.CLEAR_SORTCODE:
      Taro.removeStorageSync('sort_code')
      return { ...state, sortCode: null }
    case UserInfoActionType.LOGIN_STATUS:
      return { ...state, ...data }
    default:
      return state
  }
}
