import React, {Component} from 'react';
import {Col} from 'reactstrap';
import {API_SERVER} from '../../../config';
import FileDownloader from '../../../helpers/FileDownloader';
import {coMarketTexts} from '../../../constants/coMarketConstants';

const fileHandler = new FileDownloader();

class PackageInfo extends Component {
    downloadDocument(document){
        const fileUrl = `${API_SERVER}/utils/api/downloadFile?idDocument=${document.idDocument}&fileName=${document.documentName}.${document.extension}`
        const fileName = document.documentName + '.' + document.extension;
        fileHandler.download(fileUrl, fileName);
    }

    render() {
        const {packageInfo, documents} = this.props;

        return (
            <div id="shop-package-general-info" className="shop-package-general-info">
                { packageInfo &&
                    <span>
                        <Col lg="12"
                            className="shop-package-title"><h4>{packageInfo.name}</h4></Col>
                        <Col lg="12"
                            className="shop-package-reference"><h6>{packageInfo.reference}</h6></Col>
                        <Col lg="12"
                            dangerouslySetInnerHTML={{__html: packageInfo.shortDescription}}
                            className="shop-package-full-description"></Col>
                        <Col lg="12"
                            className="shop-package-details-country">
                                <div className="shop-package-label">{coMarketTexts.labels.AVAILABLE_IN}:</div>
                                <div className="shop-package-text">
                                    {packageInfo.country} <span className={'flag-icon flag-icon-' + packageInfo.countryCode}></span>
                                </div>
                        </Col>
                    </span>
                }
                {
                        documents && documents.length > 0 &&
                        <Col lg="12"
                            className="shop-package-details-documents">
                                <div className="shop-package-label">{coMarketTexts.labels.DOCUMENTS}:</div>
                                {
                                    documents.map((document) =>
                                        <div key={document.idDocument}>
                                            <span className="document-link"
                                                onClick={() => {this.downloadDocument(document)}}>
                                                <i className="fa fa-file" aria-hidden="true"></i> {document.documentName} ({document.extension})
                                            </span>
                                        </div>
                                    )
                                }
                        </Col>
                }
            </div>
        );
    }
}

export default PackageInfo;
