import React, { useState, useEffect } from 'react';
import { StyleSheet, View, TouchableOpacity, Linking, Pressable, Image } from 'react-native';
import FastImage from 'react-native-fast-image'
import { useUtils, useOrder, useLanguage } from 'ordering-components/native';
import { useTheme } from 'styled-components/native';
import { OIcon, OText, OModal } from '../shared';
import { BusinessBasicInformationParams } from '../../types';
import { convertHoursToMinutes, lightenDarkenColor, shape } from '../../utils';
import MaterialComIcon from 'react-native-vector-icons/MaterialCommunityIcons'
import dayjs from 'dayjs';
import timezone from 'dayjs/plugin/timezone';
import isBetween from 'dayjs/plugin/isBetween';
dayjs.extend(timezone);
dayjs.extend(isBetween);
import {
BusinessContainer,
BusinessHeader,
BusinessLogo,
BusinessInfo,
BusinessInfoItem,
WrapReviews,
TitleWrapper,
RibbonBox,
SocialListWrapper
} from './styles';
import { Fade, Placeholder, PlaceholderLine } from 'rn-placeholder';
let BusinessInformation: null | React.ElementType = null
let BusinessReviews: null | React.ElementType = null
export const BusinessBasicInformation = (
props: BusinessBasicInformationParams,
) => {
const { navigation, businessState, isBusinessInfoShow, logo, header, isPreOrder } = props;
const { business, loading } = businessState;
const types = business?.types && business?.types?.filter(({ enabled }) => (enabled)).map(({ name }) => (name))
const theme = useTheme();
const [orderState] = useOrder();
const [, t] = useLanguage();
const [{ parsePrice, parseDistance, optimizeImage }] = useUtils();
const [openBusinessInformation, setOpenBusinessInformation] = useState(false);
const [openBusinessReviews, setOpenBusinessReviews] = useState(false);
const [businessInformationObtained, setBusinessInformationObtained] = useState(false)
const [businessReviewsObtained, setBusinessReviewsObtainedbtained] = useState(false)
const [imageRealSize, setImageRealSize] = useState({ width: 16, height: 9, loading: true })
const isChewLayout = theme?.header?.components?.layout?.type?.toLowerCase() === 'chew'
const hideLogo = theme?.business_view?.components?.header?.components?.business?.components?.logo?.hidden
const hideDeliveryFee = theme?.business_view?.components?.header?.components?.business?.components?.fee?.hidden
const hideTime = theme?.business_view?.components?.header?.components?.business?.components?.time?.hidden
const hideReviews = theme?.business_view?.components?.header?.components?.business?.components?.reviews?.hidden
const hideReviewsPopup = theme?.business_view?.components?.header?.components?.reviews?.hidden
const hideDistance = theme?.business_view?.components?.header?.components?.business?.components?.distance?.hidden
const hideInfoIcon = theme?.business_view?.components?.header?.components?.business?.components?.business_info?.hidden
const hideCity = theme?.business_view?.components?.header?.components?.business?.components?.city?.hidden
const hideHeader = theme?.business_view?.components?.header?.hidden
const styles = StyleSheet.create({
businesInfoheaderStyle: {
height: 150,
},
headerStyle: {
aspectRatio: imageRealSize?.width / imageRealSize?.height
},
logoStyle: {
width: 72,
height: 72,
borderRadius: 7.6,
justifyContent: 'flex-start',
alignItems: 'flex-start'
},
businessInfo: {
paddingHorizontal: 20,
paddingTop: isChewLayout ? 0 : 56,
},
bullet: {
flexDirection: 'row',
alignItems: 'center',
},
metadata: {
marginRight: 2,
},
starIcon: {
marginHorizontal: 5,
},
reviewStyle: {
flexDirection: 'row',
alignItems: 'center',
justifyContent: 'center'
},
modalTitleSectionStyle: {
position: 'absolute',
width: '100%',
top: 0,
zIndex: 100,
left: 20
},
socialIcon: {
borderRadius: 3,
borderColor: theme.colors.border,
borderWidth: isChewLayout ? 0 : 1,
width: 30,
height: 30,
justifyContent: 'center',
alignItems: 'center',
marginRight: 10,
marginBottom: 7
},
tiktokIcon: {
height: 12,
width: 12,
margin: 0,
padding: 0,
display: 'flex',
justifyContent: 'center',
alignItems: 'center'
},
headerChewStyle: {
paddingHorizontal: 30,
justifyContent: 'center',
height: '100%'
},
socialIconsChewContainer: {
flexDirection: 'row',
justifyContent: 'flex-start',
marginTop: 5
}
});
const handleClickBusinessInformation = () => {
if (!businessInformationObtained) {
BusinessInformation = require('../BusinessInformation').BusinessInformation
setBusinessInformationObtained(true)
}
setOpenBusinessInformation(true)
}
const handleClickBusinessReviews = () => {
if (!businessReviewsObtained) {
BusinessReviews = require('../BusinessReviews').BusinessReviews
setBusinessReviewsObtainedbtained(true)
}
setOpenBusinessReviews(true)
}
const getBusinessType = () => {
if (!types) return t('GENERAL', 'General');
const _types: any = [];
types.forEach(
(type: any) =>
_types.push(type)
);
return _types.join(', ');
};
const SocialNetWork = (props: any) => {
const { socialLink, iconTitle } = props
return (
Linking.openURL(socialLink)}>
)
}
useEffect(() => {
if (businessState?.loading) return
let timeout: any = null
const currentDate = dayjs().tz(businessState?.business?.timezone)
let lapse = null
if (businessState?.business?.today?.enabled) {
lapse = businessState?.business?.today?.lapses?.find((lapse: any) => {
const from = currentDate.hour(lapse.open.hour).minute(lapse.open.minute)
const to = currentDate.hour(lapse.close.hour).minute(lapse.close.minute)
return currentDate.unix() >= from.unix() && currentDate.unix() <= to.unix()
})
}
if (lapse) {
const to = currentDate.hour(lapse.close.hour).minute(lapse.close.minute)
const timeToClose = (to.unix() - currentDate.unix()) * 1000
timeout = setTimeout(() => {
navigation.navigate('BusinessPreorder', { business: businessState?.business, handleBusinessClick: () => navigation?.goBack() })
}, timeToClose)
}
return () => {
timeout && clearTimeout(timeout)
}
}, [businessState?.business])
useEffect(() => {
const bannerImage = header || businessState?.business?.header
if (!bannerImage) {
setImageRealSize({ width: 16, height: 9, loading: false })
return
}
Image.getSize(bannerImage, (width: number, height: number) => {
setImageRealSize({ width: width, height: height, loading: false });
}, (error: any) => {
setImageRealSize({ ...imageRealSize, loading: false });
console.log(error);
});
}, [header, businessState?.business?.header])
const SocialIcons = () => {
return (
<>
{loading ? (
{[...Array(5).keys()].map(i => (
))}
) : (
{!!business?.facebook_profile && (
)}
{!!business?.instagram_profile && (
)}
{!!business?.tiktok_profile && (
Linking.openURL(business?.tiktok_profile)}>
)}
{!!business?.pinterest_profile && (
)}
{!!business?.whatsapp_number && (
)}
{!!business?.snapchat_profile && (
)}
{isChewLayout && !hideInfoIcon && (
handleClickBusinessInformation()}>
{t('SEE_MORE_DESCRIPTION', 'See more')}
)}
)}
>
)
}
return (
{!hideHeader && (
{isChewLayout && !loading && (
{business?.name}
{business?.city?.name && !hideCity && (
{business?.city?.name}
)}
)}
)}
{!hideLogo && (
{!isBusinessInfoShow && (
<>
{loading ? (
) : (
logo || businessState?.business?.logo || typeof theme.images.dummies.businessLogo === 'string' ?
:
)}
>
)}
)}
{!isChewLayout && (
<>
{loading ? (
) : (
{business?.name}
{business?.ribbon?.enabled && (
{business?.ribbon?.text}
)}
)}
{loading ? (
) : (
{getBusinessType()}
)}
{!isChewLayout && (
)}
{loading && (
)}
{!hideDeliveryFee && (
{`${t('DELIVERY_FEE', 'Delivery fee')} ${business && parsePrice(business?.delivery_price || 0)} \u2022 `}
)}
{!hideTime && (
<>
{orderState?.options?.type === 1 ? (
{convertHoursToMinutes(business?.delivery_time) + ` \u2022 `}
) : (
{convertHoursToMinutes(business?.pickup_time) + ` \u2022 `}
)}
>
)}
{!hideDistance && (
{parseDistance(business?.distance || 0) + ` \u2022 `}
)}
{!hideReviews && (
{business?.reviews?.total}
)}
{!isBusinessInfoShow && !loading && (
<>
{isPreOrder && (!business?.professionals || business?.professionals?.length === 0) && (
<>
navigation.navigate('BusinessPreorder', { business: businessState?.business, handleBusinessClick: () => navigation?.goBack() })}>
{t('PREORDER', 'Preorder')}
{' \u2022 '}
>
)}
{!hideReviewsPopup && (
<>
handleClickBusinessReviews()}>
{t('REVIEWS', 'Reviews')}
{' \u2022 '}
>
)}
{!isBusinessInfoShow && !hideInfoIcon && !isChewLayout && (
handleClickBusinessInformation()}>
{t('INFORMATION', 'Information')}
)}
>
)}
>
)}
{businessInformationObtained ? (
setOpenBusinessInformation(false)}
isNotDecoration>
{BusinessInformation && (
)}
) : null}
{businessReviewsObtained ? (
setOpenBusinessReviews(false)}
isNotDecoration
>
{BusinessReviews && (
)}
) : null}
);
};