import { Swiper, SwiperItem, View, Text } from "@tarojs/components";
import DayNightCard from "./DayNightCard";
import { useEffect, useState } from "react";
import { TimeFormatter } from "@/utils/time_format";
import { clearLocation, latestLocation } from "@/services/user";
import { useDispatch, useSelector } from "react-redux";
import '@/pages/clock/Clock.scss';
import AllRings from "./AllRings";
import AllDayRings from "./AllDayRings";
import DayNightSwiperPopup from "./DayNightSwiperPopup";
import { rpxToPx } from "@/utils/tools";

let locationObj

let useNavigation;
let Linking;
if (process.env.TARO_ENV == 'rn') {
    useNavigation = require("@react-navigation/native").useNavigation
    Linking = require('react-native').Linking;
}

export default function DayNightSwiper(props: { count: number, schedule: any, homeData: any }) {
    const user = useSelector((state: any) => state.user);
    const [loaded, setLoaded] = useState(false)
    const [firstNight, setFirstNight] = useState(true)
    const [current, setCurrent] = useState(0)
    const [authInfo, setAuthInfo] = useState<any>(null)


    useEffect(() => {
        setLoaded(false)
        getLatestLocation()
    }, [user.isLogin])

    global.swiperDayNightRefresh = () => {
        getLatestLocation()
    }

    useEffect(() => {
        if (loaded) {
            updateDate(user.isLogin ? locationObj : null)
        }
    }, [props.count])

    function getLatestLocation() {
        if (!user.isLogin) {
            setLoaded(true)
            updateDate(null)
            return
        }
        var today = new Date()
        var yesterday = new Date(today.getTime() - 24 * 3600 * 1000)
        var tomorrow = new Date(today.getTime() + 24 * 3600 * 1000 * 50)
        var strYesterday = `${yesterday.getFullYear()}-${TimeFormatter.padZero(yesterday.getMonth() + 1)}-${TimeFormatter.padZero(yesterday.getDate())}`
        var strTomorrow = `${tomorrow.getFullYear()}-${TimeFormatter.padZero(tomorrow.getMonth() + 1)}-${TimeFormatter.padZero(tomorrow.getDate())}`
        latestLocation({
            date_start: strYesterday, date_end: strTomorrow
        }).then(data => {
            // (data as any).timezone = 'GMT-1000'
            if (data && (data as any).daylights) {
                (data as any).daylights.map(item => {
                    if (!item.sunrise_ts) {
                        item.sunrise_ts = new Date(item.date + 'T' + item.sunrise + ':00').getTime()
                        item.sunset_ts = new Date(item.date + 'T' + item.sunset + ':00').getTime()
                    }
                })
            }
            setAuthInfo(data)
            updateDate(data)
            locationObj = data
            setLoaded(true)
        }).catch(e => {
            setLoaded(true)
            updateDate(null)
        })
    }

    function updateDate(data) {
        var today = new Date()
        if (data && data.daylights && data.daylights.length > 0) {
            if (data.night_completed) {
                if (today.getTime() > data.night_completed.sunrise_ts) {
                    setFirstNight(true)
                    setCurrent(1)
                    return;
                }
            }
            setCurrent(0)
            // var sunriseObj,sunsetObj;
            var list = data.daylights
            for (var i = 0; i < (list.length - 1); i++) {
                //夜间
                if (list[i].sunset_ts < today.getTime() && list[i + 1].sunrise_ts > today.getTime()) {
                    setFirstNight(true)
                }
                //白天
                else if (list[i].sunrise_ts < today.getTime() && list[i].sunset_ts > today.getTime()) {
                    setFirstNight(false)
                }
            }
        }
        else {
            setCurrent(0)
            if (today.getHours() < 6 || today.getHours() > 18) {
                setFirstNight(true)
            } else {
                setFirstNight(false)
            }
        }

    }




    if (!loaded)
        return <View className="rn_swiper" />


    var timestamp = new Date().getTime()

    return <View><Swiper
        className="rn_swiper"
        indicatorDots
        indicatorColor='#ffffff66'
        indicatorActiveColor='#ffffff99'
        current={current}
        style={{height:process.env.TARO_ENV=='weapp'?rpxToPx(204)+96+4:rpxToPx(204)+96+12}}

    >
        <SwiperItem>
            <DayNightCard isNight={firstNight} count={props.count} />
        </SwiperItem>
        <SwiperItem>
            <DayNightCard isNight={!firstNight} count={props.count} />
        </SwiperItem>
    </Swiper>
        <AllRings data={props.homeData} time={timestamp} />
        <AllDayRings schedule={props.schedule} />
        <DayNightSwiperPopup authInfo={authInfo} />
    </View>
}