/*
 * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
 * or more contributor license agreements. Licensed under the "Elastic License
 * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
 * Public License v 1"; you may not use this file except in compliance with, at
 * your election, the "Elastic License 2.0", the "GNU Affero General Public
 * License v3.0 only", or the "Server Side Public License, v 1".
 */

package org.elasticsearch.index.mapper;

import org.apache.lucene.index.IndexableField;
import org.apache.lucene.index.LeafReaderContext;
import org.elasticsearch.script.GeoPointFieldScript;
import org.elasticsearch.search.lookup.SearchLookup;

import java.util.List;
import java.util.Map;
import java.util.function.Consumer;

public class GeoPointScriptMapperTests extends MapperScriptTestCase<GeoPointFieldScript.Factory> {

    private static GeoPointFieldScript.Factory factory(Consumer<GeoPointFieldScript.Emit> executor) {
        return new GeoPointFieldScript.Factory() {
            @Override
            public GeoPointFieldScript.LeafFactory newFactory(
                String fieldName,
                Map<String, Object> params,
                SearchLookup searchLookup,
                OnScriptError onScriptError
            ) {
                return new GeoPointFieldScript.LeafFactory() {
                    @Override
                    public GeoPointFieldScript newInstance(LeafReaderContext ctx) {
                        return new GeoPointFieldScript(fieldName, params, searchLookup, OnScriptError.FAIL, ctx) {
                            @Override
                            public void execute() {
                                executor.accept(new Emit(this));
                            }
                        };
                    }
                };
            }
        };
    }

    @Override
    protected String type() {
        return "geo_point";
    }

    @Override
    protected GeoPointFieldScript.Factory serializableScript() {
        return factory(s -> {});
    }

    @Override
    protected GeoPointFieldScript.Factory errorThrowingScript() {
        return factory(s -> { throw new UnsupportedOperationException("Oops"); });
    }

    @Override
    protected GeoPointFieldScript.Factory singleValueScript() {
        return factory(s -> s.emit(-1, 1));
    }

    @Override
    protected GeoPointFieldScript.Factory multipleValuesScript() {
        return factory(s -> {
            s.emit(-1, 1);
            s.emit(-2, 2);
        });
    }

    @Override
    protected void assertMultipleValues(List<IndexableField> fields) {
        assertEquals(2, fields.size());
        assertEquals("LatLonPointWithDocValues <field:-1.000000024214387,0.9999999403953552>", fields.get(0).toString());
        assertEquals("LatLonPointWithDocValues <field:-2.000000006519258,1.9999999646097422>", fields.get(1).toString());
    }

    @Override
    protected void assertDocValuesDisabled(List<IndexableField> fields) {
        assertEquals(1, fields.size());
        assertEquals("LatLonPoint <field:-1.000000024214387,0.9999999403953552>", fields.get(0).toString());
    }

    @Override
    protected void assertIndexDisabled(List<IndexableField> fields) {
        assertEquals(1, fields.size());
        assertEquals("LatLonDocValuesField <field:-1.000000024214387,0.9999999403953552>", fields.get(0).toString());
    }
}
