# GroupDefinitionView.java

    /*
    ** Module   : GroupDefinitionView.java
    ** Abstract : GroupDefinitionView represents a view to create new group and to change a selected
    **            group account.
    **
    ** Copyright (c) 2017, Golden Code Development Corporation.
    **
    ** -#- -I- --Date-- ----------------Description----------------------
    ** 001 SBI 20170601 Created initial version.
    */
    /*
    ** This program is free software: you can redistribute it and/or modify
    ** it under the terms of the GNU Affero General Public License as
    ** published by the Free Software Foundation, either version 3 of the
    ** License, or (at your option) any later version.
    **
    ** This program is distributed in the hope that it will be useful,
    ** but WITHOUT ANY WARRANTY; without even the implied warranty of
    ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    ** GNU Affero General Public License for more details.
    **
    ** You may find a copy of the GNU Affero GPL version 3 at the following
    ** location: https://www.gnu.org/licenses/agpl-3.0.en.html
    **
    ** Additional terms under GNU Affero GPL version 3 section 7:
    **
    **   Under Section 7 of the GNU Affero GPL version 3, the following additional
    **   terms apply to the works covered under the License.  These additional terms
    **   are non-permissive additional terms allowed under Section 7 of the GNU
    **   Affero GPL version 3 and may not be removed by you.
    **
    **   0. Attribution Requirement.
    **
    **     You must preserve all legal notices or author attributions in the covered
    **     work or Appropriate Legal Notices displayed by works containing the covered
    **     work.  You may not remove from the covered work any author or developer
    **     credit already included within the covered work.
    **
    **   1. No License To Use Trademarks.
    **
    **     This license does not grant any license or rights to use the trademarks
    **     Golden Code, FWD, any Golden Code or FWD logo, or any other trademarks
    **     of Golden Code Development Corporation. You are not authorized to use the
    **     name Golden Code, FWD, or the names of any author or contributor, for
    **     publicity purposes without written authorization.
    **
    **   2. No Misrepresentation of Affiliation.
    **
    **     You may not represent yourself as Golden Code Development Corporation or FWD.
    **
    **     You may not represent yourself for publicity purposes as associated with
    **     Golden Code Development Corporation, FWD, or any author or contributor to
    **     the covered work, without written authorization.
    **
    **   3. No Misrepresentation of Source or Origin.
    **
    **     You may not represent the covered work as solely your work.  All modified
    **     versions of the covered work must be marked in a reasonable way to make it
    **     clear that the modified work is not originating from Golden Code Development
    **     Corporation or FWD.  All modified versions must contain the notices of
    **     attribution required in this license.
    */

    package com.goldencode.p2j.admin.client.application.home.accounts.groups;

    import java.util.logging.Level;

    import org.gwtbootstrap3.client.ui.Button;
    import org.gwtbootstrap3.client.ui.ListBox;
    import org.gwtbootstrap3.client.ui.Modal;
    import org.gwtbootstrap3.client.ui.TextBox;

    import com.goldencode.p2j.admin.GroupDef;
    import com.goldencode.p2j.admin.TaggedName;
    import com.goldencode.p2j.admin.client.application.home.accounts.AuthModes;
    import com.goldencode.p2j.admin.client.application.home.accounts.UseCaseMode;
    import com.goldencode.p2j.admin.client.widget.dialog.ModalViewWithUiHandlers;
    import com.google.gwt.event.dom.client.ChangeEvent;
    import com.google.gwt.event.dom.client.ChangeHandler;
    import com.google.gwt.event.dom.client.ClickEvent;
    import com.google.gwt.uibinder.client.UiBinder;
    import com.google.gwt.uibinder.client.UiField;
    import com.google.gwt.uibinder.client.UiHandler;
    import com.google.gwt.user.client.ui.Widget;
    import com.google.inject.Inject;

    /**
     * GroupDefinitionView represents a view to create new group and to change a selected group
     * account.
     */
    public class GroupDefinitionView
    extends ModalViewWithUiHandlers<GroupsUIHandlers>
    {
       /**
        * GWT UI creator
        */
       interface Binder
       extends UiBinder<Widget, GroupDefinitionView>
       {}

       /** The underlined modal dialog */
       @UiField
       Modal modal;

       /** The group account name */
       @UiField
       TextBox name;

       /** The group account description */
       @UiField
       TextBox desc;

       /** The list box of all available authentication modes */
       @UiField
       ListBox mode;

       /** The list box of the custom authentication plugins */
       @UiField
       ListBox plugin;

       /** The save button */
       @UiField
       Button saveAction;

       /** The cancel button */
       @UiField
       Button cancelAction;

       /** The adding group or editing group use case */
       private UseCaseMode usecase;

       /** Plugin model */
       private TaggedName[] pluginValues;

       /** The group definition for adding or editing*/
       private final GroupDef groupDef = new GroupDef();

       /**
        * Constructs this dialog used by by MPV gwtplatform of ArcBees Inc.
        *
        * @param    binder
        *           The injected GWT UI Binder
        */
       @Inject
       public GroupDefinitionView(GroupDefinitionView.Binder binder)
       {
          initWidget(binder.createAndBindUi(this));
          for(AuthModes value : AuthModes.values())
          {
             mode.addItem(value.toString(), value.name());
          }
          plugin.setEnabled(AuthModes.CUST_AUTH.name().equals(mode.getSelectedValue()));

          mode.addChangeHandler(new ChangeHandler()
          {

             @Override
             public void onChange(ChangeEvent event)
             {
                plugin.setEnabled(AuthModes.CUST_AUTH.name().equals(mode.getSelectedValue()));
             }
          });
          addHideHandler(()->
          {
             switch(usecase)
             {
                case ADD_GROUP:
                   getUiHandlers().cancelAddGroupDialog();
                   break;
                case EDIT_GROUP:
                   getUiHandlers().cancelEditGroupDialog();
                   break;
                default:
             }
          });
          addShowHandler(()->
          {
             switch(usecase)
             {
                case ADD_GROUP:
                   getUiHandlers().setCurrentState(GroupsPresenter.GroupsViewStates.ADD_GROUP_ACCOUNT);
                   break;
                case EDIT_GROUP:
                   getUiHandlers().setCurrentState(GroupsPresenter.GroupsViewStates.EDIT_GROUP_ACCOUNT);
                   break;
                default:
             }
          });

       }

       /**
        * The save button click handler.
        *
        * @param    e
        *           Click event
        */
       @UiHandler("saveAction")
       void onSaveAction(ClickEvent e)
       {
          logger.log(Level.INFO, "onSaveAction");

          this.groupDef.subjectId = name.getText();

          this.groupDef.description = desc.getText();

          this.groupDef.mode = mode.getSelectedIndex();

          if (plugin.isEnabled())
          {
             this.groupDef.authPlugin = plugin.getSelectedValue();
          }

          if (usecase == UseCaseMode.ADD_GROUP)
          {
             getUiHandlers().addGroup(this.groupDef);
          }
          else if (usecase == UseCaseMode.EDIT_GROUP)
          {
             getUiHandlers().updateGroup(this.groupDef);
          }

          usecase = UseCaseMode.APPROVE_ACTION;
       }

       /**
        * The cancel button click handler.
        *
        * @param    e
        *           Click event
        */
       @UiHandler("cancelAction")
       void onCancelAction(ClickEvent e)
       {
          logger.log(Level.INFO, "onCancelAction");
          if (usecase == UseCaseMode.ADD_GROUP)
          {
             getUiHandlers().cancelAddGroupDialog();
          }
          else if (usecase == UseCaseMode.EDIT_GROUP)
          {
             getUiHandlers().cancelEditGroupDialog();
          }

          usecase = UseCaseMode.CANCEL_ACTION;
       }

       /**
        * Sets the adding or editing group use case.
        *
        * @param    usecase
        *           The dialog use case
        * @param    groupDef
        *           The group definition
        */
       public void setUseCaseMode(UseCaseMode usecase, GroupDef groupDef)
       {
          logger.log(Level.INFO, "setUseCaseMode " + usecase.name());

          this.usecase = usecase;

          this.groupDef.subjectId = groupDef.subjectId;

          this.groupDef.description = groupDef.description;

          this.groupDef.authPlugin = groupDef.authPlugin;

          this.groupDef.mode = groupDef.mode;

          if (usecase == UseCaseMode.ADD_GROUP)
          {
             name.setEnabled(true);
             name.setText("");
             desc.setText("");
             mode.setItemSelected(0, true);
          }
          else if (usecase == UseCaseMode.EDIT_GROUP)
          {
             name.setEnabled(false);
             name.setText(groupDef.subjectId);
             desc.setText(groupDef.description);
             mode.setItemSelected(groupDef.mode, true);
             if (groupDef.authPlugin != null)
             {
                int i = 0;
                for (TaggedName value : this.pluginValues)
                {
                   if (groupDef.authPlugin == value.getName())
                   {
                      break;
                   }
                   i++;
                }
                plugin.setItemSelected(i, true);
             }
          }
       }

       /**
        * Sets the data for the custom authentication list box.
        *
        * @param    pluginValues
        *           The array of custom authentication plugins
        */
       public void setCustomAuthPlugins(TaggedName[] pluginValues)
       {
          plugin.clear();
          if (pluginValues == null)
          {
             return;
          }
          // ListBox doesn't have model
          this.pluginValues = pluginValues;
          for (TaggedName value : this.pluginValues)
          {
             plugin.addItem(value.getTag(), value.getName());
          }
       }

       /**
        * Provides an access to the target modal dialog.
        *
        * @return   The underlined modal dialog
        */
       @Override
       protected Modal getModal()
       {
          return modal;
       }
    }