import os
from pywps import Process, LiteralInput, ComplexOutput
from pywps import FORMATS
from pywps.app.Common import Metadata
from pywps.inout.outputs import MetaLink, MetaFile
import json

import logging
LOGGER = logging.getLogger("PYWPS")


class MultipleOutputs(Process):
    def __init__(self):
        inputs = [
            LiteralInput('count', 'Number of output files',
                         abstract='The number of generated output files.',
                         data_type='integer',
                         default='2',
                         allowed_values=[1, 2, 5, 10])]
        outputs = [
            ComplexOutput('output', 'METALINK output',
                          abstract='Testing metalink output',
                          as_reference=True,
                          supported_formats=[FORMATS.METALINK])
        ]

        super(MultipleOutputs, self).__init__(
            self._handler,
            identifier='multiple_outputs',
            title='Multiple Outputs',
            abstract='Produces multiple files and returns a document'
                     ' with references to these files.',
            metadata=[
                Metadata('User Guide', 'https://emu.readthedocs.io/en/latest/processes.html'),  # noqa
            ],
            version='1.1',
            inputs=inputs,
            outputs=outputs,
            store_supported=True,
            status_supported=True
        )

    def _handler(self, request, response):
        response.update_status('PyWPS Process started.', 0)

        LOGGER.info("starting ...")
        if 'count' in request.inputs:
            max_outputs = request.inputs['count'][0].data
        else:
            max_outputs = 2
        # generate outputs
        ml = MetaLink('test-ml-1', 'Testing MetaLink with text files.')
        for i in range(max_outputs):
            # with open(os.path.join(self.workdir, "output_{}.txt".format(i)), 'w') as fp:
            #    fp.write("my output file number %s" % i)
            # response.outputs['output']['out_{}'.format(i)].file = fp.name
            o = ComplexOutput(
                'output_{}'.format(i), 'Test output',
                abstract='Test output',
                as_reference=True,
                supported_formats=[FORMATS.TEXT])
            o.data = u'output: {}'.format(i)
            o.workdir = self.workdir
            mf = MetaFile(o)
            ml.append(mf)
        response.outputs['output'].data = ml.xml

        response.update_status('PyWPS Process completed.', 100)
        return response

@@ -1806,18 +1806,40 @@
  files.'
+, workdir=self.workdir
 )%0A
-
         
@@ -1883,416 +1883,67 @@
     
-# with open(os.path.join(self.workdir, %22output_%7B%7D.txt%22.format(i)), 'w') as fp:%0A            #    fp.write(%22my output file number %25s%22 %25 i)%0A            # response.outputs%5B'output'%5D%5B'out_%7B%7D'.format(i)%5D.file = fp.name%0A            o = ComplexOutput(%0A                'output_%7B%7D'.format(i), 'Test output',%0A                abstract='Test output',%0A                as_reference=True,%0A                supported_
+mf = MetaFile('output_%7B%7D'.format(i), 'Test output', 
 format
-s=%5B
+=
 FORM
@@ -1950,17 +1950,16 @@
 ATS.TEXT
-%5D
 )%0A      
@@ -1968,17 +1968,18 @@
     
-o
+mf
 .data = 
 u'ou
@@ -1974,17 +1974,16 @@
 .data = 
-u
 'output:
@@ -2001,74 +2001,8 @@
 (i)%0A
-            o.workdir = self.workdir%0A            mf = MetaFile(o)%0A
     

update metafile