import TextAideInput from "@/pages/launchSystemV3/components/TextAideInput"
import { extractAndFilterBracketsContent, txtLength } from "@/utils/utils"
import { Button, Card, Form, Input, InputNumber, Modal, Popconfirm, Radio, Space, Tooltip, message } from "antd"
import React, { useEffect, useState } from "react"
import { TextTypeList } from "../../const"
import { DeleteOutlined, MinusCircleOutlined, PlusOutlined } from "@ant-design/icons"
import AddTextS from "./addTextS"
import style from '../index.less'
import styles from '../Material/index.less'
import VideoNews from "@/pages/launchSystemNew/components/newsModal/videoNews"
import SelectCopyWriting from "@/pages/launchSystemV3/tencenTasset/copyWriting/selectCopyWriting"

interface Props {
    textData: any,
    dynamicMaterialDTos: any,
    mediaType: 0 | 1 | 2 | 3,
    accountCreateLogs: PULLIN.AccountCreateLogsProps[]
    targetingLength?: number
    deliveryMode?: string,
    value?: any,
    visible?: boolean
    onClose?: () => void
    onChange?: (value: any) => void
    putInType?: 'NOVEL' | 'GAME'
    adDataGroup?: { [x: number]: any[] }
}

/**
 * 创意文案
 * @param param0 
 * @returns 
 */
const NewText: React.FC<Props> = ({ visible, onClose, onChange, value, textData, accountCreateLogs, targetingLength = 1, dynamicMaterialDTos, mediaType, deliveryMode, putInType, adDataGroup }) => {

    /*************************************/
    const [form] = Form.useForm();
    const type = Form.useWatch('type', form)
    const textDto = Form.useWatch('textDto', form)

    const [textList, setTextList] = useState<PULLIN.TextDtoProps[]>([])
    const [desVisible, setDesVisible] = useState<boolean>(false)
    const [addSTitle, setAddStitle] = useState<string>()
    const [newText, setNewText] = useState<{ description?: string[], title?: string[] }>({})
    const [maxNumber, setMaxNumber] = useState<number>(1)
    const [groupNumber, setGroupNumber] = useState<number>(1)
    /*************************************/

    const handleOk = (values: any) => {
        console.log(values)
        const { type, textDto } = values
        onChange?.({
            type,
            dynamicCreativesTextDetailDTOList: textDto
        })
    }

    useEffect(() => {
        if (value && Object.keys(value).length) {
            const { type, dynamicCreativesTextDetailDTOList } = value
            form.setFieldsValue({ type, textDto: dynamicCreativesTextDetailDTOList })
        }
    }, [value])

    useEffect(() => {
        if (textData && Object.keys(textData)) {
            let newText: { description?: string[], title?: string[] } = {}
            let data = Object.values(textData).map((item: any) => {
                let content = item.children.content
                if (item.name === 'description') {
                    newText.description = ['']
                } else if (item.name === 'title') {
                    newText.title = ['']
                }
                return { label: content.description, restriction: content.restriction, value: item.name, required: item.required, arrayProperty: item?.arrayProperty }
            })
            setMaxNumber(data?.[0]?.arrayProperty?.maxNumber)
            setNewText(newText)
            if (!(value && Object.keys(value).length)) { form.setFieldsValue({ textDto: [newText] }) }
            setTextList(data)
        }
    }, [textData, value])

    // 一一对应显示素材
    const showMaterial = (index: number) => {
        const dynamicGroup = dynamicMaterialDTos?.dynamicGroup
        if (dynamicGroup && Object.keys(dynamicGroup).length) {
            let dynamic = dynamicGroup[index]
            const keys = Object.keys(dynamic)
            if (deliveryMode === "DELIVERY_MODE_CUSTOMIZE") {
                return <div className={style.detail_body_m}>
                    {(keys.includes('video_id') || keys.includes('short_video1')) ? <>
                        <div className={style.video}>
                            <VideoNews src={dynamic?.video_id?.url || dynamic?.short_video1?.url} keyFrameImageUrl={dynamic?.video_id?.keyFrameImageUrl || dynamic?.short_video1?.keyFrameImageUrl} style={{ width: 40, height: 30 }} />
                            {dynamic?.cover_id && <div className={style.cover_image} style={{ marginLeft: 4, width: 40, height: 30, minWidth: 42 }}>
                                <img src={dynamic?.cover_id?.url} style={{ maxWidth: '96%', maxHeight: '96%' }} />
                            </div>}
                        </div>
                    </> : keys.includes('image_id') ? <>
                        <div className={style.cover_image} style={{ width: 40, height: 30, minWidth: 42 }}>
                            <img src={dynamic?.image_id?.url} />
                        </div>
                    </> : (keys.includes('image_list') || keys.includes('element_story')) ? <>
                        {dynamic?.[keys.includes('image_list') ? 'image_list' : 'element_story']?.map((item: { url: string | undefined; }, index: undefined) => <div className={style.cover_image} key={index} style={{ width: 30, height: 24, minWidth: 32 }}>
                            <img src={item?.url} />
                        </div>)}
                    </> : null}
                </div>
            } else {
                return <div style={{ display: 'flex', gap: 5, flexWrap: 'wrap' }}>
                    {dynamic?.list?.map((item: any, index: number) => {
                        if (Array.isArray(item)) {
                            let length = item.length
                            return <div className={styles.boxList_body_item} key={index} style={{ width: 30, height: 30 }}>
                                <div className={styles.content} style={{ width: 30, height: 30 }}>
                                    {item.map((l, i) => <img src={l?.url} key={i} style={{ width: length === 6 ? 9.999 : 14.999 }} />)}
                                </div>
                            </div>
                        } else if (item?.url?.includes('mp4') || item?.keyFrameImageUrl) {
                            return <div className={styles.boxList_body_item} key={index} style={{ width: 30, height: 30 }}>
                                <div className={styles.content} style={{ width: 30, height: 30 }}>
                                    <VideoNews src={item?.url} style={{ width: 30, height: 30 }} keyFrameImageUrl={item?.keyFrameImageUrl} maskBodyStyle={{ backgroundColor: "rgba(242, 246, 254, 0.1)" }} />
                                </div>
                            </div>
                        } else {
                            return <div className={styles.boxList_body_item} key={index} style={{ width: 30, height: 30 }}>
                                <div className={styles.content} style={{ width: 30, height: 30 }}><img src={item?.url} /></div>
                            </div>
                        }
                    })}
                </div>
            }
        }
        return null
    }

    const setText = (valList: string[]) => {
        const fieldData = textList.find(item => item.label === addSTitle)
        const count = dynamicMaterialDTos.dynamicGroup.length
        const valListLength = valList.length
        let len = 0;

        // 定义一个更新 textDto 的通用函数  
        const updateTextDto = (textDto: any[]) => {
            return textDto.map((item: { [key: string]: any }) => {
                if (fieldData?.value && (item?.[fieldData.value]?.length === 0 || item?.[fieldData.value]?.length < groupNumber || !item?.[fieldData.value]?.every((t: string) => t)) && valListLength >= len + 1) {
                    const textDto: string[] = (item?.[fieldData.value]?.length === 0 ? [''] : item?.[fieldData.value]).map((t: string) => {
                        if (t) {
                            return t;
                        } else if (valListLength >= len + 1) {
                            return valList[len++];
                        }
                        return t;
                    });
                    if (textDto.length < groupNumber && valListLength >= len + 1) {
                        const diff = groupNumber - textDto.length;
                        Array(diff).fill('').some(() => {
                            if (valListLength >= len + 1) {
                                textDto.push(valList[len++]);
                                return false;
                            }
                            return true;
                        });
                    }
                    return {
                        ...item,
                        [fieldData.value]: textDto,
                    };
                }
                return item;
            });
        };

        const newTextDto = updateTextDto(textDto);

        if ([1, 0, 5].includes(type)) {
            form.setFieldsValue({
                textDto: newTextDto
            })
        } else if (type === 2) {
            let diffTextDto: any[] = []
            if (newTextDto.length < count && len < valListLength) {
                const diffCount = count - newTextDto.length
                const diffTextCount = Math.ceil((valListLength - len) / groupNumber)
                let diff = 0
                if (diffCount >= diffTextCount) {
                    diff = diffTextCount
                } else {
                    diff = diffCount
                }
                diffTextDto = Array(diff).fill('').map((item: { [x: string]: any }) => {
                    if (fieldData?.value) {
                        const textDto: string[] = []
                        Array(groupNumber).fill('').some(() => {
                            if (valListLength >= len + 1) {
                                textDto.push(valList[len++])
                                return false
                            } else {
                                return true
                            }
                        })
                        return { ...item, [fieldData.value]: textDto }
                    }
                    return item
                })
            }
            form.setFieldsValue({
                textDto: [...newTextDto, ...diffTextDto]
            })
        } else if ([3, 4].includes(type)) {
            let diffTextDto: any[] = []
            if (len < valListLength) {
                const diff = Math.ceil((valListLength - len) / groupNumber)
                diffTextDto = Array(diff).fill('').map((item: { [x: string]: any }) => {
                    if (fieldData?.value) {
                        const textDto: string[] = []
                        Array(groupNumber).fill('').some(() => {
                            if (valListLength >= len + 1) {
                                textDto.push(valList[len++])
                                return false
                            } else {
                                return true
                            }
                        })
                        return { ...item, [fieldData.value]: textDto }
                    }
                    return item
                })
            }
            form.setFieldsValue({
                textDto: [...newTextDto, ...diffTextDto]
            })
        }
    }

    return <Modal
        title={<Space align="center">
            <strong style={{ fontSize: 20 }}>创意文案</strong>
            {type !== 0 && <>
                {textList.some(item => item.value === "description") && <a style={{ fontSize: 12 }} onClick={() => { setDesVisible(true); setAddStitle(textList.find(item => item.value === "description")?.label) }}>批量添加{textList.find(item => item.value === "description")?.label}</a>}
                {textList.some(item => item.value === "title") && <a style={{ fontSize: 12 }} onClick={() => { setDesVisible(true); setAddStitle(textList.find(item => item.value === "description")?.label) }}>批量添加{textList.find(item => item.value === "description")?.label}</a>}
            </>}
            {textList.some(item => item.value === "description") && <>
                <SelectCopyWriting
                    onClick={() => {
                        setAddStitle(textList.find(item => item.value === "description")?.label)
                    }}
                    onChange={(valList) => {
                        setText(valList)
                    }}
                />
                <Space><span style={{ fontSize: 12 }}>每组数量：</span><InputNumber max={maxNumber} size="small" value={groupNumber} onChange={(e) => setGroupNumber(e || 1)} /></Space>
            </>}
        </Space>}
        open={visible}
        onCancel={onClose}
        footer={null}
        width={800}
        className={`modalResetCss`}
        bodyStyle={{ padding: '0 0 40px', position: 'relative', borderRadius: '0 0 8px 8px' }}
        maskClosable={false}
    >
        <Form
            form={form}
            name="newText"
            labelAlign='left'
            layout="vertical"
            colon={false}
            style={{ backgroundColor: '#f1f4fc', maxHeight: 650, overflow: 'hidden', overflowY: 'auto', padding: '0 10px 10px', borderRadius: '0 0 8px 8px' }}
            scrollToFirstError={{
                behavior: 'smooth',
                block: 'center'
            }}
            onFinishFailed={({ errorFields }) => {
                message.error(errorFields?.[0]?.errors?.[0])
            }}
            initialValues={{
                type: 0,
                textDto: [{ description: [''], title: [''] }]
            }}
            onFinish={handleOk}
        >
            <Card className="cardResetCss" style={{ marginTop: 10 }}>
                <Form.Item
                    name="type"
                    label={<Space>
                        <strong>文案分配规则</strong>
                        {type == 5 && <span style={{ fontSize: 12 }}>共<span style={{ color: 'red' }}>{dynamicMaterialDTos.dynamicGroup.length * accountCreateLogs.length}</span>个文案组</span>}
                    </Space>}
                    style={{ marginBottom: 0 }}
                    rules={[{ required: true, message: '请选择文案分配规则!' }]}
                >
                    <Radio.Group onChange={(e) => {
                        let value = e.target.value
                        let count = dynamicMaterialDTos.dynamicGroup.length
                        let oldtextDto: PULLIN.TextDtoProps[] = JSON.parse(JSON.stringify(textDto))
                        let length = oldtextDto.length
                        if (value === 0 || (mediaType === 2 && value === 2)) {
                            oldtextDto = [textDto[0] || {}]
                        } else if (value === 1) {
                            if (count > length) {
                                oldtextDto = oldtextDto.concat(Array(count - length).fill(newText || { description: [''] }))
                            } else {
                                oldtextDto = oldtextDto.slice(0, count)
                            }
                        } else if (value === 2) {
                            if (count < length) {
                                oldtextDto = oldtextDto.slice(0, count)
                            }
                        } else if (value === 5) {
                            // 创意组数量 count 广告账号数量 adLength / targetingLength 所有创意数量  count * (adLength / targetingLength)
                            const allDynCount = count * accountCreateLogs.length
                            if (allDynCount > length) {
                                oldtextDto = oldtextDto.concat(Array(allDynCount - length).fill(newText || { description: [''] }))
                            } else {
                                oldtextDto = oldtextDto.slice(0, allDynCount)
                            }
                        }
                        form.setFieldsValue({ textDto: oldtextDto })
                    }}>
                        {TextTypeList
                            .filter(item => (mediaType !== 1 && mediaType !== 3) ? ![4, 5].includes(item.value) : mediaType === 3 ? true : item.value !== 5)
                            .map(item => {
                                if (item.value === 5 && (adDataGroup ? Object.keys(adDataGroup).some((key: any) => adDataGroup[key].length > dynamicMaterialDTos.dynamicGroup.length) : accountCreateLogs.some((pre) => ((pre?.productList || [{}]).length * targetingLength) > dynamicMaterialDTos.dynamicGroup.length))) {
                                    return <Radio value={item.value} key={item.value} disabled><Tooltip title={'账号下平均分配到广告下的创意组数量不够，不能选择此项'}>
                                        {item.label}
                                    </Tooltip></Radio>
                                } else {
                                    return <Radio value={item.value} key={item.value}>{item.label}</Radio>
                                }
                            })
                        }
                    </Radio.Group>
                </Form.Item>
            </Card>

            <Form.List name="textDto">
                {(fields, { add, remove }) => (<>
                    {fields.map(({ key, name, ...restField }, num) => (
                        <Card
                            title={type === 0 ? null : <div style={{ display: 'flex', gap: 5, alignItems: 'center' }}>
                                {type === 1 ? <strong style={{ fontSize: 14 }}>创意组{num + 1}</strong> : type === 0 ? null : <strong style={{ fontSize: 14 }}>文案组{num + 1}</strong>}
                                {type === 1 && showMaterial(num)}
                            </div>}
                            className="cardResetCss"
                            style={{ marginTop: 10, width: '100%' }}
                            key={key}
                            extra={([3, 2, 4].includes(type) && textDto?.length > 1) && <Popconfirm
                                title="你确定删除当前文案组?"
                                onConfirm={() => remove(name)}
                            >
                                <DeleteOutlined style={{ color: 'red' }} />
                            </Popconfirm>}
                        >
                            <div style={{ display: 'flex', flexWrap: 'wrap', gap: 10, width: '100%', flexDirection: 'column' }}>
                                {textList.map(item => {
                                    return <Form.List
                                        {...restField}
                                        name={[name, item.value]}
                                        key={key}
                                    >
                                        {(fields, { add, remove }) => <>
                                            {fields.map((field, tIndex) => (
                                                <Form.Item
                                                    {...field}
                                                    label={<Space>
                                                        <strong>{item.label}</strong>
                                                        {textDto?.[num]?.[item.value]?.length > 1 && <a style={{ color: 'red', fontSize: 12 }} onClick={() => remove(field.name)}><MinusCircleOutlined /></a>}
                                                    </Space>}
                                                    style={{ marginBottom: 0, width: '100%' }}
                                                    rules={[{
                                                        required: item.required, validator: (rule, value) => {
                                                            if (!value) {
                                                                return Promise.reject('请输入正确的' + item.label)
                                                            } else if (!value.match(RegExp(item.restriction.textRestriction.textPattern))) {
                                                                return Promise.reject('请输入正确的' + item.label)
                                                            } else if (txtLength(value) > item.restriction.textRestriction.maxLength) {
                                                                return Promise.reject('请输入正确的' + item.label)
                                                            }
                                                            // 新增：文案首尾不能包含换行符
                                                            const first = value.charAt(0);
                                                            const last = value.charAt(value.length - 1);
                                                            if (first === '\n' || first === '\r' || last === '\n' || last === '\r') {
                                                                return Promise.reject('文案首尾不能包含换行符');
                                                            }
                                                            const result = extractAndFilterBracketsContent(value);
                                                            if (result.extracted.length > 4) {
                                                                return Promise.reject('表情数量不得超出： 4 个')
                                                            }
                                                            return Promise.resolve()
                                                        }
                                                    }]}
                                                >
                                                    <TextAideInput isShowAjax={false} placeholder={'请输入' + item.label} style={{ width: 580 }} maxTextLength={item.restriction.textRestriction.maxLength} putInType={putInType} />
                                                </Form.Item>
                                            ))}
                                            {deliveryMode === 'DELIVERY_MODE_COMPONENT' && item.arrayProperty?.maxNumber > 1 && textDto?.[num]?.[item.value]?.length < item.arrayProperty?.maxNumber && <Form.Item style={{ marginTop: 6, marginBottom: 0 }}>
                                                <Button type="dashed" onClick={() => add()} block icon={<PlusOutlined />} >
                                                    新增{item.label}
                                                </Button>
                                            </Form.Item>}
                                        </>}
                                    </Form.List>
                                })}
                            </div>
                        </Card>
                    ))}
                    {[3, 2, 4].includes(type) && !(mediaType === 2 && type === 2) && !(type === 2 && textDto.length >= dynamicMaterialDTos.dynamicGroup.length) && <Form.Item style={{ marginTop: 10, marginBottom: 0 }}>
                        <Button type="primary" onClick={() => add(newText)} block icon={<PlusOutlined />} disabled={type === 3 && textDto.length >= 30}>
                            新增
                        </Button>
                    </Form.Item>}
                </>)}
            </Form.List>
            <Form.Item className="submit_pull">
                <Space>
                    <Button onClick={onClose}>取消</Button>
                    <Button type="primary" className="modalResetCss" onClick={() => form.submit()}>
                        确定
                    </Button>
                </Space>
            </Form.Item>
        </Form>

        {/* 批量添加 */}
        {desVisible && <AddTextS
            visible={desVisible}
            title={addSTitle}
            onClose={() => {
                setDesVisible(false)
                setAddStitle(undefined)
            }}
            onChange={(value) => {
                if (value) {
                    let valList = value
                        .split(/\r?\n/)        // 按换行符分割字符串
                        .map(line => line.trim()) // 去除每行的首尾空白
                        .filter(line => line !== ''); // 过滤掉空行
                    setText(valList)
                }
                setDesVisible(false)
                setAddStitle(undefined)
            }}
        />}
    </Modal>
}

export default React.memo(NewText)