import Timeline from "@/components/view/Timeline";
import { TimeFormatter } from "@/utils/time_format";
import { View } from "@tarojs/components";
import getStatus from "../hooks/Record";
import { ColorType } from "@/context/themes/color";
import { useTranslation } from "react-i18next";

export default function TimelineFastSleep(props: { data: any, title?: string,first_real_check_time?:number }) {
    const {t} = useTranslation()
    function formateTime(obj: any, isEnd: boolean) {
        if (isEnd) {
            if (obj.real_end_time) {
                return TimeFormatter.timelineFormatTime(obj.real_end_time)
            }
            else {
                return TimeFormatter.timelineFormatTime(obj.target_end_time)
            }
        }
        else {
            if (obj.real_start_time) {
                return TimeFormatter.timelineFormatTime(obj.real_start_time)
            }
            else {
                return TimeFormatter.timelineFormatTime(obj.target_start_time)
            }
        }

    }

    function showDate(obj: any, isEnd: boolean){
        var dt = 0
        if (isEnd) {
            if (obj.real_end_time) {
                dt = obj.real_end_time
            }
            else {
                dt = obj.target_end_time
            }
        }
        else {
            if (obj.real_start_time) {
                dt = obj.real_start_time
            }
            else {
                dt = obj.target_start_time
            }
        }
        // debugger
        var dt2 = props.first_real_check_time?props.first_real_check_time:0
        var date1 = new Date(dt)
        var date2 = new Date(dt2)
        if (date1.getFullYear() == date2.getFullYear() &&
        date1.getMonth()==date2.getMonth() &&
        date1.getDate()==date2.getDate()){
            return false
        }
        return true
    }

    function formateDate(obj: any, isEnd: boolean) {
        if (isEnd) {
            if (obj.real_end_time) {
                return TimeFormatter.dateDescription(obj.real_end_time)
            }
            else {
                return TimeFormatter.dateDescription(obj.target_end_time)
            }
        }
        else {
            if (obj.real_start_time) {
                return TimeFormatter.dateDescription(obj.real_start_time)
            }
            else {
                return TimeFormatter.dateDescription(obj.target_start_time)
            }
        }

    }

    var timelineItems: any = [];
    if (props.data.fast) {
        timelineItems.push(
            {
                status: getStatus(true, true, props.data),
                title: t('feature.track_time_duration.common.start_fast'),
                content: formateTime(props.data.fast, false),
                date: showDate(props.data.fast, false)?formateDate(props.data.fast, false):'',
                color:global.fastColor?global.fastColor:ColorType.fast
            }
        )
    }
    if (props.data.sleep) {
        timelineItems.push(
            {
                status: getStatus(false, true, props.data),
                title: t('feature.track_time_duration.common.start_sleep'),
                content: formateTime(props.data.sleep, false),
                date: showDate(props.data.sleep, false)?formateDate(props.data.sleep, false):'',
                color:global.sleepColor?global.sleepColor:ColorType.sleep
            }
        )
    }
    if (props.data.sleep) {
        timelineItems.push(
            {
                status: getStatus(false, false, props.data),
                title: t('feature.track_time_duration.common.end_sleep'),
                content: formateTime(props.data.sleep, true),
                date: showDate(props.data.sleep, true)?formateDate(props.data.sleep, true):'',
                color:global.sleepColor?global.sleepColor:ColorType.sleep
            }
        )
    }
    if (props.data.fast) {
        timelineItems.push(
            {
                status: getStatus(true, false, props.data),
                title: t('feature.track_time_duration.common.end_fast'),
                content: formateTime(props.data.fast, true),
                date: showDate(props.data.fast, true)?formateDate(props.data.fast, true):'',
                color:global.fastColor?global.fastColor:ColorType.fast
            }
        )
    }

    return <Timeline items={timelineItems} title={props.title} />
}