import {createSlice} from "@reduxjs/toolkit";
import {REQ_STATUS} from "@api/config";
import {buildReqReducer, reqRedpackCheck, reqRedpackConfig, reqRedpackRecive} from "@features/api";
import {getHttpMsg} from "@utils/helpers";
import {toast} from "react-toastify";
const objInfo={}
const clearCDTimer=()=>{
    if (objInfo.cdTimer!=null){
        clearInterval(objInfo.cdTimer);
        objInfo.cdTimer=null;
    }
}
const checkRedpackLeftTime=(data)=>{
    clearCDTimer();
    let next_in_seconds=Number(data.next_in_seconds);
    let deadline=data.deadline;
    if (next_in_seconds>0){
        objInfo.cdTimer=setInterval(()=>{
            let now=Date.now();
            let during=deadline-now;
            during=Math.floor(during/1000);
            if (during<=0){
                global.dispatch(reqRedpackCheck());
                clearCDTimer();
            }
            else{
                global.dispatch(updateCD(during));
            }
        },1000);
    }
}

const reducerRedpackCheck=(state,action,arg)=>{
    let payload=action.payload;
    // payload={"data":{"status":1,"max":24680},"result":"","msg":"success","code":200};
    if (payload&&payload.code===200){
        let data=payload.data;
        if (data){
            let next_in_seconds=data.next_in_seconds?Number(data.next_in_seconds):0;
            let deadline=Date.now()+(next_in_seconds*1000);
            data.deadline=deadline;
            state.redPackData=data;
            checkRedpackLeftTime(data);
        }
    }
}
const redpackCheckStatus=(state,status,arg)=>{
    state.status.RedpackCheck=status;
}
const reducerRedpackRecive=(state,action,arg)=>{
    let payload=action.payload;
    // let msg=getHttpMsg(payload);
    if (payload&&payload.code===200){
        let data=payload.data;
        if (data){
            let amount=data.amount;
            // state.redPackData.amount=amount;
            // state.redPackData.status=2;
            state.redPackData=Object.assign({},state.redPackData,{amount:amount,status:2})
        }
        // if (msg!=null&&msg!==""){
        //     toast.success(msg);
        // }
    }else
    {
        let obj=payload.msg;
        let msg="";
        if (obj){
            if (obj.message!=null){
                msg=obj.message;
            }
        }
        if (msg!=null&&msg!==""){
            toast.error(msg,{toastId:"redpackRecive"});
        }
    }
}
const redpackReciveStatus=(state,status,arg)=>{
    state.status.RedpackRecive=status;
}
const reducerRedpackConfig=(state,action,arg)=>{
    let payload=action.payload;
    if (payload&&payload.code===200){
        state.config=payload.data;
    }
}
const redpackConfigStatus=(state,status,arg)=>{
    state.status.RedpackConfig=status;
}

export const RedpackSlice = createSlice({
    name: "redpack",
    initialState: {
        status:{
            RedpackCheck:REQ_STATUS.NONE,
            RedpackRecive:REQ_STATUS.NONE,
            RedpackConfig:REQ_STATUS.NONE,
        },
        redPackData:null,
        config:[]
    },
    reducers:{
        updateCD:((state, action) => {
            let payload=action.payload;
            if (payload!=null){
                if (state.redPackData!=null){
                    state.redPackData.next_in_seconds=payload;
                }
            }
        }),
        updateAlRecivedStatus:((state, action) => {
            state.redPackData.status=-1;
        })
    },
    extraReducers: (builder) => {
        buildReqReducer(builder,reqRedpackCheck,reducerRedpackCheck,redpackCheckStatus);
        buildReqReducer(builder,reqRedpackRecive,reducerRedpackRecive,redpackReciveStatus);
        buildReqReducer(builder,reqRedpackConfig,reducerRedpackConfig,redpackConfigStatus);
    }
})
export const {updateCD,updateAlRecivedStatus}=RedpackSlice.actions;
export const getRedPackInfo=(state)=>{
    return state.redpack.redPackData;
}
export const getRedPackConfig=(state)=>{
    return state.redpack.config;
}
export const getReciveStatus=(state)=>{
    return state.redpack.status.RedpackRecive;
}
export const getRedConfigStatus=(state)=>{
    return state.redpack.status.RedpackConfig;
}
export default RedpackSlice.reducer;