import Card from "./card";
import {useEffect, useState} from "react";
import {Pressable, Image, Linking, View} from "react-native";
// Icons
import TickActive from '../assets/ph_check-fill.svg'
import TickDanger from '../assets/tick_danger.svg'
import TickSuccess from '../assets/tick_success.svg'
import Chevron from '../assets/bi_chevron.svg'
import TickError from '../assets/codicon_error.svg'
const Tooltip = ({timeTill}) => {
const d = new Date(timeTill)
return (
{`${d.getDate()}.${d.getMonth() + 1}.${d.getFullYear()}`}
{`${d.toLocaleTimeString()}`}
)
}
const Task = ({id, status, title, description, time_till: timeTill, onPress, time_finished: timeFinished, imageType, navi, type}) => {
const [intervalId, setIntervalId] = useState(null);
const [leastTime, setLeastTime] = useState("");
const variantSelection = () => {
if (type && type === 'event') {
switch (status) {
case "active":
return 'default'
case "completed":
return 'success'
}
}
switch (status) {
case "active":
if (new Date().getTime() <= new Date(timeTill)) {
return "default"
}
return "error"
case "uncompleted":
return "error"
case "completed":
if (new Date(timeFinished).getTime() <= new Date(timeTill).getTime()) {
return "success"
}
return "danger"
}
}
const countDays = () => {
const date = new Date(timeTill);
let now = !!timeFinished ? new Date(timeFinished) : new Date();
const diffTime = Math.abs(date - now);
return Math.floor(diffTime / (1000 * 60 * 60 * 24));
}
const countHours = () => {
const date = new Date(timeTill);
let now = !!timeFinished ? new Date(timeFinished) : new Date();
if (now.getTime() <= date.getTime()) {
now = now.getTime() + countDays() * 24 * 60 * 60 * 1000;
} else {
now = now.getTime() - countDays() * 24 * 60 * 60 * 1000;
}
const diffTime = Math.abs(date - now);
return Math.floor(diffTime / (1000 * 60 * 60));
}
const countMinutes = () => {
const date = new Date(timeTill);
let now = !!timeFinished ? new Date(timeFinished) : new Date();
if (now.getTime() <= date.getTime()) {
now = now.getTime() + countDays() * 24 * 60 * 60 * 1000 + countHours() * 60 * 60 * 1000;
} else {
now = now.getTime() - countDays() * 24 * 60 * 60 * 1000 - countHours() * 60 * 60 * 1000;
}
const diffTime = Math.abs(date - now);
return Math.floor(diffTime / (1000 * 60));
}
const absTime = () => {
const query = {
start: (d, idx) => {
const val = +(d.split('').slice(-2).join(''))
if (val > 10 && val < 20) return [`${d} дней`, `${d} часов`, `${d} минут`][idx]
return query['1'](d, idx)
}
,
'1': (d, idx) => {
const val = +(d.split('').slice(-1))
if (val === 1) return [`${d} день`, `${d} час`, `${d} минута`][idx]
return query['2-4'](d, idx)
},
'2-4': (d, idx) => {
const val = +(d.split('').slice(-1))
if (val > 1 && val < 5) return [`${d} дня`, `${d} часа`, `${d} минуты`][idx]
return [`${d} дней`, `${d} часов`, `${d} минут`][idx]
},
}
let days = query.start(`${countDays()}`, 0)
const hours = query.start(`${countHours()}`, 1)
const minutes = query.start(`${countMinutes()}`, 2)
setLeastTime(`${days} ${hours} ${minutes}`)
}
useEffect(() => {
if (intervalId !== null) {
clearInterval(intervalId)
setIntervalId(null)
}
absTime()
if (status !== 'active') return;
setIntervalId(setInterval(() => absTime(), 1000))
}, [timeTill, timeFinished]);
const SelectImage = () => {
if (imageType === 'chevron') {
return
}
switch (status) {
case "active":
return
case "completed":
if (new Date(timeFinished).getTime() <= new Date(timeTill).getTime()) {
return
}
return
case "uncompleted":
return
}
}
return (
}
tooltip={type !== 'event' && }>
{title.split("|").map(txt => {
if (txt.replace("", '').replace("", '') !== txt) {
return {txt.replace('', '').replace('', '')}
}
else {
return {txt}
}
})}
{!!description && {description}}
{!!navi && Linking.openURL(`yandexnavi://build_route_on_map?lat_to=${navi.lat}&lon_to=${navi.lon}`)}>
Открыть в яндекс картах
}
{
type !== 'event' && status === 'active' && new Date(timeTill) >= new Date() &&
Осталось времени:
}
{
type !== 'event' && status === 'active' && new Date(timeTill) < new Date() &&
Опоздание:
}
{
type !== 'event' && status === 'completed' && new Date(timeFinished) <= new Date(timeTill) &&
Завершено раньше на:
}
{
type !== 'event' && status === 'completed' && new Date(timeFinished) > new Date(timeTill) &&
Завершено c опозданием на:
}
{
type !== 'event' && status !== 'uncompleted' && {leastTime}
}
)
}
export default Task;