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


interface nightState {
    sunRise: string | '06:00',
    sunSet: string | '18:00',
    sunRiseTomorrow: string | '06:00',
    sunSetTomorrow: string | '18:00',
    showNightRing: boolean | false,
    isMember: boolean | false,
    gpsInfo: Object | null,
    nightRingSunset: string | '18:00',
    nightRingSunrise: string | '06:00',
    nightRingDate: any | null
}

const initialState: nightState = {
    sunRise: '06:00',
    sunSet: '18:00',
    sunRiseTomorrow: '06:00',
    sunSetTomorrow: '18:00',
    showNightRing: false,
    isMember: false,
    gpsInfo: null,
    nightRingSunset: '18:00',
    nightRingSunrise: '06:00',
    nightRingDate: 0,
}

const nightSlice = createSlice({
    name: 'night',
    initialState,
    reducers: {
        showNight(state, action) {
            state.showNightRing = action.payload;
        },
        setNightRingData(state, action) {
            const { sunrise, sunset, date } = action.payload
            state.nightRingDate = date;
            state.nightRingSunrise = sunrise
            state.nightRingSunset = 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
            }
        },

        clearNightStore(state) {
            state.sunRise = '06:00'
            state.sunSet = '18:00'
            state.sunRiseTomorrow = '06:00'
            state.sunSetTomorrow = '18:00'
            state.nightRingSunset = '18:00'
            state.nightRingSunrise = '06:00'
            state.showNightRing= false
            state.nightRingDate = 0
        }
    }
});

export default nightSlice.reducer;
export const { showNight, updateMember, setNightRingData, clearNightStore } = nightSlice.actions;