/*
 * RestoreFactoryDefaultsView.java
 * 
 * Copyright (c) 2003-2004 Digi International
 * This program and the information contained in it is confidential and
 * proprietary to Digi International and may not be used, copied, or re-
 * produced without the prior written permission of Digi International.
 * 
 */

package com.digi.config.ui;

import com.digi.config.core.*;
import com.digi.config.util.*;

import javax.swing.*;
import javax.swing.event.*;
import javax.swing.tree.*;
import javax.swing.border.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.io.*;
import java.net.*;




/** 
 * This class is a view panel that allows the user to restore factory
 * default settings on the device.
 * 
 * This view can be embedded in other panels as desired.
 */


public class RestoreFactoryDefaultsView extends ConfigViewImpl {
    /** Panel that holds the view content */
    GridContentPanel content;
    JCheckBox revertOption_UIC;

    /**
     * Basic constructor. Sets up the tree view and the edit panel.
     */
    public RestoreFactoryDefaultsView() throws Exception {
        // Create the panel that will hold the view controls
        content = new GridContentPanel(1);

        //
        // Create the fields and put them on the view
        //

        content.addTextLine("RestoreFactoryDefaultsHeading");
        revertOption_UIC = content.addJCheckBox("RestoreFactoryDefaultsRevertOption", null);
        content.addAction(null, new ConfigAction("RestoreNowBtn", this, "doRestoreFactoryDefaultsAction"), "RestoreNowBtnDesc");
        content.addVGlue();
    }


    public void doRestoreFactoryDefaultsAction() {
        boolean success = restoreFactoryDefaults();
        // errors or not, update the cache with any new values.
        doRefreshAction();
        if (success) {
            int doReboot = JOptionPane.showConfirmDialog(content,
                                                         ConfigResource.getUiRbString("RestoreFactoryDefaultsRebootPrompt"),
                                                         ConfigResource.getUiRbString("RestoreFactoryDefaultsViewTitle"),
                                                         JOptionPane.YES_NO_OPTION);
            if (doReboot == JOptionPane.YES_OPTION) {
                reboot();
            }
        }
    }

    boolean restoreFactoryDefaults() {
        boolean success = false;

        Busy.begin(ConfigResource.getUiRbString("RestoreFactoryDefaultsStatus"));
        try {
            device.restoreFactoryDefaults(revertOption_UIC.isSelected());
            SystemLog.log("Op1Succ",new Serializable[]{ConfigResource.getUiRbString("RestoreFactoryDefaultsMenu")});
            success = true;
        } catch (DeviceCommandException dce) {
            SystemLog.log("Op1Fail",new Serializable[]{ConfigResource.getUiRbString("RestoreFactoryDefaultsMenu")},dce);
        } catch (DeviceValidationException dve) {
            SystemLog.debug("Invalid fields detected while restoring factory default settings on device", dve);
            KvpNode errorCluster = dve.getCluster();
            Collection errors = errorCluster.getErrors();
            StringBuffer buff = new StringBuffer();
            for (Iterator i=errors.iterator(); i.hasNext();) {
                RciCommandError error = (RciCommandError)i.next();
                buff.append(error.toString()+"\n");
            }
            SystemLog.log("RestoreFactoryDefaultsError", new Serializable[] {buff.toString()});
            // finish this.... POPUP a dialog showing the errors as well??
        }
        Busy.end();
        return success;
    }

    void reboot() {
        //TODO: ping device to try to determine when reboot is complete.
        Busy.begin(ConfigResource.getUiRbString("RebootStatus"));
        try {
            device.reboot();
            SystemLog.log("Op1Succ",new Serializable[]{ConfigResource.getUiRbString("RebootMenu")});
        } catch (DeviceCommandException dce) {
            SystemLog.log("Op1Fail",new Serializable[]{ConfigResource.getUiRbString("RebootMenu")},dce);
        }
        Busy.end();
    }

    /**
     * Returns a one word name identifying this view. 
     */
    public String getName() {
        return "RestoreFactoryDefaultsView";
    }

    /** 
     * Return the Component that displays the primary content for this view
     */
    public Component getViewContent() {
        return content;
    }

    /** 
     * Returns the Component that displays the buttons below the view.
     * If this view has no buttons to display this method returns null.
     */
    public Component getViewButtons() {
        // We don't want buttons on this view so just return null
        return null;
    }


    /**
     * Informs view what device to work with. View should flush any
     * cached device data and mark current view content as invalid
     * so that it is refreshed appropriatly.
     */
    public void setDevice(Device device) {
        super.setDevice(device);
    }


    /**
     * This method is called to instruct the view that it is about to be made
     * active. When a view is active it needs to make sure its content is correct.
     * An example of why a panel may be inactive is if it were on a tabbed pane 
     * and was not currently visible or if the user selected some other view.
     */
    public void activate() {
        SystemLog.debug("Activating RestoreFactoryDefaultsView");
    }

    /**
     * This method is called to instruct the view that it is about to be made
     * inactive. When a view is inactive, it does not need to worry
     * about maintaining its content in a correct state. An example of why a panel
     * may be inactive is if it were on a tabbed pane and was not currently visible
     * or if the user selected some other view.
     */
    public void deactivate() {
        SystemLog.debug("Deactivating RestoreFactoryDefaultsView");
    }

    /**
     * Indicates if any changes have been made by the user that have not yet been
     * saved to the device(ie committed).
     */
    public boolean isChanged() {
        return false;
    }

    /**
     * This method instructs the view to place any changes the user has made within 
     * the view into the provided cluster. Once the changes have been saved to
     * to the device, a subsequent call to commitChanges() will be made.
     */
    public void getChanges(KvpNode settingCluster, KvpNode stateCluster) {
    }

    /**
     * Instructs the view to consider any user changes within the view as 
     * saved to the device (ie committed).
     */
    public void commitChanges() {
    }

    /** 
     * Instructs the view to discard any changes the user has made within the
     * view and revert those fields to the original state. The view is also 
     * free to refresh all its fields to the present cached state of the device at 
     * this time.
     */
    public void cancelChanges() {
    }


}

