import {createSlice} from "@reduxjs/toolkit";
import {REQ_STATUS} from "@api/config";
import {
    buildReqReducer,
    reqMailCheck,
    reqMailDel,
    reqMailList,
    reqMailRead,
    reqMailTake,
    reqPageModules
} from "@features/api";
import {getHttpMsg} from "@utils/helpers";
import {toast} from "react-toastify";
const reducerMailList=(state,action,arg)=>{
    let json=action.payload;
    if (json&&json.code===200){
        let data=json.data;
        if (data!=null){
            state.pageData=data;
            let list=data.data;
            state.mailList=list;
        }
    }
}
const mailListStatus=(state,status,arg)=>{
    state.status.MAIL_LIST=status;
}
const reducerMailCheck=(state,action,arg)=>{
    let json=action.payload;
    if (json&&json.code===200){
        let data=json.data;
        if (data){
            let newmail=data.new;
            state.new=newmail;
        }
    }
}
const mailCheckStatus=(state,status,arg)=>{
    state.status.MAIL_CHECK=status;
}
const reducerMailRead=(state,action,arg)=>{
    let json=action.payload;
    if (json&&json.code===200){
        let data=json.data;
        if (data){
            let MailID=data.MailID;
            let item=getMailFromID(MailID,state);
            if(item)item.MailStatus="2";
            let mailNew=state.new;
            state.new=mailNew-1;
        }
    }
}
const getMailFromID=(MailID,state)=>{
    let mailList=state.mailList;
    if (mailList){
        for (let i = 0; i <mailList.length ; i++) {
            let item=mailList[i];
            if (item){
                if (item.MailID===MailID){
                    return item;
                }
            }
        }
    }
    return null;
}
const mailReadStatus=(state,status,arg)=>{
    state.status.MAIL_READ=status;
}
const reducerMailDel=(state,action,arg)=>{
    let json=action.payload;
    let msg=getHttpMsg(json);
    if (json&&json.code===200){
        toast.success(msg,{toastId:'mailDel'});
    }
    else {
        toast.error(msg,{toastId:'mailDel'});
    }
}
const mailDelStatus=(state,status,arg)=>{
    state.status.MAIL_DEL=status;
}
const reducerMailTake=(state,action,arg)=>{
    let json=action.payload;
    let msg=getHttpMsg(json);
    if (json&&json.code===200){
        // take成功返回：'MailID','OrgScore','GiftScore','NowScore'
        let data=json.data;
        if (data){
            let MailID=data.MailID;
            if (MailID!=null){
                let item=getMailFromID(MailID,state);
                if (item)item.MailStatus="3";
            }
        }
        toast.success(msg,{toastId:'mailTake'});
    }
    else
    {
        toast.error(msg,{toastId:'mailTake'});
    }
}
const mailTakeStatus=(state,status,arg)=>{
    state.status.MAIL_TAKE=status;
}
export const Mail=createSlice({
    name: 'mail',
    initialState: {
        pageData:null,
        new:0,
        mailList:[
            // {
            //     CreateTime:1716370381,
            //     MailID:7297133,
            //     MailStatus:2,
            //     UserID:4596382,
            //     strkBonus:"30000,0;",
            //     strkText:"hehehehe,\nhehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehe,hehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehe,hehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehe,hehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehe,hehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehe,hehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehe,hehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehe,hehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehe,hehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehe,hehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehe,hehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehe,hehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehe,hehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehe,hehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehe,hehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehe,hehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehe,hehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehe,hehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehe,hehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehe,hehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehe,hehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehe,hehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehe,hehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehe,hehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehe,hehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehe,hehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehe,hehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehe,hehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehehe",
            //     strkTitle:"hhhhhhwwww"
            // },
            // {
            //     CreateTime:1716370381,
            //     MailID:7297131,
            //     MailStatus:2,
            //     UserID:4596382,
            //     strkBonus:"30000,10000;",
            //     strkText:"fffff",
            //     strkTitle:"hhhh"
            // },
        ],
        readMailID:0,
        status:{
            MAIL_LIST:REQ_STATUS.NONE,
            MAIL_CHECK:REQ_STATUS.NONE,
            MAIL_READ:REQ_STATUS.NONE,
            MAIL_DEL:REQ_STATUS.NONE,
            MAIL_TAKE:REQ_STATUS.NONE
        }
    },
    reducers: {
        readMail:((state, action) => {
            state.readMailID=action.payload;
        }),
        preDelMail:((state, action) => {
            let MailID=action.payload;
            if (MailID===0)state.mailList=[];
            else
            {
                for (let i = 0; i < state.mailList.length; i++) {
                    let item=state.mailList[i];
                    if (item){
                        if (item.MailID===MailID){
                            state.mailList.splice(i,1);
                            break;
                        }
                    }
                }
            }
        }),
    },
    extraReducers: (builder) => {
        buildReqReducer(builder,reqMailList,reducerMailList,mailListStatus);
        buildReqReducer(builder,reqMailCheck,reducerMailCheck,mailCheckStatus);
        buildReqReducer(builder,reqMailRead,reducerMailRead,mailReadStatus);
        buildReqReducer(builder,reqMailDel,reducerMailDel,mailDelStatus);
        buildReqReducer(builder,reqMailTake,reducerMailTake,mailTakeStatus);
    },
})
export const {readMail,preDelMail} = Mail.actions;
export const getMailList=(state)=>{
    let mailList=state.mail.mailList;
    if (mailList==null)return [];
    return mailList;
}
export const getMail=(mailId)=>(state)=>{
    let list=getMailList(state);
    if (list.length===0)return null;
    for (let i = 0; i < list.length; i++) {
        let item=list[i];
        if (item&&item.MailID===mailId){
            return item;
        }
    }
    return null;
}
export const getReadID=(state)=>{
    return state.mail.readMailID;
}
export const getMailStatus=(key)=>(state)=>{
    return state.mail.status[key];
}
export const getMailNew=(state)=>{
    return state.mail.new;
}
export default Mail.reducer;
