import {frontendPreferences as frontendPrefsConfig} from '../config'

export function getCookie(name){
  const begin = document.cookie.indexOf(name)
  if (begin === -1){
    return ''
  }
  const fromBegin = document.cookie.substr(begin)
  const tokenWithName = fromBegin.split(';')
  const tokenWithNameSplit = tokenWithName[0].split('=')
  const token = tokenWithNameSplit[1]
  return token.trim()
}

export function setCookie(name, value = '', expireDays, path) {
  const expiresDate = Date.now() + expireDays * 24 * 60 * 60 * 1000
  const expiresTime = new Date(expiresDate).toUTCString()
  //http://stackoverflow.com/questions/1134290/cookies-on-localhost-with-explicit-domain
  const cookie = `${name}=${value}; expires=${expiresTime}; path=${path}`
  return document.cookie = cookie
}

import moment from 'moment'

export function fromNowOrNow(date) {
  var now = moment(date)

  if (Math.abs(moment().diff(now)) < 1000) { // 1000 milliseconds
    return 'just now'
  }

  return now.fromNow()
}

import defaultUserpic50Path from 'assets/images/default-userpic-50.png'
import defaultUserpic75Path from 'assets/images/default-userpic-75.png'

const userDefaults = {
  profilePictureMediumUrl: defaultUserpic50Path,
  profilePictureLargeUrl: defaultUserpic75Path,
  frontendPreferences: {
    displayNames: {
      displayOption: 1,
      useYou: true
    }
  }
}

export function userParser(user) {
  // Profile pictures
  user.profilePictureMediumUrl = user.profilePictureMediumUrl || userDefaults.profilePictureMediumUrl
  user.profilePictureLargeUrl = user.profilePictureLargeUrl || userDefaults.profilePictureLargeUrl

  // Frontend preferences (only use this client's subtree)
  const prefSubTree = user.frontendPreferences && user.frontendPreferences[frontendPrefsConfig.clientId]
  user.frontendPreferences = _.merge(userDefaults.frontendPreferences, prefSubTree)

  return {...user}
}

export function postParser(post) {
  post.commentsDisabled = (post.commentsDisabled === '1')
  return {...post}
}

export function preventDefault(realFunction) {
  return (event) => {
    event.preventDefault()
    return realFunction && realFunction()
  }
}

export function confirmFirst(realFunction) {
  return () => {
    if (confirm('Are you sure?')) {
      return realFunction && realFunction()
    }
  }
}

export function getCurrentRouteName(router) {
  return router && router.routes[router.routes.length - 1].name
}

export function pluralForm(n, singular, plural = null, format = 'n w') {
  let w

  if (n == 1) {
    w = singular
  } else if (plural) {
    w = plural
  } else {
    w = singular + 's'
  }

  return format.replace('n', n).replace('w', w)
}

import {frontendPreferences as frontendPrefsConfig} from '../config'

export function getCookie(name){
  const begin = document.cookie.indexOf(name)
  if (begin === -1){
    return ''
  }
  const fromBegin = document.cookie.substr(begin)
  const tokenWithName = fromBegin.split(';')
  const tokenWithNameSplit = tokenWithName[0].split('=')
  const token = tokenWithNameSplit[1]
  return token.trim()
}

export function setCookie(name, value = '', expireDays, path) {
  const expiresDate = Date.now() + expireDays * 24 * 60 * 60 * 1000
  const expiresTime = new Date(expiresDate).toUTCString()
  //http://stackoverflow.com/questions/1134290/cookies-on-localhost-with-explicit-domain
  const cookie = `${name}=${value}; expires=${expiresTime}; path=${path}`
  return document.cookie = cookie
}

import moment from 'moment'

export function fromNowOrNow(date) {
  var now = moment(date)

  if (Math.abs(moment().diff(now)) < 1000) { // 1000 milliseconds
    return 'just now'
  }

  return now.fromNow()
}

import defaultUserpic50Path from 'assets/images/default-userpic-50.png'
import defaultUserpic75Path from 'assets/images/default-userpic-75.png'

const userDefaults = {
  profilePictureMediumUrl: defaultUserpic50Path,
  profilePictureLargeUrl: defaultUserpic75Path,
  frontendPreferences: {
    displayNames: {
      displayOption: 1,
      useYou: true
    }
  }
}

export function userParser(user) {
  const newUser = {...user}

  // Profile pictures
  newUser.profilePictureMediumUrl = user.profilePictureMediumUrl || userDefaults.profilePictureMediumUrl
  newUser.profilePictureLargeUrl = user.profilePictureLargeUrl || userDefaults.profilePictureLargeUrl

  // Frontend preferences (only use this client's subtree)
  const prefSubTree = user.frontendPreferences && user.frontendPreferences[frontendPrefsConfig.clientId]
  newUser.frontendPreferences = _.merge({}, userDefaults.frontendPreferences, prefSubTree)

  return newUser
}

export function postParser(post) {
  post.commentsDisabled = (post.commentsDisabled === '1')
  return {...post}
}

export function preventDefault(realFunction) {
  return (event) => {
    event.preventDefault()
    return realFunction && realFunction()
  }
}

export function confirmFirst(realFunction) {
  return () => {
    if (confirm('Are you sure?')) {
      return realFunction && realFunction()
    }
  }
}

export function getCurrentRouteName(router) {
  return router && router.routes[router.routes.length - 1].name
}

export function pluralForm(n, singular, plural = null, format = 'n w') {
  let w

  if (n == 1) {
    w = singular
  } else if (plural) {
    w = plural
  } else {
    w = singular + 's'
  }

  return format.replace('n', n).replace('w', w)
}

Fix properties leaking in userParser()