/*
 * Autopsy Forensic Browser
 *
 * Copyright 2014 Basis Technology Corp.
 * Contact: carrier <at> sleuthkit <dot> org
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.sleuthkit.autopsy.timeline.explorernodes;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javafx.beans.Observable;
import javax.swing.Action;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.openide.nodes.Children;
import org.openide.nodes.PropertySupport;
import org.openide.nodes.Sheet;
import org.openide.util.Exceptions;
import org.openide.util.lookup.Lookups;
import org.sleuthkit.autopsy.timeline.TimeLineController;
import org.sleuthkit.autopsy.timeline.events.TimeLineEvent;
import org.sleuthkit.autopsy.datamodel.DataModelActionsFactory;
import org.sleuthkit.autopsy.datamodel.DisplayableItemNode;
import org.sleuthkit.autopsy.datamodel.DisplayableItemNodeVisitor;
import org.sleuthkit.autopsy.datamodel.NodeProperty;
import org.sleuthkit.datamodel.AbstractFile;
import org.sleuthkit.datamodel.BlackboardArtifact;
import org.sleuthkit.datamodel.Content;

/** * Node for {@link TimeLineEvent}s. */
class EventNode extends DisplayableItemNode {

    private final TimeLineEvent e;

    EventNode(TimeLineEvent eventById, AbstractFile file, BlackboardArtifact artifact) {
        super(Children.LEAF, Lookups.fixed(eventById, file, artifact));
        this.e = eventById;
        this.setIconBaseWithExtension("org/sleuthkit/autopsy/timeline/images/" + e.getType().getIconBase());
    }

    EventNode(TimeLineEvent eventById, AbstractFile file) {
        super(Children.LEAF, Lookups.fixed(eventById, file));
        this.e = eventById;
        this.setIconBaseWithExtension("org/sleuthkit/autopsy/timeline/images/" + e.getType().getIconBase());
    }

    @Override
    protected Sheet createSheet() {
        Sheet s = super.createSheet();
        Sheet.Set properties = s.get(Sheet.PROPERTIES);
        if (properties == null) {
            properties = Sheet.createPropertiesSet();
            s.put(properties);
        }

        final TimeProperty timePropery = new TimeProperty("time", "Date/Time", "time ", getDateTimeString());

        TimeLineController.getTimeZone().addListener((Observable observable) -> {
            try {
                timePropery.setValue(getDateTimeString());
            } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
                Exceptions.printStackTrace(ex);
            }
        });

        //  String name, String displayName, String desc, T value
        properties.put(new NodeProperty<>("icon", "Icon", "icon", true)); //gets overriden with icon

        properties.put(timePropery);
        properties.put(new NodeProperty<>("eventID", "Event ID", "event id", e.getEventID()));
        properties.put(new NodeProperty<>("fileID", "File ID", "File id", e.getFileID()));
        properties.put(new NodeProperty<>("artifactID", "Result ID", "result id", e.getArtifactID() == 0 ? "" : e.getArtifactID()));
        properties.put(new NodeProperty<>("eventBaseType", "Base Type", "base type", e.getType().getSuperType().getDisplayName()));
        properties.put(new NodeProperty<>("eventSubType", "Sub Type", "sub type", e.getType().getDisplayName()));
        properties.put(new NodeProperty<>("Known", "Known", "known", e.getKnown().toString()));
        properties.put(new NodeProperty<>("description", "Description", "description", e.getFullDescription()));
        return s;
    }

    private String getDateTimeString() {
        return new DateTime(e.getTime() * 1000, DateTimeZone.UTC).toString(TimeLineController.getZonedFormatter());
    }

    @Override
    public Action[] getActions(boolean context) {
        Action[] superActions = super.getActions(context);
        List<Action> actionsList = new ArrayList<>();
        actionsList.addAll(Arrays.asList(superActions));

        final Content content = getLookup().lookup(Content.class);
        final BlackboardArtifact artifact = getLookup().lookup(BlackboardArtifact.class);

        final List<Action> factoryActions = DataModelActionsFactory.getActions(content, artifact != null);

        actionsList.addAll(factoryActions);
        return actionsList.toArray(new Action[0]);
    }

    @Override
    public boolean isLeafTypeNode() {
        return true;
    }

    @Override
    public <T> T accept(DisplayableItemNodeVisitor<T> dinv) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    class TimeProperty extends PropertySupport.ReadWrite<String> {

        private String value;

        @Override
        public boolean canWrite() {
            return false;
        }

        public TimeProperty(String name, String displayName, String shortDescription, String value) {
            super(name, String.class, displayName, shortDescription);
            setValue("suppressCustomEditor", Boolean.TRUE); // remove the "..." (editing) button NON-NLS
            this.value = value;
        }

        @Override
        public String getValue() throws IllegalAccessException, InvocationTargetException {
            return value;
        }

        @Override
        public void setValue(String t) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {
            String oldValue = getValue();
            value = t;
            firePropertyChange("time", oldValue, t);
        }
    }

}

@@ -1172,126 +1172,8 @@
 ps;%0A
-import org.sleuthkit.autopsy.timeline.TimeLineController;%0Aimport org.sleuthkit.autopsy.timeline.events.TimeLineEvent;%0A
 impo
@@ -1232,16 +1232,16 @@
 actory;%0A
+
 import o
@@ -1412,16 +1412,134 @@
 operty;%0A
+import org.sleuthkit.autopsy.timeline.TimeLineController;%0Aimport org.sleuthkit.autopsy.timeline.events.TimeLineEvent;%0A
 import o
@@ -1672,16 +1672,25 @@
 ;%0A%0A/** *
+ Explorer
  Node fo
@@ -3101,74 +3101,8 @@
 );%0A%0A
-        //  String name, String displayName, String desc, T value%0A
     
@@ -3198,17 +3198,16 @@
 th icon%0A
-%0A
         
@@ -3282,278 +3282,72 @@
 %3C%3E(%22
-eventID%22, %22Event ID%22, %22event id%22, e.getEventID()));%0A        properties.put(new NodeProperty%3C%3E(%22fileID%22, %22File ID%22, %22File id%22, e.getFileID()));%0A        properties.put(new NodeProperty%3C%3E(%22artifactID
+description%22, %22Description
 %22, %22
-Result ID%22, %22result id%22, e.getArtifactID() == 0 ? %22%22 : e.getArtifactID
+description%22, e.getFullDescription
 ()))
@@ -3694,121 +3694,9 @@
 ));%0A
-        properties.put(new NodeProperty%3C%3E(%22description%22, %22Description%22, %22description%22, e.getFullDescription()));
 %0A
+
     
@@ -5709,17 +5709,16 @@
       %7D%0A
+
     %7D%0A
-%0A
 %7D%0A

remove event, file, and artifact id columns from result viewer, move description after time