/**
 * Copyright (C) 2009-2013 Akiban Technologies, Inc.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.akiban.sql.optimizer;

import com.akiban.server.rowdata.SchemaFactory;
import com.akiban.sql.compiler.ASTTransformTestBase;
import com.akiban.sql.compiler.BooleanNormalizer;
import com.akiban.sql.compiler.TypeComputer;
import com.akiban.sql.parser.SQLParser;
import com.akiban.sql.views.ViewDefinition;

import com.akiban.ais.model.AkibanInformationSchema;
import com.akiban.server.service.ServiceManager;
import com.akiban.server.service.config.TestConfigService;
import com.akiban.server.service.functions.FunctionsRegistryImpl;

import com.akiban.server.service.servicemanager.GuicedServiceManager;
import com.akiban.server.service.text.FullTextIndexService;
import com.akiban.server.service.text.FullTextIndexServiceImpl;
import org.junit.Before;
import org.junit.Ignore;

import java.io.File;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Ignore
public class OptimizerTestBase extends ASTTransformTestBase
{
    
    protected OptimizerTestBase(String caseName, String sql, 
                                String expected, String error) {
        super(caseName, sql, expected, error);
    }

    public static final File RESOURCE_DIR = 
        new File("src/test/resources/"
                 + OptimizerTestBase.class.getPackage().getName().replace('.', '/'));
    public static final String DEFAULT_SCHEMA = "test";

    protected static ServiceManager sm = prepareServices();
    protected static int populateDelayInterval = 1000;
    protected static int updateInterval = 3000;
    
    // Base class has all possible transformers for convenience.
    protected AISBinder binder;
    protected TypeComputer typeComputer;
    protected BooleanNormalizer booleanNormalizer;
    protected SubqueryFlattener subqueryFlattener;
    protected DistinctEliminator distinctEliminator;
    
    @Before
    public void makeTransformers() throws Exception {
        parser = new SQLParser();
        parser.setNodeFactory(new BindingNodeFactory(parser.getNodeFactory()));
        unparser = new BoundNodeToString();
        typeComputer = new FunctionsTypeComputer(new FunctionsRegistryImpl());
        booleanNormalizer = new BooleanNormalizer(parser);
        subqueryFlattener = new SubqueryFlattener(parser);
        distinctEliminator = new DistinctEliminator(parser);
    }

    protected static ServiceManager serviceManager()
    {
        return sm;
    }

    public static AkibanInformationSchema parseSchema(List<File> ddls) throws Exception {
        StringBuilder ddlBuilder = new StringBuilder();
        for (File ddl : ddls)
            ddlBuilder.append(fileContents(ddl));
        String sql = ddlBuilder.toString();
        SchemaFactory schemaFactory = new SchemaFactory(DEFAULT_SCHEMA);
        AkibanInformationSchema ret = schemaFactory.ais(serviceManager(), sql);
        return ret;
    }

    public static AkibanInformationSchema parseSchema(File ddl) throws Exception {
        return parseSchema(Collections.singletonList(ddl));
    }

    protected AkibanInformationSchema loadSchema(List<File> ddl) throws Exception {
        AkibanInformationSchema ais = parseSchema(ddl);
        binder = new AISBinder(ais, DEFAULT_SCHEMA);
        if (!ais.getViews().isEmpty())
            new TestBinderContext(parser, binder, typeComputer);
        return ais;
    }

    protected AkibanInformationSchema loadSchema(File ddl) throws Exception {
        return loadSchema(Collections.singletonList(ddl));
    }

    protected static class TestBinderContext extends AISBinderContext {
        public TestBinderContext(SQLParser parser, AISBinder binder, TypeComputer typeComputer) {
            this.parser = parser;
            this.defaultSchemaName = DEFAULT_SCHEMA;
            setBinderAndTypeComputer(binder, typeComputer);
        }
    }

     
    protected static ServiceManager createServiceManager(Map<String, String> startupConfigProperties)
    {
        return new GuicedServiceManager(serviceBindingsProvider());
    }

    protected static GuicedServiceManager.BindingsConfigurationProvider serviceBindingsProvider()
    {
        return GuicedServiceManager
                    .testUrls()
                        .bindAndRequire(FullTextIndexService.class,
                                        FullTextIndexServiceImpl.class);
    }
    

    protected static Map<String, String> startupConfigProperties() {
        Map<String, String> properties = new HashMap<>();
        properties.put("akserver.text.indexpath", "/tmp/aktext");
        properties.put(FullTextIndexServiceImpl.UPDATE_INTERVAL, Long.toString(updateInterval));
        properties.put(FullTextIndexServiceImpl.POPULATE_DELAY_INTERVAL, Long.toString(populateDelayInterval));
        return properties;
    }
    
    private static ServiceManager prepareServices()
    {
        System.setProperty("akiban.home", System.getProperty("user.home"));
        ServiceManager ret = createServiceManager(startupConfigProperties());
        ret.startServices();
        
        return ret;
    }
}

@@ -4629,53 +4629,10 @@
 ger(
-Map%3CString, String%3E startupConfigProperties
 )%0A
+
     
@@ -5035,449 +5035,8 @@
     
-%0A%0A    protected static Map%3CString, String%3E startupConfigProperties() %7B%0A        Map%3CString, String%3E properties = new HashMap%3C%3E();%0A        properties.put(%22akserver.text.indexpath%22, %22/tmp/aktext%22);%0A        properties.put(FullTextIndexServiceImpl.UPDATE_INTERVAL, Long.toString(updateInterval));%0A        properties.put(FullTextIndexServiceImpl.POPULATE_DELAY_INTERVAL, Long.toString(populateDelayInterval));%0A        return properties;%0A    %7D%0A    
 %0A   
@@ -5162,24 +5162,24 @@
 er.home%22));%0A
+
         Serv
@@ -5220,33 +5220,8 @@
 ger(
-startupConfigProperties()
 );%0A 

use default settings in OptimizerTestBase