import { useAjax } from '@/Hook/useAjax';
import { getMpMsgGroupSendStrategy, addMpMsgGroupSendStrategy, executionNowApi, editMpMsgGroupSendStrategy, delMpMsgGroupSendStrategy, Props, preview, localPreviewApi, sendPreview, refreshSendResult, catLogs, mpMsgGroupSendStrategy, getDetails, delByLog, getRestart } from '@/services/operating/news';
import moment from 'moment';
import { useEffect, useReducer } from 'react'
type State = {
    visible: boolean,//弹窗开启与否
    wxVisible: boolean,//微信公众号选择弹窗
    msgVisible: boolean,//图片，视频，音频类型弹窗
    textVisible: boolean,//文字类型弹窗
    graphicVisible: boolean,//图文类型弹窗
    theEdit: any,//选中的数据
    rules: 0 | 1,//微信后台权限 
    fans: 0 | 1,//群发粉丝
    name: string,//任务名称
    msgType: 'mpnews' | 'text' | 'voice' | 'mpvideo' | 'image',//消息类型
    mobile?: string,
    sendIgnoreReprint: boolean,
    actionWX: any,
    triggerType: number,//发送方式
    weekDay: number,//星期几
    time: string,//时分
    sendTime: string,//指定时间
    enabled: boolean,//启停开关
    resDelayHours: number,//时
    resDelayMinutes: number,//分
    resDelaySecond: number,//秒
    mpFansListDto: {
        sex: 'all' | 0 | 1 | 2,// 'all'全部，0未知，1男，2女
        tagIds: any[],
        tagType: 0 | 1 | 2, // 0：并集,1：交集,2：取反

    },

}
export type Action = {
    type: 'show' | 'wxShow' |
    'msgType' | 'msgVisible' |
    'textVisible' | 'graphicVisible' |
    'theEdit' | 'setDate' | 'setTime' | 'setTime3' |
    'setRules' | 'setFans' | 'setSex' |
    'setPhone' | 'setSendIgnoreReprint' |
    'setMpFansListDto' | 'initData' | 'setActionWX' | 'setName' | 'mediaId' | 'triggerType' | 'setWeekDay' | 'setEnabled'
    ,
    params?: any
}
function reducer(state: State, action: Action) {
    let { type, params } = action
    switch (type) {
        case 'setActionWX':
            return { ...state, actionWX: params.actionWX }
        case 'show':
            return { ...state, visible: !state.visible }
        case 'wxShow':
            return { ...state, wxVisible: params.show ? params.show : !state.wxVisible }
        case 'msgType':
            return { ...state, msgType: params.msgType }
        case 'msgVisible':
            return { ...state, msgVisible: !state.msgVisible }
        case 'textVisible':
            return { ...state, textVisible: !state.textVisible }
        case 'graphicVisible':
            return { ...state, graphicVisible: !state.graphicVisible }
        case 'theEdit':
            console.log('params.theEdit ', params.theEdit)
            return { ...state, theEdit: params.theEdit }
        case 'mediaId':
            return { ...state, theEdit: { ...state.theEdit, mediaId: params.mediaId } }
        case 'setDate':
            console.log('setDate', params.sendTime)
            return { ...state, sendTime: params.sendTime }
        case 'setWeekDay':
            console.log('setWeekDay', params.weekDay)
            return { ...state, weekDay: params.weekDay }
        case 'triggerType':
            console.log('triggerType', params.triggerType)
            return { ...state, triggerType: params.triggerType }
        case 'setTime':
            return { ...state, time: params.time }
        case 'setRules':
            return { ...state, rules: params.rules }
        case 'setFans':
            return { ...state, fans: params.fans, rules: params.fans === 0 ? 1 : state.rules }
        case 'setSex':
            return { ...state, sex: params.sex }
        case 'setPhone':
            return { ...state, mobile: params.mobile }
        case 'setName':
            return { ...state, name: params.name }
        case 'setEnabled':
            return { ...state, enabled: params.enabled }
        case 'setSendIgnoreReprint':
            return { ...state, sendIgnoreReprint: params.sendIgnoreReprint }
        case 'setMpFansListDto':
            return { ...state, mpFansListDto: { ...state.mpFansListDto, ...params } }
        case 'setTime3':
            Object.keys(params).forEach((key) => {
                state[key] = params[key]
            })
            return { ...state }
        case 'initData':
            if (!params) {
                return {
                    visible: false,
                    wxVisible: false,
                    msgType: 'mpnews',
                    msgVisible: false,
                    theEdit: null,
                    graphicVisible: false,
                    textVisible: false,
                    time: moment(new Date()).format('HH:mm'),
                    rules: 1,
                    fans: 1,
                    sex: 'all',
                    name: '',
                    triggerType: 1,
                    weekDay: 1,
                    sendTime: null,
                    sendIgnoreReprint: false,
                    actionWX: null,
                    enabled: true,
                    resDelayHours: 0,//时
                    resDelayMinutes: 0,//分
                    resDelaySecond: 0,//秒
                    mpFansListDto: {
                        sex: 'all',
                        tagIds: [],
                        tagType: 0
                    }
                }
            }
            let { appId, mpId, appName, msgType, content, fansAll, tagIds, sex, tagType, title, items, downUrl, sendTime, ...param } = params
            let data = {
                ...param,
                actionWX: { appId, id: mpId, nickName: appName },
                // theEdit: msgType === 'text' ? { textContent: content.split('<br/>'), msgType } : {
                //     thumbUrl: downUrl,
                //     name: title ,
                //     msgType
                // },
                fans: fansAll ? 1 : 0,
                msgType,
                sendTime,
                mpFansListDto: {
                    tagIds: tagIds ? tagIds && Array.isArray(tagIds) ? tagIds : [tagIds] : [],
                    sex: sex !== null ? sex : 'all',
                    tagType
                }
            }
            return { ...state, ...data }
        default:
            return state;
    }
}
export default function useNews() {
    const [state, dispatch] = useReducer(reducer, {
        visible: false,
        wxVisible: false,
        msgType: 'mpnews',
        msgVisible: false,
        theEdit: null,
        graphicVisible: false,
        textVisible: false,
        time: moment(new Date()).format('HH:mm'),
        rules: 1,
        fans: 1,
        name: '',
        sex: 'all',
        triggerType: 1,
        weekDay: 1,
        sendTime: null,
        sendIgnoreReprint: false,
        actionWX: null,
        enabled: true,
        resDelayHours: 0,//时
        resDelayMinutes: 0,//分
        resDelaySecond: 0,//秒
        mpFansListDto: {
            sex: 'all',
            tagIds: [],
            tagType: 0
        }
    })
    const getMsgGroup = useAjax((params?: { date?: string, state?: number, appId?: string, pageNum: number, pageSize: number }) => getMpMsgGroupSendStrategy(params))
    const delMsgGroup = useAjax((params: { id: number }) => delMpMsgGroupSendStrategy(params), { msgNmae: '删除' })
    const addMsgGroup = useAjax((params: Props) => addMpMsgGroupSendStrategy(params), { msgNmae: '新增' })
    const editMsgGroup = useAjax((params: Props) => editMpMsgGroupSendStrategy(params), { msgNmae: '编辑' })
    const postPreview = useAjax((params: { appId: string, content: any, mediaId: string, msgType: string, openId: string, wxName: string }) => preview(params), { msgNmae: '预览' })
    const localPreview = useAjax((params: { sysMediaId: number, mpId: number, fansId: number }) => localPreviewApi(params), { msgNmae: '预览' })
    const kfPreview = useAjax((params: { fansIds: string, mpIds: any[] }) => sendPreview(params), { msgNmae: '预览' })
    const refresh = useAjax((params: { id: string }) => refreshSendResult(params), { msgNmae: '刷新状态' })
    const catlog = useAjax((id) => catLogs(id))
    const enabled = useAjax((params) => mpMsgGroupSendStrategy(params))
    const getDetail = useAjax((id) => getDetails(id))
    const delLog = useAjax((id) => delByLog(id), { msgNmae: '删除成功' })
    const restart = useAjax((id) => getRestart(id), { msgNmae: '执行成功' })
    const executionNow = useAjax((id) => executionNowApi(id), { msgNmae: '重发' })
    useEffect(() => {
        localStorage.setItem('collapsed', state.visible ? '1' : '0')
    }, [state.visible])
    return {
        state,
        getMsgGroup,
        delMsgGroup,
        addMsgGroup,
        editMsgGroup,
        postPreview,
        localPreview,
        dispatch,
        kfPreview,
        refresh,
        catlog,
        enabled,
        getDetail,
        delLog,
        restart,
        executionNow
    }
}
