
import Taro from '@tarojs/taro'
import {
  CLEAR_ADMINUSERINFO,
  CLEAR_SESSIONKEY,
  CLEAR_SORTCODE,
  CLEAR_TOKEN,
  CLEAR_USERINFO,
  LOGIN_STATUS,
  SET_ADMINUSERINFO,
  SET_SESSIONKEY,
  SET_SORTCODE,
  SET_TOKEN,
  SET_USERINFO,
} from '../constants/userInfo'

export interface UserParam {
  nickName?: string
  phone?: string
  avatarUrl?: string
  city?: string
  country?: string
  province?: string
  gender?: number
  language?: string
  timestamp?: number
  physical_warehouse?: number
  physical_warehouse_name?: 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?: false|true
  is_authorize_phone?: false|true
  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
  collection_quantity?: number
  sale_user_phone?: string
  sale_user_name?: string
  sale_user_id?: number|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?: false // 登录状态 true 正在登录
}

interface Action {
  type?: string
  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 function counter(state = INIT_USER, action: Action) {
  const { type, data } = action
  switch (type) {
    case SET_USERINFO:
      Taro.setStorageSync('userInfo', JSON.stringify(data?.userInfo))
      return { ...state, ...data }
    case SET_ADMINUSERINFO:
      Taro.setStorageSync('adminUserInfo', JSON.stringify(data?.adminUserInfo))
      return { ...state, ...data }
    case SET_TOKEN:
      Taro.setStorageSync('token', data?.token)
      return { ...state, ...data }
    case SET_SESSIONKEY:
      Taro.setStorageSync('session_key', data?.session_key)
      return { ...state, ...data }
    case SET_SORTCODE:
      Taro.setStorageSync('sort_code', JSON.stringify(data?.sort_code))
      return { ...state, ...data }
    case CLEAR_TOKEN:
      Taro.removeStorageSync('token')
      return { ...state, token: '' }
    case CLEAR_SESSIONKEY:
      Taro.removeStorageSync('session_key')
      return { ...state, session_key: '' }
    case CLEAR_USERINFO:
      Taro.removeStorageSync('userInfo')
      return { ...state, userInfo: null }
    case CLEAR_ADMINUSERINFO:
      Taro.removeStorageSync('adminUserInfo')
      return { ...state, adminUserInfo: null }
    case CLEAR_SORTCODE:
      Taro.removeStorageSync('sort_code')
      return { ...state, sortCode: null }
    case LOGIN_STATUS:
      return { ...state, ...data }
    default:
      return state
  }
}
