import { createSlice } from "@reduxjs/toolkit";


interface dayState {
    sunRise: string | '06:00',
    sunSet: string | '18:00',
    sunRiseTomorrow: string | '06:00',
    sunSetTomorrow: string | '18:00',
    showDayRing: boolean | false,
    isMember: boolean | false,
    gpsInfo: Object | null,
    dayRingSunrise: string | '06:00',
    dayRingSunset: string | '18:00',
    dayRingDate: any | null,
}

const initialState: dayState = {
    sunRise: '06:00',
    sunSet: '18:00',
    sunRiseTomorrow: '06:00',
    sunSetTomorrow: '18:00',
    showDayRing: false,
    isMember: false,
    gpsInfo: null,
    dayRingSunrise: '06:00',
    dayRingSunset: '18:00',
    dayRingDate: 0,
}

const daySlice = createSlice({
    name: 'day',
    initialState,
    reducers: {
        showDay(state, action) {
            state.showDayRing = action.payload;
        },
        setDayRingData(state, action) {
            const { sunrise, sunset, date } = action.payload
            state.dayRingDate = date+'';
            state.dayRingSunrise = sunrise
            state.dayRingSunset = sunset
        },
        updateMember(state, action) {
            var isMember = action.payload.isMember;
            var gpsInfo = action.payload.gpsInfo;
            state.isMember = isMember;
            if (isMember && gpsInfo) {
                state.sunRise = '07:00'
                state.sunSet = '19:00'
                state.sunRiseTomorrow = '07:00'
                state.sunSetTomorrow = '19:00'
                state.gpsInfo = gpsInfo
            }
            else {
                state.sunRise = '06:00'
                state.sunSet = '18:00'
                state.sunRiseTomorrow = '06:00'
                state.sunSetTomorrow = '18:00'
                state.gpsInfo = gpsInfo ? gpsInfo : null
            }
        },
        clearDayStore(state) {
            state.sunRise = '06:00'
            state.sunSet = '18:00'
            state.sunRiseTomorrow = '06:00'
            state.sunSetTomorrow = '18:00'
            state.gpsInfo = null
            state.dayRingSunrise = '06:00'
            state.dayRingSunset = '18:00'
            state.dayRingDate = 0
            state.showDayRing = false
        }
    }
});

export default daySlice.reducer;
export const { showDay, updateMember, setDayRingData, clearDayStore } = daySlice.actions;