/*******************************************************************************
 * Copyright (C) 2022, 1C-Soft LLC and others.
 *
 * This program and the accompanying materials are made
 * available under the terms of the Eclipse Public License 2.0
 * which is available at https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     1C-Soft LLC - initial API and implementation
 *******************************************************************************/
package com.e1c.v8codestyle.form.check.itests;

import static com._1c.g5.v8.dt.metadata.mdclass.MdClassPackage.Literals.CONFIGURATION;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

import org.eclipse.core.runtime.IProgressMonitor;
import org.junit.Test;

import com._1c.g5.v8.bm.core.IBmObject;
import com._1c.g5.v8.bm.core.IBmTransaction;
import com._1c.g5.v8.bm.integration.AbstractBmTask;
import com._1c.g5.v8.bm.integration.IBmModel;
import com._1c.g5.v8.dt.core.platform.IDtProject;
import com._1c.g5.v8.dt.form.model.Form;
import com._1c.g5.v8.dt.form.model.FormItem;
import com._1c.g5.v8.dt.form.model.Table;
import com._1c.g5.v8.dt.metadata.mdclass.Configuration;
import com._1c.g5.v8.dt.metadata.mdclass.ScriptVariant;
import com._1c.g5.v8.dt.validation.marker.Marker;
import com.e1c.g5.v8.dt.testing.check.CheckTestBase;
import com.e1c.v8codestyle.form.check.FormListFieldRefNotAddedCheck;

/**
 * Tests for {@link FormListFieldRefNotAddedCheck} check.
 *
 * @author Olga Bozhko
 */
public class FormListFieldRefNotAddedCheckTest
    extends CheckTestBase
{
    private static final String CHECK_ID = "form-list-field-ref-not-added";
    private static final String PROJECT_NAME = "FormListFieldRefNotAdded";
    private static final String FQN_FORM = "Catalog.TestCatalog.Form.TestListForm.Form";

    /**
     * Test the Reference field is not added to dynamic list (En Script variant).
     *
     * @throws Exception the exception
     */
    @Test
    public void testRefNotAddedToDynamicList() throws Exception
    {
        IDtProject dtProject = openProjectAndWaitForValidationFinish(PROJECT_NAME);
        assertNotNull(dtProject);

        IBmModel model = bmModelManager.getModel(dtProject);
        model.execute(new AbstractBmTask<Void>("change mode")
        {
            @Override
            public Void execute(IBmTransaction transaction, IProgressMonitor monitor)
            {
                Form form = (Form)transaction.getTopObjectByFqn(FQN_FORM);
                FormItem item = form.getItems().get(1);
                assertTrue(item instanceof Table);
                Table table = (Table)item;
                table.getItems().remove(0);
                return null;
            }
        });
        waitForDD(dtProject);

        IBmObject object = getTopObjectByFqn(FQN_FORM, dtProject);
        assertTrue(object instanceof Form);
        Marker marker = getFirstNestedMarker(CHECK_ID, object.bmGetId(), dtProject);
        assertNotNull(marker);
    }

    /**
     * Test the Reference field is added to dynamic list (En Script variant).
     *
     * @throws Exception the exception
     */
    @Test
    public void testRefAddedToDynamicList() throws Exception
    {
        IDtProject dtProject = openProjectAndWaitForValidationFinish(PROJECT_NAME);
        assertNotNull(dtProject);

        IBmObject object = getTopObjectByFqn(FQN_FORM, dtProject);
        assertTrue(object instanceof Form);

        Marker marker = getFirstNestedMarker(CHECK_ID, object.bmGetId(), dtProject);
        assertNull(marker);
    }

    /**
     * Test the Reference field is not added to dynamic list (Ru script variant).
     *
     * @throws Exception the exception
     */
    @Test
    public void testRefNotAddedToDynamicListRu() throws Exception
    {
        IDtProject dtProject = openProjectAndWaitForValidationFinish(PROJECT_NAME);
        assertNotNull(dtProject);

        IBmModel changeLanguage = bmModelManager.getModel(dtProject);
        changeLanguage.execute(new AbstractBmTask<Void>("change mode")
        {
            @Override
            public Void execute(IBmTransaction transaction, IProgressMonitor monitor)
            {
                IBmObject object = transaction.getTopObjectByFqn(CONFIGURATION.getName());
                assertTrue(object instanceof Configuration);
                Configuration config = (Configuration)object;
                config.setScriptVariant(ScriptVariant.RUSSIAN);
                assertTrue(config.getScriptVariant() == ScriptVariant.RUSSIAN);
                return null;
            }
        });
        waitForDD(dtProject);

        IBmModel removeItem = bmModelManager.getModel(dtProject);
        removeItem.execute(new AbstractBmTask<Void>("change mode")
        {
            @Override
            public Void execute(IBmTransaction transaction, IProgressMonitor monitor)
            {
                Form form = (Form)transaction.getTopObjectByFqn(FQN_FORM);
                FormItem item = form.getItems().get(1);
                assertTrue(item instanceof Table);
                Table table = (Table)item;
                table.getItems().remove(0);
                return null;
            }
        });
        waitForDD(dtProject);

        IBmObject object = getTopObjectByFqn(FQN_FORM, dtProject);
        assertTrue(object instanceof Form);

        Marker marker = getFirstNestedMarker(CHECK_ID, object.bmGetId(), dtProject);
        assertNotNull(marker);
    }

}
