package de.espend.idea.oxid;

import com.intellij.openapi.components.ProjectComponent;
import com.intellij.openapi.progress.ProgressIndicator;
import com.intellij.openapi.progress.Task;
import com.intellij.openapi.project.DumbService;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiElement;
import de.espend.idea.oxid.utils.OxidUtil;
import fr.adrienbrault.idea.symfony2plugin.util.PhpElementsUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Timer;
import java.util.TimerTask;

/**
 * @author Daniel Espendiller <daniel@espendiller.net>
 */
public class OxidProjectComponent implements ProjectComponent {

    public static final int DUMPER_PERIODE = 600 * 1000;

    private final Project project;
    private Timer timer;

    public OxidProjectComponent(Project project) {
        this.project = project;
    }

    @Override
    public void projectOpened() {

        DumbService.getInstance(this.project).smartInvokeLater(() -> {
            if(PhpElementsUtil.getClassInterface(project, "\\oxArticle") == null) {
                return;
            }

            new Task.Backgroundable(project, "OXID extends dumper", true) {
                @Override
                public void run(@NotNull ProgressIndicator indicator) {
                    if(DumbService.getInstance(project).isDumb()) {
                        return;
                    }

                    OxidUtil.buildClassMetadataFile(project);
                }
            }.queue();


            timer = new Timer();

            timer.schedule(new TimerTask() {
                public void run() {

                    if(DumbService.getInstance(project).isDumb()) {
                        return;
                    }

                    OxidUtil.buildClassMetadataFile(project);
                }
            }, DUMPER_PERIODE, DUMPER_PERIODE);

        });


    }

    @Override
    public void projectClosed() {
        if(this.timer != null) {
            this.timer.cancel();
            timer.purge();
            timer = null;
        }
    }

    @Override
    public void initComponent() {

    }

    @Override
    public void disposeComponent() {

    }

    @NotNull
    @Override
    public String getComponentName() {
        return "OXID Plugin";
    }

    public static boolean isValidForProject(@Nullable PsiElement psiElement) {
        if(psiElement == null) return false;

        /*
        if(VfsUtil.findRelativeFile(psiElement.getProject().getBaseDir(), "engine", "Shopware", "Kernel.php") != null) {
            return true;
        }

        if(PhpElementsUtil.getClassInterface(psiElement.getProject(), "\\Enlight_Controller_Action") != null) {
            return true;
        }
        */

        return true;
    }

}

@@ -2443,33 +2443,78 @@
 ll) 
-return false;%0A%0A        /*
+%7B%0A            return false;%0A        %7D%0A%0A        // @TODO: plugin switch
 %0A   
@@ -2518,19 +2518,19 @@
         
-if(
+// 
 VfsUtil.
@@ -2619,35 +2619,32 @@
 el.php%22) != null
-) %7B
 %0A            ret
@@ -2640,47 +2640,11 @@
     
-    return true;%0A        %7D%0A%0A        if(
+// 
 PhpE
@@ -2740,57 +2740,8 @@
 null
-) %7B%0A            return true;%0A        %7D%0A        */
 %0A%0A  

make plugin enable switch check more explicit shopware class will not help here