import styles from './style.module.css'
import Image from 'next/image'
import { InferResultRes } from '@/pages/api/base/type'
import { useContext } from 'react'
import AppContext from '@/components/hooks/createContext'
import { BASE_URL } from '@/common/constans/international'
import ShowTemplate from '../ShowTemplate'
import CopyPrompt from '../CopyPrompt'
import { useTranslation } from 'next-i18next'
import ShowRetask from '../ShowRetask'
import PromptTag from '../PromptTag'

interface Iprops {
    item: InferResultRes
}
const ShowPrompt = ({ item }: Iprops) => {
    const { t } = useTranslation('studio')
    const {
        showEdit: [showEdit, setShowEdit],
        taskDetail,
        taskId: [taskId],
        agentName: [agentName],
        requestLoading: [, setRequestLoading],
        // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
    } = useContext(AppContext)!

    const editAgain = async (executionId: string) => {
        if (!showEdit) {
            // 再次编辑打点
            const logParam = window?.logger.getParams('10000002', window?.logger.EVENT_TYPE.CLICK, {
                taskId,
                executionId: executionId,
                agentName,
            })
            window?.logger.log([logParam])
            setRequestLoading(true)
            await taskDetail(executionId)
            setRequestLoading(false)
        }
        setShowEdit(!showEdit)
    }

    const downloadPsdClick = async (executionId: string) => {
        //psd下载打点
        const logParam = window?.logger.getParams(
            window?.logger.EID.COMMON_CLICK,
            window?.logger.EVENT_TYPE.CLICK,
            {
                type: 'psdDownload',
                executionId: executionId,
                taskId,
            },
        )
        window?.logger.log([logParam])
        return true
    }

    return (
        <>
            <div className={styles.prompt}>
                <div className={styles.promptList}>
                    <div className={styles.promptDeatail}>
                        <PromptTag name={item?.agentVersion}></PromptTag>
                        {item.replicateFromTaskNo ? (
                            <ShowRetask item={item}></ShowRetask>
                        ) : item.posPrompt || item.textDescription ? (
                            <>
                                {item?.sdCheckpoint ? (
                                    <PromptTag name={item.sdCheckpoint}></PromptTag>
                                ) : null}

                                {item?.sdSamplingMethod ? (
                                    <PromptTag name={item.sdSamplingMethod}></PromptTag>
                                ) : null}

                                <CopyPrompt
                                    posPrompt={item?.posPrompt || item.textDescription}
                                    executionId={item?.executionId}
                                    negPrompt={item?.negPrompt}
                                ></CopyPrompt>
                            </>
                        ) : (
                            <ShowTemplate item={item}></ShowTemplate>
                        )}
                        {item?.waitingCnt ? (
                            <p className={`ag16 ${styles.waitingCnt}`}>
                                {t('showPrompt.inline', { waitingCnt: item?.waitingCnt })}
                            </p>
                        ) : null}
                    </div>

                    {/* 下载和编辑 */}
                    <div>
                        <div
                            className={`${styles.reeditButton} ag14`}
                            onClick={() => {
                                editAgain(item.executionId)
                            }}
                        >
                            <Image
                                src="/setting.svg"
                                alt="setting"
                                width={14}
                                height={14}
                                className={styles.setting}
                            ></Image>
                            <span>{t('showPrompt.editAgain')}</span>
                        </div>
                        {item?.status === 'Success' ? (
                            <a
                                download
                                href={`/api/v1/agent/task/downloadPsd?executionId=${item.executionId}`}
                                className={`${styles.reeditButton} ag14`}
                                onClick={(e) => {
                                    e.preventDefault()
                                    downloadPsdClick(item.executionId).then((result) => {
                                        if (result) {
                                            window.open(
                                                `${BASE_URL}/v1/agent/task/downloadPsd?executionId=${item.executionId}`,
                                                '_blank',
                                            )
                                        }
                                    })
                                }}
                            >
                                <Image
                                    src="/document-download.svg"
                                    alt="document-download"
                                    width={14}
                                    height={14}
                                    className={styles.setting}
                                ></Image>
                                <span>{t('showPrompt.downliadPSD')}</span>
                            </a>
                        ) : null}
                    </div>
                </div>
                <div className={`ag14 ${styles.time}`}>
                    <span className={styles.executeSeq}>
                        {t('execution', { executeSeq: item?.executeSeq })}
                    </span>
                    <span>{item?.executionTime}</span>
                </div>
            </div>
        </>
    )
}

export default ShowPrompt
