import { createSlice } from "@reduxjs/toolkit";
import { buildReqReducer, reqAgentCheck, reqCheckinCheck, reqCheckinGet } from "@features/api";
import { REQ_STATUS } from "@api/config";
import { formatCash, getHttpMsg, parseNYTimeToTimestamp } from "@utils/helpers";
import { toast } from "react-toastify";
const i18n = global.i18n;
const reducerCheckinCheck = (state, action, arg) => {
    let payload = action.payload;
    if (payload != null && payload.code === 200) {
        let data = payload.data;
        if (data) {
            let config = data.config;
            let RewardDay = [];
            let RewardScore = [];
            let nDayNumber = -1;
            let cbRecord = [];
            if (config) {
                for (let i = 0; i < config.length; i++) {
                    let item = config[i];
                    let DayNumber = parseInt(item.DayNumber);
                    let RecordDay = parseInt(data.RecordDay.substring(i, i + 1)) > 0 ? 1 : 0;
                    let RewScore = Number(formatCash(item.RewardScore, true));
                    let SignInDay = parseInt(item.SignInDay);
                    RewardDay.push(DayNumber);
                    RewardScore.push(RewScore);
                    cbRecord.push(RecordDay);
                    nDayNumber = SignInDay;
                }
            }
            // nDayNumber=Number(data.NextDayNumber)
            let LastDate = 0;
            let NextGetTime = 0;
            let isCanCheckIn = 0;
            if (data.LastDate != null) {
                // LastDate=data.LastDate;
                // LastDate=LastDate.split(".").shift();
                // LastDate=Date.parse(LastDate+"+06:00");
                LastDate = parseNYTimeToTimestamp(data.LastDate);
                NextGetTime = LastDate + 86400000;
            }
            let now = Date.now();
            if (now - NextGetTime > 0) {
                isCanCheckIn = 1;
            }
            let NextDayNumber = Number(data.NextDayNumber);
            if (NextDayNumber > 7) {
                NextDayNumber = 7
                isCanCheckIn = 0;
            }
            state.checkInData = {
                RewardDay,
                RewardScore,
                cbRecord,
                nDayNumber,
                LastDate,
                NextDayNumber
            }
            state.NextGetTime = NextGetTime;
            state.isCanCheckIn = isCanCheckIn;
            let callback = arg;
            if (callback != null) callback(isCanCheckIn);
        }
    }
}
const checkinCheckStatus = (state, status, arg) => {
    state.status.CheckInCheck = status;
}
const reducerCheckinGet = (state, action, arg) => {
    let payload = action.payload;
    let msg = getHttpMsg(payload);
    if (payload != null && payload.code === 200) {
        let data = payload.data;
        // global.showDropCoins();
        msg = i18n.t("CheckIn.get_success");
        if (msg && msg !== "") toast.success(msg,{toastId: "reducerCheckinGet"});
        state.isCanCheckIn = false;
    }
    else {
        if (msg && msg !== "") toast.error(msg,{toastId: "reducerCheckinGet"});
    }
    if (arg.callback) arg.callback(payload);
}
const checkinGetStatus = (state, status, arg) => {
    state.status.CheckInGet = status;
}
export const CheckInSlice = createSlice({
    name: 'checkIn',
    initialState: {
        checkInData: {
            // nDayNumber:2,
            // cbRecord:[1],
            // RewardDay:[1,2,3,4,5,6,7],
            // RewardScore:[1,1,1,2,2,2,2]
            nDayNumber: -1,
            cbRecord: [],
            RewardDay: [],
            RewardScore: [],
            LastDate: 0,
        },
        NextGetTime: 0,
        isCanCheckIn: 0,
        status: {
            CheckInCheck: REQ_STATUS.NONE,
            CheckInGet: REQ_STATUS.NONE,
        }
    },
    reducers: {
    },
    extraReducers: (builder) => {
        buildReqReducer(builder, reqCheckinCheck, reducerCheckinCheck, checkinCheckStatus);
        buildReqReducer(builder, reqCheckinGet, reducerCheckinGet, checkinGetStatus);
    },
})
export const { } = CheckInSlice.actions;
export const getCheckInData = (state) => {
    return state.checkIn.checkInData;
}
export const getCheckInStatus = (state) => {
    return state.checkIn.status;
}
export const getNextGetTime = (state) => {
    return state.checkIn.NextGetTime;
}
export const getIsCanCheckIn = (state) => {
    return state.checkIn.isCanCheckIn;
}
export default CheckInSlice.reducer;
