import React, { useEffect, useState } from 'react'
import { StarPRNT } from 'react-native-star-prnt';
import { ActivityIndicator, Dimensions, SafeAreaView, StyleSheet, View } from 'react-native';
import SelectDropdown from 'react-native-select-dropdown'
import { useLanguage } from 'ordering-components/native'
import { useTheme } from 'styled-components/native'
import { useForm, Controller } from 'react-hook-form';
import FAIcons from 'react-native-vector-icons/FontAwesome'
import MCIcons from 'react-native-vector-icons/MaterialCommunityIcons'
import SimpleLineIcons from 'react-native-vector-icons/SimpleLineIcons'
import FeatherIcon from 'react-native-vector-icons/Feather'
import {
ContainerEdition,
BackArrowWrapper,
BackArrow,
WrapperIcons,
WrapperHeader,
WrapperIcon
} from './styles'
import { Container } from '../../layouts/Container'
import { OText, OInput, OIcon, OButton } from '../shared'
import { PRINTERS } from './printerList'
import { MessageAlert } from './MessageAlert'
const printerList = PRINTERS.map(printer => ({
...printer,
ip: '',
type: 1,
nickname: printer.model,
portName1: `BT:${printer.model.split(' ')[0]}`,
bt: `BT:${printer.model.split(' ')[0]}`
}))
export const PrinterEdition = (props: any) => {
const { printer, onClose } = props
const HEIGHT_SCREEN = Dimensions.get('window').height
const [, t] = useLanguage()
const theme = useTheme()
const { control, setValue, watch } = useForm()
const [currentPrinter, setCurrentPrinter] = useState(printer)
const [discoverPort, setDiscoverPort] = useState({ loading: false, msg: null })
const watchIp = watch('ip')
const watchBt = watch('bt')
const watchNickname = watch('nickname')
const errorIP = !/^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/i.test(watchIp)
const errorBT = !/^[^\n]*$/i.test(watchBt) || !watchBt
const isErrorInput = currentPrinter?.type === 2 ? errorIP : errorBT
const isErrorNickname = watchNickname && !/^[^\n]*$/i.test(watchNickname)
const styles = StyleSheet.create({
icons: {
maxWidth: 40,
height: 40,
padding: 10,
alignItems: 'flex-end'
},
optionIcons: {
paddingRight: 10
},
inputStyle: {
borderWidth: 1,
borderRadius: 8,
color: theme.colors.arrowColor,
borderColor: theme.colors.inputSignup,
backgroundColor: theme.colors.transparent,
minHeight: 50,
maxHeight: 50
},
savePrinterBtnText: {
color: theme.colors.white,
fontSize: 18,
},
savePrinterBtn: {
height: 44,
borderRadius: 8,
marginHorizontal: 20
}
})
const handleChangePrinter = (values: any) => {
props.handleChangePrinter({
...values,
index: currentPrinter?.index ?? null,
type: currentPrinter?.type,
ip: currentPrinter?.ip,
item: currentPrinter
})
}
const portDiscovery = async () => {
try {
setDiscoverPort({ ...discoverPort, loading: true })
let printers = await StarPRNT.portDiscovery('Bluetooth');
if (printers?.length) {
setValue('bt', printers[0]?.portName)
setCurrentPrinter({
...currentPrinter,
['bt']: printers[0]?.portName
})
}
setTimeout(() => {
setDiscoverPort({
...discoverPort,
loading: false,
msg: !printers?.length
? t('NO_PRINTERS_FOUND', 'No printers found')
: null
})
}, 1000);
} catch (e) {
setValue('bt', currentPrinter?.portName)
setCurrentPrinter({
...currentPrinter,
['bt']: currentPrinter?.portName
})
}
}
const onSubmit = () => {
handleChangePrinter({ edit: !!printer, isAdd: !printer })
onClose && onClose()
}
useEffect(() => {
currentPrinter?.ip && setValue('ip', currentPrinter?.ip)
currentPrinter?.bt && setValue('bt', currentPrinter?.bt)
currentPrinter?.nickname && setValue('nickname', currentPrinter?.nickname)
}, [currentPrinter?.type])
useEffect(() => {
setCurrentPrinter(printer)
}, [printer])
return (
onClose()}>
{t('PRINTER_CONFIGURE', 'Printer Configure')}
p.model === currentPrinter?.model)}
dropdownOverlayColor='transparent'
defaultButtonText={t('SELECT_PRINTER', 'Select printer')}
buttonTextAfterSelection={item => item.model}
rowTextForSelection={item => item.model}
renderDropdownIcon={() => dropDownIcon()}
buttonStyle={{
width: '100%',
backgroundColor: theme.colors.primary,
borderRadius: 8,
flexDirection: 'row-reverse',
justifyContent: 'space-between',
alignItems: 'center',
height: 44,
marginTop: 20,
}}
buttonTextStyle={{
color: theme.colors.white,
fontSize: 18,
textAlign: 'left',
marginHorizontal: 0
}}
dropdownStyle={{
borderRadius: 8,
borderColor: theme.colors.primary,
maxHeight: HEIGHT_SCREEN * 0.3
}}
rowStyle={{
borderBottomColor: theme.colors.disabled,
backgroundColor: theme.colors.gray100,
height: 40,
flexDirection: 'column',
alignItems: 'flex-start',
paddingTop: 8,
paddingHorizontal: 14,
width: '100%'
}}
rowTextStyle={{
marginHorizontal: 0,
fontSize: 14,
}}
onSelect={item => setCurrentPrinter(item)}
/>
{!!currentPrinter && (
{`${t('PRINTER_NICKNAME', 'Nickname')}:`}
(
{
setValue('nickname', value)
setCurrentPrinter({
...currentPrinter,
nickname: value
})
}}
/>
)}
/>
{`${t('CONNECTION_TYPE', 'Connection type')}:`}
{
setCurrentPrinter({
...currentPrinter,
type: 1,
ip: '',
portName: currentPrinter?.portName1
})
}}
>
{t('CONNECTION_VIA_BLUETOOTH', 'Via Bluetooth')}
setCurrentPrinter({ ...currentPrinter, type: 2 })}
>
{t('CONNECTION_VIA_LAN', 'Via LAN')}
{currentPrinter?.type === 1 && (
{t('SEARCH_AVAILABLE_PRINTER_MESSAGE', 'Use the search icon to find an available printer')}
)}
(
{
setValue(currentPrinter?.type === 2 ? 'ip' : 'bt', value)
setCurrentPrinter({
...currentPrinter,
[currentPrinter?.type === 2 ? 'ip' : 'bt']: value
})
}}
/>
)}
/>
{currentPrinter?.type === 1 && (
<>
{!discoverPort.loading ? (
portDiscovery()}
/>
) : (
)}
>
)}
{discoverPort.msg ? (
setDiscoverPort({ ...discoverPort, msg: null })}
/>
) : (
`${t('EXAMPLE_SHORT', 'Ex:')} ${currentPrinter?.type === 2 ? '8.8.8.8' : currentPrinter?.portName1}`
)}
)}
onSubmit()}
/>
)
}
const dropDownIcon = () => {
const theme = useTheme()
return (
)
}