import {createSlice} from "@reduxjs/toolkit";
import {
    buildReqReducer,
    reqGameList,
    reqTurnplateBonus,
    reqTurnplateHelp,
    reqTurnplateIndex,
    reqTurnplateKan
} from "@features/api";
import {REQ_STATUS} from "@api/config";
import {getHttpMsg} from "@utils/helpers";
import {toast} from "react-toastify";
const reducerTurnplateIndex=(state,action,arg)=>{
    let payload=action.payload;
    if (payload&&payload.code===200){
        let data=payload.data;
        if (data){
            state.turnplate=data.turnplate;
            state.record=data.record;
            state.link=data.link;
            state.numbers=data.numbers;
            state.myRecord=data.myRecord;
        }
    }
}
const reducerTurnplateKan=(state,action,arg)=>{
    let payload=action.payload;
    // payload={
    //     "data": {
    //         "type": 2,
    //         "bonus": 9414,
    //         "times": 0,
    //         "collect_amount": 9414
    //     },
    //     "result": "",
    //     "msg": "success",
    //     "code": 200
    // }
    let callback=arg;
    if (payload&&payload.code===200){
        if (callback)callback(payload.data,payload.code);
    }
    else
    {
        let msg=getHttpMsg(payload);
        if (msg!=null&&msg!==""){
            toast.error(msg,{toastId: 'turnplateKan'});
        }
        if (callback)callback(payload.data,payload.code);
    }
}
const turnplateKanStatus=(state,status,arg)=>{
    state.status.TURNPLATE_KAN=status;
}
const reducerTurnplateBonus=(state,action,arg)=>{
    let payload=action.payload;
    let msg=getHttpMsg(payload);
    if (payload&&payload.code===200){
        if (msg!=null&&msg!==""){
            toast.success(msg,{toastId: 'turnplateBonus'});
        }
    }
    else {
        if (msg!=null&&msg!==""){
            toast.error(msg,{toastId: 'turnplateBonus'});
        }
    }
}
const turnplateBonusStatus=(state,status,arg)=>{
    state.status.TURNPLATE_BONUS=status;
}
const reducerTurnplateHelp=(state,action,arg)=>{
    let payload=action.payload;
    let callback=arg;
    let msg=getHttpMsg(payload);
    if (payload&&payload.code===200){
        if (callback)callback(payload.data);
        if (msg!=null&&msg!==""){
            toast.success(msg,{toastId: 'turnplateHelp'});
        }
    }
    else
    {
        if (msg!=null&&msg!==""){
            toast.error(msg,{toastId: 'turnplateHelp'});
        }
        if (callback)callback(payload.data);
    }
    state.helpdone=true;
}
export const Turnplate=createSlice({
    name: 'turnplate',
    initialState: {
        turnplate:null,
        record:[],
        link:null,
        numbers:[],
        myRecord:[],
        status:{
            TURNPLATE_KAN:REQ_STATUS.NONE,
            TURNPLATE_BONUS:REQ_STATUS.NONE
        },
        helpdone:false
    },
    reducers: {
        updateTimes:((state, action) => {
            let payload=action.payload;
            if (payload){
                let times=payload.times;
                state.turnplate.times=times;
            }
        }),
        updateCollect:((state, action) => {
            let payload=action.payload;
            if (payload){
                let collect_amount=payload.collect_amount;
                state.turnplate.collect_amount=collect_amount;
            }
        }),
    },
    extraReducers: (builder) => {
        buildReqReducer(builder,reqTurnplateIndex,reducerTurnplateIndex,null);
        buildReqReducer(builder,reqTurnplateKan,reducerTurnplateKan,turnplateKanStatus);
        buildReqReducer(builder,reqTurnplateBonus,reducerTurnplateBonus,turnplateBonusStatus);
        buildReqReducer(builder,reqTurnplateHelp,reducerTurnplateHelp,null);
    },
})
export const {updateTimes,updateCollect} = Turnplate.actions;
export const getTurnplate=(state)=>{
    return state.turnplate.turnplate;
}
export const getRecord=(state)=>{
    return state.turnplate.record;
}
export const getMyRecord=(state)=>{
    return state.turnplate.myRecord;
}
export const getShareUrl=(state)=>{
    return state.turnplate.link;
}
export const getTotalAmount=(state)=>{
    if (state.turnplate.turnplate){
        return state.turnplate.turnplate.total_amount;
    }
    else return 0;
}
export const getIsHelp=(state)=>{
    return state.turnplate.helpdone;
}
export const getNumbers=(state)=>{
    if (state.turnplate.numbers){
        let numbers=state.turnplate.numbers;
        let temp=numbers.slice(0);
        let random=[];
        for (let i = 0; i <20 ; i++) {
            let rand=Math.floor(Math.random()*temp.length);
            random.push(temp[rand]);
            temp.splice(rand,1);
        }
        return random;
    }
    return [];
}
export const getBonusStatus=(state)=>{
    return state.turnplate.status.TURNPLATE_BONUS;
}
export default Turnplate.reducer;
