package com.justeat.mickeydb.scoping;

import com.google.inject.Inject;
import com.justeat.mickeydb.MickeyAssembler;
import com.justeat.mickeydb.MickeyEnvironment;
import com.justeat.mickeydb.MickeyModel;
import com.justeat.mickeydb.ModelUtil;
import com.justeat.mickeydb.mickeyLang.ActionStatement;
import com.justeat.mickeydb.mickeyLang.AlterTableAddColumnStatement;
import com.justeat.mickeydb.mickeyLang.AlterTableRenameStatement;
import com.justeat.mickeydb.mickeyLang.ColumnSourceRef;
import com.justeat.mickeydb.mickeyLang.ContentUriParamSegment;
import com.justeat.mickeydb.mickeyLang.ContentUriQueryParam;
import com.justeat.mickeydb.mickeyLang.DropTableStatement;
import com.justeat.mickeydb.mickeyLang.DropTriggerStatement;
import com.justeat.mickeydb.mickeyLang.DropViewStatement;
import com.justeat.mickeydb.mickeyLang.MickeyFile;
import com.justeat.mickeydb.mickeyLang.MickeyLangPackage;
import com.justeat.mickeydb.mickeyLang.MigrationBlock;
import com.justeat.mickeydb.mickeyLang.NewColumn;
import com.justeat.mickeydb.mickeyLang.OldColumn;
import com.justeat.mickeydb.mickeyLang.SingleSourceTable;
import com.justeat.mickeydb.mickeyLang.UpdateColumnExpression;
import java.util.List;
import org.apache.log4j.Logger;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.common.util.TreeIterator;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EReference;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.xtext.naming.IQualifiedNameProvider;
import org.eclipse.xtext.resource.IEObjectDescription;
import org.eclipse.xtext.resource.IResourceDescriptions;
import org.eclipse.xtext.scoping.IScope;
import org.eclipse.xtext.scoping.impl.AbstractDeclarativeScopeProvider;
import org.eclipse.xtext.xbase.lib.Exceptions;
import org.eclipse.xtext.xbase.lib.Functions.Function1;
import org.eclipse.xtext.xbase.lib.IterableExtensions;
import org.eclipse.xtext.xbase.lib.IteratorExtensions;
import org.eclipse.xtext.xbase.lib.ListExtensions;

@SuppressWarnings("all")
public class MickeyLangScopeProvider extends AbstractDeclarativeScopeProvider {
  private final static Logger LOG = Logger.getLogger(MickeyLangScopeProvider.class);
  
  @Inject
  private IQualifiedNameProvider nameProvider;
  
  @Inject
  private IResourceDescriptions resourceDescriptions;
  
  @Inject
  private MickeyAssembler assembler;
  
  @Inject
  private MickeyEnvironment mickeyEnvironment;
  
  public IScope scope_AlterTableAddColumnStatement_table(final AlterTableAddColumnStatement context, final EReference ref) {
    throw new Error("Unresolved compilation problems:"
      + "\nThe method getModel(EObject) from the type ModelUtil refers to the missing type MickeyFile"
      + "\ndatabaseName cannot be resolved");
  }
  
  public IScope scope_AlterTableRenameStatement_table(final AlterTableRenameStatement context, final EReference ref) {
    throw new Error("Unresolved compilation problems:"
      + "\nThe method getModel(EObject) from the type ModelUtil refers to the missing type MickeyFile"
      + "\ndatabaseName cannot be resolved");
  }
  
  public IScope scope_ActionStatement_type(final ActionStatement context, final EReference ref) {
    throw new Error("Unresolved compilation problems:"
      + "\nThe method getModel(EObject) from the type ModelUtil refers to the missing type MickeyFile"
      + "\ndatabaseName cannot be resolved");
  }
  
  public IScope scope_SingleSourceTable_tableReference(final SingleSourceTable context, final EReference ref) {
    throw new Error("Unresolved compilation problems:"
      + "\nThe method getModel(EObject) from the type ModelUtil refers to the missing type MickeyFile"
      + "\ndatabaseName cannot be resolved");
  }
  
  public IScope scope_ColumnSourceRef_column(final ColumnSourceRef context, final EReference ref) {
    throw new Error("Unresolved compilation problems:"
      + "\nThe method getModel(EObject) from the type ModelUtil refers to the missing type MickeyFile"
      + "\ndatabaseName cannot be resolved");
  }
  
  public IScope scope_NewColumn_column(final NewColumn context, final EReference ref) {
    throw new Error("Unresolved compilation problems:"
      + "\nThe method getModel(EObject) from the type ModelUtil refers to the missing type MickeyFile"
      + "\ndatabaseName cannot be resolved");
  }
  
  public IScope scope_OldColumn_column(final OldColumn context, final EReference ref) {
    throw new Error("Unresolved compilation problems:"
      + "\nThe method getModel(EObject) from the type ModelUtil refers to the missing type MickeyFile"
      + "\ndatabaseName cannot be resolved");
  }
  
  public IScope scope_UpdateColumnExpression_columnName(final UpdateColumnExpression context, final EReference ref) {
    throw new Error("Unresolved compilation problems:"
      + "\nThe method or field targetTableName is undefined for the type DMLStatement"
      + "\nThe method getModel(EObject) from the type ModelUtil refers to the missing type MickeyFile"
      + "\ndatabaseName cannot be resolved");
  }
  
  public IScope scope_ColumnSourceRef_source(final ColumnSourceRef context, final EReference ref) {
    throw new Error("Unresolved compilation problems:"
      + "\nThe method or field findAllSingleSources is undefined for the type SelectExpression");
  }
  
  public IScope scope_DropViewStatement_view(final DropViewStatement context, final EReference ref) {
    throw new Error("Unresolved compilation problems:"
      + "\nThe method getModel(EObject) from the type ModelUtil refers to the missing type MickeyFile"
      + "\ndatabaseName cannot be resolved");
  }
  
  public IScope scope_DropTableStatement_table(final DropTableStatement context, final EReference ref) {
    throw new Error("Unresolved compilation problems:"
      + "\nThe method getModel(EObject) from the type ModelUtil refers to the missing type MickeyFile"
      + "\ndatabaseName cannot be resolved");
  }
  
  public IScope scope_DropTriggerStatement_trigger(final DropTriggerStatement context, final EReference ref) {
    throw new Error("Unresolved compilation problems:"
      + "\nThe method getModel(EObject) from the type ModelUtil refers to the missing type MickeyFile"
      + "\ndatabaseName cannot be resolved");
  }
  
  public IScope scope_MigrationBlock_from(final MigrationBlock context, final EReference ref) {
    throw new Error("Unresolved compilation problems:"
      + "\nThe method getModel(EObject) from the type ModelUtil refers to the missing type MickeyFile"
      + "\ndatabaseName cannot be resolved");
  }
  
  public MickeyModel getMickeyModelInScope(final EObject context) {
    List<MickeyFile> files = null;
    boolean _isStandalone = this.mickeyEnvironment.isStandalone();
    String _plus = ("[Get Model In Scope] Standalone: " + Boolean.valueOf(_isStandalone));
    MickeyLangScopeProvider.LOG.debug(_plus);
    boolean _isStandalone_1 = this.mickeyEnvironment.isStandalone();
    if (_isStandalone_1) {
      Resource _eResource = context.eResource();
      ResourceSet _resourceSet = _eResource.getResourceSet();
      EList<Resource> _resources = _resourceSet.getResources();
      final Function1<Resource, MickeyFile> _function = (Resource it) -> {
        TreeIterator<EObject> _allContents = it.getAllContents();
        EObject _head = IteratorExtensions.<EObject>head(_allContents);
        return ((MickeyFile) _head);
      };
      List<MickeyFile> _map = ListExtensions.<Resource, MickeyFile>map(_resources, _function);
      files = _map;
    } else {
      Iterable<IEObjectDescription> _exportedObjectsByType = this.resourceDescriptions.getExportedObjectsByType(MickeyLangPackage.Literals.MICKEY_FILE);
      final Function1<IEObjectDescription, MickeyFile> _function_1 = (IEObjectDescription e) -> {
        EObject _eObjectOrProxy = e.getEObjectOrProxy();
        EObject _resolve = EcoreUtil.resolve(_eObjectOrProxy, context);
        return ((MickeyFile) _resolve);
      };
      Iterable<MickeyFile> _map_1 = IterableExtensions.<IEObjectDescription, MickeyFile>map(_exportedObjectsByType, _function_1);
      List<MickeyFile> _list = IterableExtensions.<MickeyFile>toList(_map_1);
      files = _list;
    }
    int _size = files.size();
    boolean _greaterThan = (_size > 0);
    if (_greaterThan) {
      int _size_1 = files.size();
      String _plus_1 = ("[Get Model In Scope] Files: " + Integer.valueOf(_size_1));
      MickeyLangScopeProvider.LOG.debug(_plus_1);
      MigrationBlock migration = ModelUtil.<MigrationBlock>getAncestorOfType(context, MigrationBlock.class);
      try {
        return this.assembler.assemble(files, migration);
      } catch (final Throwable _t) {
        if (_t instanceof Exception) {
          final Exception e = (Exception)_t;
          MickeyLangScopeProvider.LOG.debug("[Get Model In Scope] Failed with error ", e);
        } else {
          throw Exceptions.sneakyThrow(_t);
        }
      }
    }
    MickeyLangScopeProvider.LOG.debug("[Get Model In Scope] Failed ");
    return null;
  }
  
  public IScope scopeTableColumns(final EObject context, final String dbName, final String tableName) {
    throw new Error("Unresolved compilation problems:"
      + "\nThe method or field viewResultColumns is undefined for the type CreateViewStatement"
      + "\nThe method getSnapshot() from the type MickeyDatabaseModel refers to the missing type SqliteDatabaseSnapshot"
      + "\nThe field MickeyDatabaseModel.initTables refers to the missing type CreateTableStatement"
      + "\nThe field MickeyDatabaseModel.initViews refers to the missing type CreateViewStatement"
      + "\ngetTableDefinition cannot be resolved"
      + "\n== cannot be resolved"
      + "\nname cannot be resolved"
      + "\nequals cannot be resolved"
      + "\n== cannot be resolved"
      + "\nname cannot be resolved"
      + "\nequals cannot be resolved");
  }
  
  public IScope scope_ContentUriParamSegment_param(final ContentUriParamSegment context, final EReference ref) {
    throw new Error("Unresolved compilation problems:"
      + "\nThe method getModel(EObject) from the type ModelUtil refers to the missing type MickeyFile"
      + "\ndatabaseName cannot be resolved");
  }
  
  public IScope scope_ContentUriQueryParam_column(final ContentUriQueryParam context, final EReference ref) {
    throw new Error("Unresolved compilation problems:"
      + "\nThe method getModel(EObject) from the type ModelUtil refers to the missing type MickeyFile"
      + "\ndatabaseName cannot be resolved");
  }
}
