import { useAjax } from "@/Hook/useAjax"
import { getGraphicDetail, getMedia, getWxMediaInfo } from "@/services/operating/material"
import { message } from "antd"
import { useCallback, useReducer } from "react"
import { useModel } from "umi"

interface State {
    isHover: boolean,
    fx: 'topRight' | 'topLeft' | 'bottomRight' | 'bottomLeft',
    dataArr: any[],//数据存储
    actionId: number,//左侧当前选中
    actionIdold?: number,//记录上一次的选中
    delIds: any[]
}
interface Action {
    type: 'isHover' | 'fx' | 'addDataArr' | 'delDataArr' | 'action' | 'actionData' | 'mobile' | 'pushData' | 'init',
    params?: any
}

function reducer(state: State, action: Action) {
    let { type, params } = action
    let { dataArr } = state
    switch (type) {
        case 'isHover':
            return { ...state, isHover: params.isHover }
        case 'fx':
            return { ...state, fx: params.fx }
        case 'addDataArr':  //左侧添加
            if (dataArr.length < 8) {
                let id = dataArr.length + 1
                let newDelIds = [...state.delIds]
                // if (newDelIds.length > 0) {
                //     id = state.delIds[0]
                //     newDelIds.splice(0, 1)
                // }

                return { ...state, dataArr: [...dataArr, { menuId: id, ...params.data }], delIds: newDelIds }
            }
            return state
        case 'delDataArr':   //左侧删除
            if (dataArr.length > 0) {
                let newArr= dataArr.filter((item: any) => item.menuId !== params.menuId).map((item:any,index:number)=>{
                    item.menuId = index+1
                    return item
                })
                return { ...state, dataArr: newArr, delIds: [...state.delIds, params.menuId] }
            }
            return { ...state, ... { dataArr: [], actionId: 1, mediaTagIds: [], groupId: '', actionData: null } }
        case 'action':  //左侧选中
            return { ...state, actionIdold: state.actionId, actionId: params.menuId }
        case 'actionData':
            return { ...state, actionData: params.data }
        case 'mobile'://左侧移动
            let arr: any[] = []
            dataArr.forEach((item: any, index: number) => {
                if (index === (params.up ? params.eq - 1 : params.eq + 1)) {
                    arr[index] = dataArr[params.eq]
                    arr[params.eq] = dataArr[index]
                } else if (index !== params.eq) {
                    arr[index] = dataArr[index]
                }
            })
            return { ...state, dataArr: arr }
        case 'init':
            return { ...state, dataArr: [], delIds: [], actionId: 1 }
        default:
            return { ...state }
    }
}
let initData = {
    isHover: false,
    fx: 'topRight',
    dataArr: [],
    actionId: 1,
    delIds: []
}

function useSynthetic() {
    const { actionWX }: any = useModel('useOperating.useWxGroupList', model => ({ actionWX: model.state.actionWX }))
    const bdDetail = useAjax((params) => getMedia(params))
    const wxDetail = useAjax((params: {id: number, isCon?: boolean}) => getWxMediaInfo(params))
    const [state, dispatch] = useReducer(reducer, initData)
    const addData = useCallback((data: { sysMediaId: number, id: number, mediaId: string, thumbMediaId: string, index: number, smId?: number, smType?: string ,title:string}) => {
        if (state.dataArr.length === 8) {
            message.error('不能超过8篇！')
            return
        }
        let { smType = '本地' } = data
        if (smType === '本地') {//本地
            bdDetail.run(data?.sysMediaId).then((res) => {
                let isOk = true
                if (res) {
                    res?.news?.forEach((item: { id: number}, index: number) => {
                        if ((item.id === data.id || data.index === index) && isOk) {
                            dispatch({ type: 'addDataArr', params: { data: item } })
                            isOk = false
                        }
                    })
                }else{
                    message.error('不存在的文章')
                }
            })
        } else {//微信
            wxDetail.run({id: data.smId}).then((res) => {
                if (res) {
                    let isOk = true
                    res?.news?.forEach((item: { thumbMediaId: string,title:string}, index: number) => {
                        if (((item.thumbMediaId === data.thumbMediaId && item.title === data.title)|| data.index === index) && isOk) {
                            dispatch({ type: 'addDataArr', params: { data: item } })
                            isOk = false
                        }
                    })
                }
            })
        }

    }, [state, actionWX])

    console.log('state.dataArr',state.dataArr)
    return { state, dispatch, addData }
}
export default useSynthetic