package com.darkyen;

import com.intellij.openapi.components.*;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.wm.StatusBar;
import com.intellij.openapi.wm.WindowManager;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 */
@State(
        name="DarkyenusTimeTracker",
        storages = {@Storage(value = StoragePathMacros.WORKSPACE_FILE)}
        )
public final class TimeTrackerComponent implements ProjectComponent, PersistentStateComponent<TimeTrackerState> {

    private static final Logger LOG = Logger.getLogger(TimeTrackerComponent.class.getName());

    private final Project project;
    private TimeTrackerWidget widget;

    private TimeTrackerState lastStateCache = null;

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

    @Override
    public void initComponent() {
    }

    @Override
    public void disposeComponent() {
    }

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

    private StatusBar getProjectStatusBar() {
        final WindowManager windowManager = WindowManager.getInstance();
        if (windowManager == null) return null;
        return windowManager.getStatusBar(project);
    }

    @Override
    public void projectOpened() {
        if (widget == null) {
            widget = new TimeTrackerWidget(project);
            if (lastStateCache != null) {
                widget.setState(lastStateCache);
                lastStateCache = null;
            }
            final StatusBar statusBar = getProjectStatusBar();
            if (statusBar != null) {
                statusBar.addWidget(widget);
            } else {
                LOG.log(Level.SEVERE, "Can't initialize time tracking widget, status bar returned by IDE is null");
            }
        }
    }

    @Override
    public void projectClosed() {
        if (widget != null) {
            final StatusBar statusBar = getProjectStatusBar();
            if (statusBar != null) {
                statusBar.removeWidget(widget.ID());
            } else {
                LOG.log(Level.WARNING, "Can't remove time tracking widget, status bar returned by IDE is null");
            }
            lastStateCache = widget.getState();
        }
    }

    @Nullable
    @Override
    public TimeTrackerState getState() {
        if (widget != null) {
            return widget.getState();
        } else if(lastStateCache != null) {
            return lastStateCache;
        } else {
            return new TimeTrackerState();
        }
    }

    @Override
    public void loadState(TimeTrackerState state) {
        if (widget != null) {
            widget.setState(state);
        } else {
            lastStateCache = state;
        }
    }
}

@@ -762,16 +762,55 @@
  widget;
+%0A    private StatusBar widgetStatusBar;
 %0A%0A    pr
@@ -1179,50 +1179,86 @@
     
-private StatusBar getProjectStatusBar() %7B%0A
+@Override%0A    public void projectOpened() %7B%0A        if (widget == null) %7B%0A    
     
@@ -1330,24 +1330,28 @@
 ();%0A        
+    
 if (windowMa
@@ -1369,29 +1369,126 @@
 ll) 
-return null;%0A
+%7B%0A                LOG.log(Level.SEVERE, %22Can't initialize time tracking widget, WindowManager is null%22);%0A     
         
 retu
@@ -1483,22 +1483,80 @@
         
+   
 return
+;%0A            %7D%0A            final StatusBar statusBar =
  windowM
@@ -1593,581 +1593,537 @@
     
-%7D%0A%0A
     
-@Override%0A    public void projectOpened() %7B%0A        if (widget == null) %7B%0A            widget = new TimeTrackerWidget(project);%0A            if (lastStateCache != null) %7B%0A                widget.setState(lastStateCache);%0A                lastStateCache = null;%0A            %7D%0A            final StatusBar statusBar = getProjectStatusBar();%0A            if (statusBar != null) %7B%0A                statusBar.addWidget(widget);%0A            %7D else %7B%0A                LOG.log(Level.SEVERE, %22Can't initialize time tracking 
+    if (statusBar == null) %7B%0A                LOG.log(Level.SEVERE, %22Can't initialize time tracking widget, status bar returned by IDE is null%22);%0A                return;%0A            %7D%0A%0A            final TimeTrackerWidget widget = new TimeTrackerWidget(project);%0A            if (lastStateCache != null) %7B%0A                widget.setState(lastStateCache);%0A                lastStateCache = null;%0A            %7D%0A            statusBar.addWidget(widget);%0A            this.widget = widget;%0A            this.
 widget
-, s
+S
 tatus
- bar returned by IDE is null%22);%0A            %7D
+Bar = statusBar;
 %0A   
@@ -2214,92 +2214,24 @@
 null
-) %7B%0A            final StatusBar statusBar = getProjectStatusBar();%0A            if (s
+ && this.widgetS
 tatu
@@ -2250,37 +2250,39 @@
 ) %7B%0A            
-    s
+widgetS
 tatusBar.removeW
@@ -2317,190 +2317,115 @@
     
-%7D else %7B%0A                LOG.log(Level.WARNING, %22Can't remove time tracking widget, status bar returned by IDE is null%22);%0A            %7D%0A            lastStateCache = widget.getState()
+lastStateCache = widget.getState();%0A            this.widget = null;%0A            this.widgetStatusBar = null
 ;%0A  

Fix TimeTrackerWidget not begin removed on project change