import moment from 'moment';
import {fromWiaasProcessStep} from './ProcessHelper';
import { getDocumentIcon } from './DocumentHelper';

function formatDate(date) {
    return date ? moment(date).format("Do MMM, YYYY") : undefined;
}

function formatAddress(addressObject) {
    return `${addressObject.address_1}, ${addressObject.city}, ${addressObject.country}, ${addressObject.postcode}`;
}

export const fromWCOrder = (WCOrder) => {
    let processInfo = Object.assign({},WCOrder['delivery-process']);
    if (WCOrder['delivery-process']){
        processInfo.steps = WCOrder['delivery-process'].steps.map(step=>fromWiaasProcessStep(step));
    }
    return {
        id: WCOrder.id,
        number: WCOrder.number,
        isMyOrder: WCOrder['is_my_order'],
        dateCreated: formatDate(WCOrder['date_created']),
        dateCompleted: formatDate(WCOrder['date_completed']),
        estimatedDeliveryDate: undefined,
        vatCode: WCOrder['vat'],
        reference: WCOrder['reference'],
        tender: WCOrder['tender'],
        companyName: WCOrder['company_name'],
        projectName: WCOrder['project_name'],
        assignedTo: 'assigned to',
        fixedPrice: WCOrder.total,
        recurringPrice: WCOrder['recurring_price'],
        status: WCOrder.status,
        currency: WCOrder.currency,
        billing:{
            firstName: WCOrder.billing.first_name,
            lastName: WCOrder.billing.last_name,
            email: WCOrder.billing.email,
            address: formatAddress(WCOrder.billing)
        },
        documents: WCOrder.documents || [],
        packages: WCOrder['line_items'].map(packageLine => {
            return {
                id: packageLine['product_id'],
                orderItemId: packageLine['id'],
                name: packageLine.name,
                quantity: packageLine.quantity,
                price: packageLine.price,
                status: packageLine.status,
                paymentType: packageLine['payment_type'],
                servicePrice: packageLine['service_price'],
                serviceContractPeriod: packageLine['service_contract_period'],
                maxContractPeriod: packageLine['max_contract_period'],
                periodUnit: packageLine['period_unit'],
                recurringPrice: packageLine['recurring_price'],
                payPeriod: packageLine['pay_period'],
                shortDesc: packageLine['short_desc'],
                dateCompleted: formatDate(packageLine['date_completed']),
                additionalPackages:  packageLine['additional_packages'] ? packageLine['additional_packages'].map(additionalPackage => ({
                    idPackage: additionalPackage.id,
                    packageName: additionalPackage.name,
                })) : [],
                options: packageLine['options'] ? packageLine['options'].map(packageOption => ({
                    idPackage: packageOption.id,
                    packageName: packageOption.name,
                    groupName: packageOption['group_name'] || '',
                })) : [],
                documents:  packageLine.documents ? packageLine.documents.map(document => {
                    document['icon'] = getDocumentIcon(document.extension);
                    return document;
                }) : []
            };
        }),
        process: processInfo,
        comments: WCOrder.comments ? WCOrder.comments.map(comment => ({
            id: comment.id,
            comment: comment.content,
            username: comment.username,
            dateCreated: formatDate(comment.date),
            isOwner: comment['is_owner']
        })) : [],
        deliveryAddress: formatAddress(WCOrder.shipping),
        customer: WCOrder.customer,
        commercialLead: WCOrder['commercial_lead'],
    }
};