package org.ovirt.engine.ui.uicommonweb.models.userportal;

import org.ovirt.engine.core.common.action.VdcActionType;
import org.ovirt.engine.core.common.action.VdcReturnValueBase;
import org.ovirt.engine.core.common.action.VmPoolUserParameters;
import org.ovirt.engine.core.common.businessentities.VM;
import org.ovirt.engine.core.common.businessentities.VMStatus;
import org.ovirt.engine.core.common.businessentities.vm_pools;
import org.ovirt.engine.core.compat.Event;
import org.ovirt.engine.core.compat.EventArgs;
import org.ovirt.engine.core.compat.Guid;
import org.ovirt.engine.core.compat.PropertyChangedEventArgs;
import org.ovirt.engine.ui.frontend.AsyncQuery;
import org.ovirt.engine.ui.frontend.Frontend;
import org.ovirt.engine.ui.frontend.INewAsyncCallback;
import org.ovirt.engine.ui.uicommonweb.UICommand;
import org.ovirt.engine.ui.uicommonweb.dataprovider.AsyncDataProvider;
import org.ovirt.engine.ui.uicompat.FrontendActionAsyncResult;
import org.ovirt.engine.ui.uicompat.IFrontendActionAsyncCallback;

@SuppressWarnings("unused")
public class PoolItemBehavior extends ItemBehavior
{
    public PoolItemBehavior(UserPortalItemModel item)
    {
        super(item);
    }

    @Override
    public void OnEntityChanged()
    {
        UpdateProperties();
        UpdateActionAvailability();
    }

    @Override
    public void EntityPropertyChanged(PropertyChangedEventArgs e)
    {
    }

    @Override
    public void ExecuteCommand(UICommand command)
    {
        if (command == getItem().getTakeVmCommand())
        {
            TakeVm();
        }
    }

    @Override
    public void eventRaised(Event ev, Object sender, EventArgs args)
    {
        // Do nothing. There are no events to handle for pools.
    }

    private void TakeVm()
    {
        vm_pools entity = (vm_pools) getItem().getEntity();

        Frontend.RunAction(VdcActionType.AttachUserToVmFromPoolAndRun, new VmPoolUserParameters(entity.getvm_pool_id(),
                Frontend.getLoggedInUser(),
                false),
                new IFrontendActionAsyncCallback() {
                    @Override
                    public void Executed(FrontendActionAsyncResult result) {

                        PoolItemBehavior behavior = (PoolItemBehavior) result.getState();
                        VdcReturnValueBase returnValueBase = result.getReturnValue();
                        Guid Guid = (Guid) returnValueBase.getActionReturnValue();
                        behavior.PostTakeVm(Guid);

                    }
                }, this);
    }

    public void PostTakeVm(Guid vmId)
    {
        AsyncDataProvider.GetVmById(new AsyncQuery(this,
                new INewAsyncCallback() {
                    @Override
                    public void OnSuccess(Object target, Object returnValue) {

                        PoolItemBehavior behavior = (PoolItemBehavior) target;
                        UserPortalItemModel model = behavior.getItem();
                        model.setEntity(returnValue);

                    }
                }), vmId);
    }

    private void UpdateProperties()
    {
        vm_pools entity = (vm_pools) getItem().getEntity();

        getItem().setName(entity.getvm_pool_name());
        getItem().setDescription(entity.getvm_pool_description());
        getItem().setIsPool(true);
        getItem().setIsServer(false);
        getItem().setStatus(VMStatus.Down);
        getItem().setIsFromPool(false);
        getItem().setPoolType(entity.getvm_pool_type());

        AsyncDataProvider.GetAnyVm(new AsyncQuery(this,
                new INewAsyncCallback() {
                    @Override
                    public void OnSuccess(Object target, Object returnValue) {

                        PoolItemBehavior behavior = (PoolItemBehavior) target;
                        VM vm = (VM) returnValue;
                        if (vm != null)
                        {
                            UserPortalItemModel model = behavior.getItem();
                            model.setOsType(vm.getvm_os());
                        }

                    }
                }), entity.getvm_pool_name());
    }

    private void UpdateActionAvailability()
    {
        getItem().getTakeVmCommand().setIsAvailable(true);

        getItem().getRunCommand().setIsAvailable(false);
        getItem().getPauseCommand().setIsAvailable(true);
        getItem().getShutdownCommand().setIsAvailable(true);
        getItem().getStopCommand().setIsAvailable(true);
        getItem().getReturnVmCommand().setIsAvailable(false);

        getItem().getPauseCommand().setIsExecutionAllowed(false);
        getItem().getShutdownCommand().setIsExecutionAllowed(false);
        getItem().getStopCommand().setIsExecutionAllowed(false);
    }
}

@@ -53,16 +53,65 @@
 ortal;%0A%0A
+import java.util.HashMap;%0Aimport java.util.Map;%0A%0A
 import o
@@ -408,16 +408,79 @@
 Status;%0A
+import org.ovirt.engine.core.common.businessentities.VmOsType;%0A
 import o
@@ -1209,16 +1209,197 @@
 avior%0A%7B%0A
+%0A    // this has to be static because in every request a new instance of this class is created%0A    private static Map%3CGuid, VmOsType%3E poolToOsType = new HashMap%3CGuid, VmOsType%3E();%0A%0A
     publ
@@ -3787,16 +3787,165 @@
 type());
+%0A        if (poolToOsType.containsKey(entity.getvm_pool_id())) %7B%0A            getItem().setOsType(poolToOsType.get(entity.getvm_pool_id()));%0A        %7D
 %0A%0A      
@@ -4473,24 +4473,133 @@
 etvm_os());%0A
+                            poolToOsType.put(((vm_pools) model.getEntity()).getvm_pool_id(), vm.getvm_os());%0A
             

OS type for pool was incorrect