"""
Classes to represent AnswerFactory domain objects

Contact: mtrotter@digitalglobe.com
Contact: jerikson@digitalglobe.com
Contact: mchaudry@digitalglobe.com
"""
from builtins import object
from builtins import str

from gbdxtools.answerfactory import Project as ProjectService
from gbdxtools.answerfactory import Recipe as RecipeService
from gbdxtools.simpleworkflows import Task

import re
import uuid

REGEX_RECIPE_NAME = re.compile(r'[a-zA0-9\-_\s()\\]*')
REGEX_VERSIONED_TASK_NAME = re.compile(r'^(?P<task_name>.*):\d+\.\d+\.\d+$')
REGEX_PARSE_S3_PATH = re.compile(r'^s3://(?P<bucket_name>[^/]+)(?P<key>.*)$')

RECIPE_TYPES = ['workflow', 'partitioned-workflow', 'vector-aggregation', 'es-query']
RECIPE_INPUT_TYPES = ['none', 'acquisition', 'acquisitions', 'esri-service', 
                      'seasonal-acquisition', 'vector-service']
RECIPE_OUTPUT_TYPES = ['esri-service', 'es-query-service', 'vector-service']

ACQUISITION_TASKS = ['AOP_Strip_Processor']
VECTOR_INPUT_TASKS = ['CompareVectors', 'GenerateRandomVectors']
VECTOR_OUTPUT_TASKS = ['IngestGeoJsonToVectorServices', 'IngestItemJsonToVectorServices',
                'IngestShpToVectorServices', 'WriteGeoJsonToVectorServices',
                'WriteItemJsonToVectorServices', 'WriteShpToVectorServices']

VECTOR_INGEST_BUCKETS = ['vector-shapefile-ingest-dev', 'vector-shapefile-ingest-prod',
                         'vector-test-gbdx-vectors', 'vector-gbdx-vectors-prod']
VECTOR_MODEL_BUCKETS = ['vector-lulc-models']
S3_UPLOADS = ['StageDataToS3']

SUBSTITUTION_VAR_WORKFLOW_ACCOUNT_ID = '{account_id}'
SUBSTITUTION_VAR_WORKFLOW_USER = '{user}'
SUBSTITUTION_VAR_WORKFLOW_PROJECT_ID = '{project_id}'
SUBSTITUTION_VAR_WORKFLOW_PROJECT_NAME = '{project_name}'
SUBSTITUTION_VAR_WORKFLOW_RECIPE_ID = '{recipe_id}'
SUBSTITUTION_VAR_WORKFLOW_RECIPE_NAME = '{recipe_name}'
SUBSTITUTION_VAR_WORKFLOW_PROJECT_GEOMETRY = '{project_geometry}'
SUBSTITUTION_VAR_WORKFLOW_TASK_NAME = '{task_name}'

SUBSTITUTION_VAR_WORKFLOW_VECTOR_SERVICE_OUTPUT_RUN_ID = '{run_id}'
SUBSTITUTION_VAR_WORKFLOW_VECTOR_SERVICE_OUTPUT_VECTOR_HOST = '{vector_host}'
SUBSTITUTION_VAR_WORKFLOW_VECTOR_SERVICE_OUTPUT_VECTOR_INGEST_BUCKET = '{vector_ingest_bucket}'
SUBSTITUTION_VAR_WORKFLOW_VECTOR_SERVICE_OUTPUT_INGEST_DATE = '{ingest_date}'
SUBSTITUTION_VAR_WORKFLOW_VECTOR_SERVICE_OUTPUT_QUERY_INDEX = '{query_index}'

SUBSTITUTION_VAR_WORKFLOW_ACQUISITION_INPUT_RASTER_PATH = '{raster_path}'
SUBSTITUTION_VAR_WORKFLOW_ACQUISITION_INPUT_RASTER_PATH_I = '{raster_path_%d}'
SUBSTITUTION_VAR_WORKFLOW_ACQUISITION_INPUT_CATALOG_ID = '{catalog_id}'
SUBSTITUTION_VAR_WORKFLOW_ACQUISITION_INPUT_CATALOG_ID_I = '{catalog_id_%d}'
SUBSTITUTION_VAR_WORKFLOW_ACQUISITION_INPUT_ACQUISITION_ID = '{acquisition_id}'
SUBSTITUTION_VAR_WORKFLOW_ACQUISITION_INPUT_ACQUISITION_ID_I = '{acquisition_id_%d}'
SUBSTITUTION_VAR_WORKFLOW_ACQUISITION_INPUT_ACQUISITION_DATE = '{acquisition_date}'
SUBSTITUTION_VAR_WORKFLOW_ACQUISITION_INPUT_ACQUISITION_DATE_I = '{acquisition_date_%d}'
SUBSTITUTION_VAR_WORKFLOW_ACQUISITION_INPUT_ACQUISITION_BANDS = '{acquisition_bands}'
SUBSTITUTION_VAR_WORKFLOW_ACQUISITION_INPUT_ACQUISITION_BANDS_I = '{acquisition_bands_%d}'
SUBSTITUTION_VAR_WORKFLOW_ACQUISITION_INPUT_ACQUISITION_GEOMETRY = '{acquisition_geometry}'
SUBSTITUTION_VAR_WORKFLOW_ACQUISITION_INPUT_ACQUISITION_GEOMETRY_I = '{acquisition_geometry_%d}'
SUBSTITUTION_VAR_WORKFLOW_ACQUISITION_INPUT_ACQUISITION_INDEX_I = '{acquisition_index_%d}'
SUBSTITUTION_VAR_WORKFLOW_ACQUISITION_INPUT_ACQUISITION_INTERSECTION = '{acquisition_intersection}'
SUBSTITUTION_VAR_WORKFLOW_ACQUISITION_INPUT_PROJECT_ACQUISITION_INTERSECTION = \
    '{project_acquisition_intersection}'
SUBSTITUTION_VAR_WORKFLOW_ACQUISITION_INPUT_MODEL_LOCATION_S3 = '{model_location_s3}'

SUBSTITUTION_VAR_WORKFLOW_VECTOR_SERVICE_INPUT_QUERY_STRING = '{query_string}'
SUBSTITUTION_VAR_WORKFLOW_VECTOR_SERVICE_INPUT_QUERY_STRING_I = '{query_string_%d}'
SUBSTITUTION_VAR_WORKFLOW_VECTOR_SERVICE_INPUT_QUERY_INDEX = '{query_index}'
SUBSTITUTION_VAR_WORKFLOW_VECTOR_SERVICE_INPUT_QUERY_INDEX_I = '{query_index_%d}'
SUBSTITUTION_VAR_WORKFLOW_VECTOR_SERVICE_INPUT_RESULT_GEOMETRY = '{result_geometry}'

FMT_S3_INGEST_PATH = 's3://' \
                     + '/'.join([
    SUBSTITUTION_VAR_WORKFLOW_VECTOR_SERVICE_OUTPUT_VECTOR_INGEST_BUCKET,
    SUBSTITUTION_VAR_WORKFLOW_RECIPE_ID,
    SUBSTITUTION_VAR_WORKFLOW_VECTOR_SERVICE_OUTPUT_RUN_ID,
    SUBSTITUTION_VAR_WORKFLOW_TASK_NAME])


class RecipePrerequisite(object):
    def __init__(self, **kwargs):
        self._id = kwargs.get('id', None)
        self._aggregator = kwargs.get('aggregator', None)
        self._operator = kwargs.get('operator', None)
        self._properties = kwargs.get('properties', {})

    @property
    def id(self):