/*
 * SLD Editor - The Open Source Java SLD Editor
 *
 * Copyright (C) 2018, SCISYS UK Limited
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.sldeditor.test.unit.filter.v2.envvar;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

import com.sldeditor.common.DataSourcePropertiesInterface;
import com.sldeditor.common.vendoroption.VersionData;
import com.sldeditor.datasource.DataSourceInterface;
import com.sldeditor.datasource.DataSourceUpdatedInterface;
import com.sldeditor.datasource.SLDEditorFileInterface;
import com.sldeditor.datasource.attribute.DataSourceAttributeData;
import com.sldeditor.datasource.attribute.DataSourceAttributeListInterface;
import com.sldeditor.datasource.checks.CheckAttributeInterface;
import com.sldeditor.datasource.impl.CreateDataSourceInterface;
import com.sldeditor.datasource.impl.GeometryTypeEnum;
import com.sldeditor.filter.v2.envvar.EnvironmentVariablePanel;
import com.sldeditor.filter.v2.expression.ExpressionFilterInterface;
import com.sldeditor.filter.v2.expression.ExpressionNode;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import org.geotools.coverage.grid.io.AbstractGridCoverage2DReader;
import org.geotools.data.FeatureSource;
import org.geotools.factory.CommonFactoryFinder;
import org.geotools.styling.UserLayer;
import org.junit.jupiter.api.Test;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.feature.simple.SimpleFeatureType;
import org.opengis.feature.type.PropertyDescriptor;
import org.opengis.filter.FilterFactory;
import org.opengis.filter.expression.Expression;

/**
 * Unit test for EnvironmentVariablePanel class.
 *
 * <p>{@link com.sldeditor.filter.v2.envvar.EnvironmentVariablePanel}
 *
 * @author Robert Ward (SCISYS)
 */
class EnvironmentVariablePanelTest {

    class TestDataSourceInterface implements DataSourceInterface {

        @Override
        public void addListener(DataSourceUpdatedInterface listener) {}

        @Override
        public void removeListener(DataSourceUpdatedInterface listener) {}

        @Override
        public void connect(
                String typeName,
                SLDEditorFileInterface editorFile,
                List<CheckAttributeInterface> checkList) {}

        @Override
        public void reset() {}

        @Override
        public FeatureSource<SimpleFeatureType, SimpleFeature> getFeatureSource() {
            return null;
        }

        @Override
        public FeatureSource<SimpleFeatureType, SimpleFeature> getExampleFeatureSource() {
            return null;
        }

        @Override
        public List<String> getAttributes(Class<?> expectedDataType) {
            List<String> fieldList = new ArrayList<String>();

            fieldList.add("Field1");
            fieldList.add("Field2");
            fieldList.add("Field3");
            fieldList.add("Field4");
            fieldList.add("Field5");
            return fieldList;
        }

        @Override
        public List<String> getAllAttributes(boolean includeGeometry) {
            return null;
        }

        @Override
        public GeometryTypeEnum getGeometryType() {
            return null;
        }

        @Override
        public String getGeometryFieldName() {
            return null;
        }

        @Override
        public void readAttributes(DataSourceAttributeListInterface attributeData) {}

        @Override
        public DataSourcePropertiesInterface getDataConnectorProperties() {
            return null;
        }

        @Override
        public List<String> getAvailableDataStoreList() {
            return null;
        }

        @Override
        public void updateFields(DataSourceAttributeListInterface attributeData) {}

        @Override
        public void addField(DataSourceAttributeData dataSourceField) {}

        @Override
        public void setDataSourceCreation(
                CreateDataSourceInterface internalDataSource,
                CreateDataSourceInterface externalDataSource,
                CreateDataSourceInterface inlineDataSource) {}

        @Override
        public Collection<PropertyDescriptor> getPropertyDescriptorList() {
            return null;
        }

        @Override
        public AbstractGridCoverage2DReader getGridCoverageReader() {
            return null;
        }

        @Override
        public Map<UserLayer, FeatureSource<SimpleFeatureType, SimpleFeature>>
                getUserLayerFeatureSource() {
            return null;
        }

        @Override
        public void updateUserLayers() {}

        @Override
        public void updateFieldType(String fieldName, Class<?> dataType) {}
    };

    class TestEnvironmentVariablePanel extends EnvironmentVariablePanel {
        /** The Constant serialVersionUID. */
        private static final long serialVersionUID = 1L;

        public TestEnvironmentVariablePanel(ExpressionFilterInterface parent) {
            super(parent);
        }

        /* (non-Javadoc)
         * @see com.sldeditor.filter.v2.envvar.EnvironmentVariablePanel#applyButton()
         */
        @Override
        protected void applyButton() {
            super.applyButton();
        }

        /* (non-Javadoc)
         * @see com.sldeditor.filter.v2.envvar.EnvironmentVariablePanel#revertButton()
         */
        @Override
        protected void revertButton() {
            super.revertButton();
        }

        public void setAttribute(Expression expression) {
            dataSourceAttributePanel.setAttribute(expression);
        }
    }

    class TestExpressionFilterInterface implements ExpressionFilterInterface {
        public boolean dataAppliedFlag = false;

        @Override
        public void dataApplied() {
            dataAppliedFlag = true;
        }

        @Override
        public List<VersionData> getVendorOptionList() {
            return null;
        }
    }

    /**
     * Test method for {@link
     * com.sldeditor.filter.v2.envvar.EnvironmentVariablePanel#EnvironmentVariablePanel(com.sldeditor.filter.v2.expression.ExpressionFilterInterface)}.
     */
    @Test
    void testEnvironmentVariablePanel() {
        TestEnvironmentVariablePanel testObj = new TestEnvironmentVariablePanel(null);

        // Nothing selected
        testObj.revertButton();
        testObj.applyButton();
        testObj.dataSourceLoaded(null);

        TestExpressionFilterInterface receiver = new TestExpressionFilterInterface();
        testObj = new TestEnvironmentVariablePanel(receiver);
        TestDataSourceInterface dataSource = new TestDataSourceInterface();
        testObj.dataSourceLoaded(dataSource);

        assertFalse(receiver.dataAppliedFlag);
        FilterFactory ff = CommonFactoryFinder.getFilterFactory();
        ExpressionNode node = new ExpressionNode();

        Expression property = ff.property("Field2");
        node.setExpression(property);

        testObj.setAttribute(property);
        testObj.applyButton();
        assertTrue(receiver.dataAppliedFlag);
        receiver.dataAppliedFlag = false;
        testObj.setSelectedNode(node);
        testObj.applyButton();
        assertTrue(receiver.dataAppliedFlag);
    }
}
