
import { Button } from 'antd';
import * as React from 'react';
import { useParams } from 'react-router-dom';
import { HttpRepository, HttpMethod, HttpRoute } from '../../core/repository/http_repository';


export const pathStabilityScreen = '/stability/preview/usecase/'

interface IStabilityCheckResponce {
    status: "rejected" | "fulfilled";
    value: undefined | string;
    index: number;
}
interface IStability {
    status: boolean;
    detail: string;
}

export const StabilityPreviewScreen: React.FunctionComponent = () => {
    const id = useParams().id
    const [stabilityResult, setStability] = React.useState<IStability[] | null>(null);
    React.useEffect(() => {
        const stabilityCheck = async () => {
            const result = await HttpRepository.jsonRequest<Array<string>>(HttpMethod.GET, '/' + id + '/generation/step-structure.json')
            const promises = []
            for (let i = 0; i !== result.length; i++) {
                const stabilitySubId = i + 1
                promises.push(HttpRepository.jsonRequest<Array<string>>(HttpMethod.GET, '/' + id + '/generation/stability/' + stabilitySubId + '/geometry.json'))
            }
            const stabilityCheck = await (await Promise.allSettled(promises)).map<IStability>((element, index) => {
                return {
                    status: element.status === 'fulfilled' ? true : false,
                    detail: result[index],
                }
            })
            setStability(stabilityCheck)
        };
        stabilityCheck()
    }, []);
    return (<div>
        {stabilityResult != null ? (<>
            {stabilityResult.map((el, index) => {
                return (<div><div>{el.detail}</div> <div>{el.status ? (<>Sucses</>) : (<><Button onClick={async () => {
                    await HttpRepository.jsonRequest(HttpMethod.POST, '/assembly/stability/write/computed', {
                        "id": id,
                        "buildNumber": (index + 1).toString()
                    })
                }}>need input </Button></>)}</div> </div>)
            })}

        </>) : (<div>loading</div>)}

    </div>);
};


