import './global.less'
import { businessPlanData, GENDER_TYPE } from "../../page/businessPlan/create/const"

const targetingData = [
    { key: 'msgTagIds', name: '智能标签' },
    { key: 'msgExcludeTagIds', name: '排除智能标签' },
    { key: 'platformKey', name: '书城' },
    { key: 'sysExternalExcludeTagNames', name: '排除标签' },
    { key: 'lastBookNameList', name: '最近阅读书籍' },
    { key: 'lastChapterNameList', name: '最近阅读章节' },
    { key: 'platformUserId', name: '书城用户ID' },
    { key: 'rechargeBook', name: '充值小说' },
    { key: 'begin', name: '关注时间' },
    { key: 'genders', name: '性别' },
    { key: 'rechargeCount', name: '充值次数' },
    { key: 'rechargeMoney', name: '充值金额' },
    { key: 'lastRechargeTime', name: '最后充值时间' },
    { key: 'firstRechargeMoney', name: '首充金额' },
    { key: 'rechargeMoneyMax', name: '单笔最大充值金额' },
    { key: 'subHour', name: '关注距今时间' },
    { key: 'subDay', name: '关注距今天' },
    { key: 'lastRechargeHour', name: '最近充值时间距今（小时）' },
    { key: 'lastReadTime', name: '最近阅读时间' },
    { key: 'lastReadHour', name: '最近阅读至今（小时）' },
    { key: 'opers', name: '手机系统' }
]

const targetingGroupData = [
    { key: 'groupChatNames', name: '群名称列表' },
    { key: 'groupChatTime', name: '群创建时间' },
    { key: 'groupChatHour', name: '群创建时长' },
    { key: 'groupChatUser', name: '群人数' }
]

interface ContentProps {
    unlimited?: string,         // 不限
    msgTagIds?: { [x: string]: string }[], // 智能标签
    msgExcludeTagIds?: { [x: string]: string }[], // 排除智能标签
    platformKey?: string,   // 书城
    sysExternalExcludeTagNames?: string[],   // 书城
    lastBookNameList?: string[],
    lastChapterNameList?: string[],
    platformUserId?: string,// 书城用户ID
    rechargeBook?: string,  // 充值小说
    begin?: {               // 关注时间
        beginTime?: string,
        endTime?: string
    },
    genders?: string[],     // 性别
    rechargeCount?: {       // 充值次数
        rechargeCountMin?: number,
        rechargeCountMax?: number
    },
    rechargeMoney?: {       // 充值金额
        rechargeMoneyMin?: number
        rechargeMoneyMax?: number
    },
    lastRechargeTime?: {        // 最后充值时间
        lastRechargeTimeMin?: string,
        lastRechargeTimeMax?: string
    },
    firstRechargeMoney?: { // 首充金额
        firstRechargeMoneyMin?: number,
        firstRechargeMoneyMax?: number
    },
    rechargeMoneyMax?: {  // 单笔最大充值金额
        rechargeMoneyMaxMin?: number,
        rechargeMoneyMaxMax?: number
    },
    subHour?: {          // 关注距今时间
        subHourMin?: number,
        subHourMax?: number
    },
    subDay?: {          // 关注距今天
        subDayMin?: number
        subDayMax?: number
    },
    lastRechargeHour?: {          // 充值距今
        lastRechargeHourMin?: number
        lastRechargeHourMax?: number
    },
    lastReadTime?: {        // 最近阅读时间
        lastReadTimeMin?: string,
        lastReadTimeMax?: string
    },
    lastReadHour?: {          // 充值距今
        lastReadHourMin?: number
        lastReadHourMax?: number
    },
    opers?: string[]   // 手机系统
    // 群
    groupChatNames?: string,
    groupChatTime?: {
        groupChatBeginTime?: string,
        groupChatEndTime?: string
    }
    groupChatHour?: {
        groupChatHourMin?: number,
        groupChatHourMax?: number
    }
    groupChatUser?: {
        groupChatUserMin?: number,
        groupChatUserMax?: number
    }
}

/** 重置筛选用户数据 */
export const resetFilterUserData = (data: { [x: string]: any }): ContentProps => {
    const {
        beginTime, endTime, rechargeCountMin, rechargeCountMax, rechargeMoneyMin, rechargeMoneyMax, lastRechargeTimeMin, lastRechargeTimeMax,
        firstRechargeMoneyMin, firstRechargeMoneyMax, rechargeMoneyMaxMin, rechargeMoneyMaxMax, subHourMin, subHourMax, subDayMin, subDayMax, 
        lastRechargeHourMin, lastRechargeHourMax, lastReadHourMin, lastReadHourMax, groupChatNames, groupChatBeginTime, groupChatEndTime, groupChatHourMin, groupChatHourMax, 
        groupChatUserMin, groupChatUserMax, lastReadTimeMin, lastReadTimeMax,
        ...params
    } = data

    const newData: ContentProps = { ...params }
    if (beginTime || endTime) {
        newData.begin = { beginTime, endTime }
    }
    if (rechargeCountMin || rechargeCountMax) {
        newData.rechargeCount = { rechargeCountMin, rechargeCountMax }
    }
    if (rechargeMoneyMin || rechargeMoneyMax) {
        newData.rechargeMoney = { rechargeMoneyMin, rechargeMoneyMax }
    }
    if (lastRechargeTimeMin || lastRechargeTimeMax) {
        newData.lastRechargeTime = { lastRechargeTimeMin, lastRechargeTimeMax }
    }
    if (lastReadTimeMin || lastReadTimeMax) {
        newData.lastReadTime = { lastReadTimeMin, lastReadTimeMax }
    }
    if (firstRechargeMoneyMin || firstRechargeMoneyMax) {
        newData.firstRechargeMoney = { firstRechargeMoneyMin, firstRechargeMoneyMax }
    }
    if (rechargeMoneyMaxMin || rechargeMoneyMaxMax) {
        newData.rechargeMoneyMax = { rechargeMoneyMaxMin, rechargeMoneyMaxMax }
    }
    if (subHourMin || subHourMax) {
        newData.subHour = { subHourMin, subHourMax }
    }
    if (subDayMin || subDayMax) {
        newData.subDay = { subDayMin, subDayMax }
    }
    if (lastRechargeHourMin || lastRechargeHourMax) {
        newData.lastRechargeHour = { lastRechargeHourMin, lastRechargeHourMax }
    }
    if (lastReadHourMin || lastReadHourMax) {
        newData.lastReadHour = { lastReadHourMin, lastReadHourMax }
    }
    if (groupChatNames?.length) {
        newData.groupChatNames = groupChatNames.join('、')
    }
    if (groupChatBeginTime && groupChatEndTime) {
        newData.groupChatTime = { groupChatBeginTime, groupChatEndTime }
    }
    if (groupChatHourMin || groupChatHourMax) {
        newData.groupChatHour = { groupChatHourMin, groupChatHourMax }
    }
    if (groupChatUserMin || groupChatUserMax) {
        newData.groupChatUser = { groupChatUserMin, groupChatUserMax }
    }
    return newData
}

const getContent = (data: any, configType: BUSINES_SPLAN_API.ConfigTypeProps) => {
    const currentTargetingData = configType === 'GROUP_GROUP' ? targetingGroupData : targetingData
    if (data) {
        const newData = resetFilterUserData(data)
        let newConten: ContentProps = newData
        newConten.unlimited = currentTargetingData.filter((item: any) => !Object.keys(newData).includes(item.key))?.map((item: any) => item?.name)?.toString()
        return newConten
    } else {
        return { unlimited: currentTargetingData?.map((item: any) => item?.name)?.toString() }
    }
}

interface Props {
    data: any,
    bookCityList: { label: string, value: string }[]
    configName?: string
    bookPlatForm?: { platformName: string, id: number, platformKey: string }[]
    bookList?: { id: number, bookName: string, platformId: number }[]
    configType?: BUSINES_SPLAN_API.ConfigTypeProps
}
/**
 * 定向处理展示
 * @returns 
 */
const FilterUserText = ({ data, configName, configType = 'USER_GROUP', bookCityList, bookPlatForm, bookList }: Props): string => {


    const content: any = getContent(data, configType)

    return (configName ? configName + '\n' : '') +
    (content?.msgTagIds ? `智能标签：${content?.msgTagIds?.['business'] ? `业务：${businessPlanData.find(i => i.value === content?.msgTagIds['business'])?.label}` : ''}--${content?.msgTagIds?.['bookCity'] ? `书城：${bookPlatForm.find(i => i.id === content?.msgTagIds['bookCity'])?.platformName}` : ''}--${content?.msgTagIds?.['product'] ? `产品：${bookList.find(i => i.id === content?.msgTagIds['product'])?.bookName}\n` : ''}` : '') + 
    (content?.msgExcludeTagIds ? `排除智能标签：${content?.msgExcludeTagIds?.['business'] ? `业务：${businessPlanData.find(i => i.value === content?.msgExcludeTagIds['business'])?.label}` : ''}--${content?.msgExcludeTagIds?.['bookCity'] ? `书城：${bookPlatForm.find(i => i.id === content?.msgExcludeTagIds['bookCity'])?.platformName}` : ''}--${content?.msgExcludeTagIds?.['product'] ? `产品：${bookList.find(i => i.id === content?.msgExcludeTagIds['product'])?.bookName}\n` : ''}` : '') + 
    (content?.sysExternalExcludeTagNames ? `排除标签：${content?.sysExternalExcludeTagNames.join('、') || '--'}\n` : '') +
    (content?.platformKey ? `书城：${bookCityList?.find(item => item.value === content?.platformKey)?.label || '--'}\n` : '') +
    (content?.lastBookNameList ? `最近阅读书籍：${content?.lastBookNameList?.join('、') || '--'}\n` : '') +
    (content?.lastChapterNameList ? `最近阅读章节：${content?.lastChapterNameList?.join('、') || '--'}\n` : '') +
    (content?.platformUserId ? `书城用户ID：${content?.platformUserId}\n` : '') + 
    (content?.rechargeBook ? `充值小说：${content?.rechargeBook}\n` : '') +
    (content?.begin ? `关注时间：${content?.begin?.beginTime ? content?.begin?.beginTime : '不限'} - ${content?.begin?.endTime ? content?.begin?.endTime : '不限'}\n` : '') + 
    (content?.genders?.length > 0 ? `性别：${content?.genders.map(g => GENDER_TYPE[g]).join('、')}\n` : '') +
    (content?.rechargeCount ? `充值次数：${content?.rechargeCount?.rechargeCountMin ? content?.rechargeCount?.rechargeCountMin : '不限'} - ${content?.rechargeCount?.rechargeCountMax ? content?.rechargeCount?.rechargeCountMax : '不限'}\n` : '') +
    (content?.rechargeMoney ? `充值金额：${content?.rechargeMoney?.rechargeMoneyMin ? content?.rechargeMoney?.rechargeMoneyMin : '不限'} - ${content?.rechargeMoney?.rechargeMoneyMax ? content?.rechargeMoney?.rechargeMoneyMax : '不限'}\n` : '') +
    (content?.lastRechargeTime ? `最后充值时间：${content?.lastRechargeTime?.lastRechargeTimeMin ? content?.lastRechargeTime?.lastRechargeTimeMin : '不限'} - ${content?.lastRechargeTime?.lastRechargeTimeMax ? content?.lastRechargeTime?.lastRechargeTimeMax : '不限'}\n` : '') +
    (content?.firstRechargeMoney ? `首充金额：${content?.firstRechargeMoney?.firstRechargeMoneyMin ? content?.firstRechargeMoney?.firstRechargeMoneyMin : '不限'} - ${content?.firstRechargeMoney?.firstRechargeMoneyMax ? content?.firstRechargeMoney?.firstRechargeMoneyMax : '不限'}\n` : '') +
    (content?.rechargeMoneyMax ? `单笔最大充值金额：${content?.rechargeMoneyMax?.rechargeMoneyMaxMin ? content?.rechargeMoneyMax?.rechargeMoneyMaxMin : '不限'} - ${content?.rechargeMoneyMax?.rechargeMoneyMaxMax ? content?.rechargeMoneyMax?.rechargeMoneyMaxMax : '不限'}\n` : '') +
    (content?.subHour ? `关注距今时间：${content?.subHour?.subHourMin ? content?.subHour?.subHourMin + '小时' : '不限'} - ${content?.subHour?.subHourMax ? content?.subHour?.subHourMax + '小时' : '不限'}\n` : '') +
    (content?.subDay ? `关注距今天：${content?.subDay?.subDayMin ? content?.subDay?.subDayMin + '天' : '不限'} - ${content?.subDay?.subDayMax ? content?.subDay?.subDayMax + '天' : '不限'}\n` : '') +
    (content?.lastRechargeHour ? `最近一次充值距今：${content?.lastRechargeHour?.lastRechargeHourMin ? content?.lastRechargeHour?.lastRechargeHourMin + '小时' : '不限'} - ${content?.lastRechargeHour?.lastRechargeHourMax ? content?.lastRechargeHour?.lastRechargeHourMax + '小时' : '不限'}\n` : '') +
    (content?.lastReadTime ? `最近阅读时间：${content?.lastReadTime?.lastReadTimeMin ? content?.lastReadTime?.lastReadTimeMin : '不限'} - ${content?.lastReadTime?.lastReadTimeMax ? content?.lastReadTime?.lastReadTimeMax : '不限'}\n` : '') +
    (content?.lastRechargeHour ? `最近阅读至今：${content?.lastReadHour?.lastReadHourMin ? content?.lastReadHour?.lastReadHourMin + '小时' : '不限'} - ${content?.lastReadHour?.lastReadHourMax ? content?.lastReadHour?.lastReadHourMax + '小时' : '不限'}\n` : '') +
    (content?.opers?.length > 0 ? `手机系统：${content?.opers.map(g => ({ '0': '未知', '1': '安卓', '2': 'IOS' } as any)[g]).join('、')}\n` : '') +
    (content?.groupChatNames ? `群名称列表：${content?.groupChatNames?.toString()}` : '') +
    (content?.groupChatTime ? `群创建时间：${content?.groupChatTime?.groupChatBeginTime ? content?.groupChatTime?.groupChatBeginTime + '小时' : '不限'} - ${content?.groupChatTime?.groupChatEndTime ? content?.groupChatTime?.groupChatEndTime + '小时' : '不限'}\n` : '') +
    (content?.groupChatHour ? `群创建时长：${content?.groupChatHour?.groupChatHourMin ? content?.groupChatHour?.groupChatHourMin : '不限'} - ${content?.groupChatHour?.groupChatHourMax ? content?.groupChatHour?.groupChatHourMax : '不限'}\n` : '') +
    (content?.groupChatUser ? `群人数：${content?.groupChatUser?.groupChatUserMin ? content?.groupChatUser?.groupChatUserMin : '不限'} - ${content?.groupChatUser?.groupChatUserMax ? content?.groupChatUser?.groupChatUserMax : '不限'}\n` : '') +
    (content?.unlimited ? `不限：${content?.unlimited}` : '')
}

export default FilterUserText