import React from 'react';
import superagent from 'superagent';

import * as ApiUtils from '../CloudApi/ApiUtils.js';
import FabricatorWrapper from './FabricatorWrapper.jsx';
import JobComponent from './JobComponent.jsx';


export default class Job extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            job: null
        };
    }

    async reloadJob() {
        try {
            let schemas = await ApiUtils.getFabricatorSchemas();
            this.setState({ schemas });

            const jobId = this.props.match.params.jobId;
            const res = await superagent.get(`/api/admin/fabricator/job/${jobId}`).accept('json');
            //res.body.binLocation = this.hashToThreeDigitsMD5(jobId);
            this.setState({job: res.body});
        } catch (e) {
            console.log(e);
        }
    }

    async componentDidMount() {
        await this.reloadJob();
    }

    // Start a new normal function for this class
    hashToThreeDigitsMD5(str) {
        /*
        const hashObject = createHash('md5').update(str).digest('hex');
        return (parseInt(hashObject, 16) % 1000) / 10;
        */
       return 0;
    }

    render() {
        if (this.state.job) {
            return (
                <FabricatorWrapper title={`Job Summary for ${this.state.job.id}`} loading={false}>    
                    <JobComponent job={this.state.job} schemas={this.state.schemas} />
                </FabricatorWrapper>
            );
        } else {
            return null;
        }
    }
}
