import React, {Component} from 'react';
import {Row, Col, Container,  Popover, PopoverBody, PopoverHeader} from 'reactstrap';
import {API_SERVER} from '../../../../config';
import FileDownloader from '../../../../helpers/FileDownloader';
import {orderTexts} from '../../../../constants/ordersConstants';

const fileHandler = new FileDownloader();

class PackageInfo extends Component {
    constructor(props) {
        super(props);

        this.toggle = this.toggle.bind(this);
        this.state = {
          popoverOpen: false
        };
    }

    toggle() {
        this.setState({
          popoverOpen: !this.state.popoverOpen
        });
    }

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

    render() {
        const {orderPackage, onViewChange} = this.props;
        const shouldShowPriceInfo = orderPackage.packageRecuringPrice > 0 || orderPackage.packageServicePrice > 0;

        return (
            <Container fluid={true} id="package-info" className="order-package-info">
                <Row>
                    <Col xl="3">
                        <div className="subtitle">
                            <h6>
                                {orderTexts.labels.PACKAGE_PRICE}: {' '}
                                { shouldShowPriceInfo &&
                                    <i className="price-info-btn fa fa-info-circle"
                                        id={'price-info-' + orderPackage.idPackage}
                                        onClick={this.toggle}></i>
                                }
                            </h6>

                        </div>
                        <div>
                            {orderTexts.labels.TOTAL_DELVIERY_PRICE}: {' '}
                            {(orderPackage.units * orderPackage.packageFixedPrice).toLocaleString() + ' ' + orderPackage.packageCurrency.currency}
                        </div>
                        {
                            shouldShowPriceInfo &&
                            <div>
                                {orderTexts.labels.TOTAL_RECURRENT_PRICE}:{' '}
                                {(orderPackage.units * (orderPackage.packageRecuringPrice + orderPackage.packageServicePrice)).toLocaleString()} {orderPackage.packageCurrency.currency}
                            </div>
                        }

                        { shouldShowPriceInfo &&
                            <Popover placement="bottom"
                                        isOpen={this.state.popoverOpen}
                                        target={'price-info-' + orderPackage.idPackage}
                                        container={'price-info-' + orderPackage.idPackage}
                                        className="price-info-popover"
                                        toggle={this.toggle}>
                               <PopoverHeader>{orderPackage.paymentType}</PopoverHeader>
                               <PopoverBody>
                                   <div>
                                       { orderPackage.packageRecuringPrice > 0 &&
                                           <div className="package-price-recurrent">
                                               <span className="price-info-title">{orderTexts.labels.RECURRENT_PRICE}: </span>
                                               {(orderPackage.units * orderPackage.packageRecuringPrice).toLocaleString()} {orderPackage.packageCurrency.currency} / {orderPackage.periodUnit}
                                               {
                                                   orderPackage.packagePayPeriod > 0 &&
                                                   <span>
                                                       for {orderPackage.packagePayPeriod} {orderPackage.periodUnit}
                                                   </span>
                                               }
                                           </div>
                                       }

                                       { orderPackage.packageServicePrice > 0 &&
                                           <div className="services-price-recurrent">
                                               <span className="price-info-title">{orderTexts.labels.SERVICES_PRICE}: </span>
                                               {(orderPackage.units * orderPackage.packageServicePrice).toLocaleString()} {orderPackage.packageCurrency.currency} / {orderPackage.periodUnit}
                                               {
                                                   orderPackage.servicesContractPeriod > 0 &&
                                                   <span>
                                                       for {orderPackage.servicesContractPeriod} {orderPackage.periodUnit}
                                                   </span>
                                               }
                                                <span> {orderTexts.labels.EXTEND} {orderPackage.periodUnit} (Max {orderPackage.maxContractPeriod} {orderPackage.periodUnit})</span>
                                           </div>
                                       }
                                   </div>
                               </PopoverBody>
                            </Popover>
                        }
                    </Col>
                    {
                        orderPackage.options.length > 0 &&
                        <Col xl="3">
                            <div className="subtitle"><h6>{orderTexts.labels.OPTIONS}:</h6></div>
                            {
                                orderPackage.options.map((option, index) => <div key={'option-'+index}>
                                    {option.groupName}: {option.packageName}
                                </div>)
                            }
                        </Col>
                    }
                    {
                        orderPackage.additionalPackages.length > 0 &&
                        <Col xl="3">
                            <div className="subtitle"><h6>{orderTexts.labels.ADDITIONAL_PACKAGES}:</h6></div>
                            {
                                orderPackage.additionalPackages.map((additional, index) => <div key={'additional-'+index}>
                                    {additional.packageName}
                                </div>)
                            }
                        </Col>
                    }
                    {
                        orderPackage.documents &&
                        <Col xl="2">
                            <div className="subtitle"><h6>{orderTexts.labels.DOCUMENTS}:</h6></div>
                            {
                                orderPackage.documents.length > 0 ?
                                <div className="link-to-docs" onClick={()=>onViewChange('documents')}>{orderTexts.buttons.SEE_DOCUMENTS}</div>:
                                <span>{orderTexts.labels.NO_DOCUMENTS}</span>
                            }
                        </Col>
                    }
                </Row>
            </Container>
        );
    }
}

export default PackageInfo;
