/**
 * @flow
 */
import _ from 'lodash'
import { Alert, Platform } from 'react-native'
import { HybridModalAlert } from './native-utils'

function alert(title: string, message?: string|null, buttons?: Array<Object>) {
  if (Platform.OS === 'ios') {
    Alert.alert(message || title, null, buttons || [{ text: '确定' }])
  } else {
    const btns = buttons ? _(buttons).reverse().value() : buttons || [{ text: '确定' }]
    if (HybridModalAlert) {
      const onPress = (position:number = 0) => {
        const btn = (btns[position] || {})
        if (btn.onPress && typeof btn.onPress === 'function') {
          btn.onPress()
        }
      }

      let cancelText = ''
      if (btns.length === 2) {
        cancelText = btns[1].text || ''
      }


      HybridModalAlert.alert({
        title: '',
        message: message || title,
        cancelText,
        okText: btns[0].text || '',
      }, onPress)
    } else {
      Alert.alert(message ? title : '', message || title, btns)
    }
  }
}

export default {
  alert,
}
