/*******************************************************************************
 * Copyright (c) 2012-2016 Codenvy, S.A.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *   Codenvy, S.A. - initial API and implementation
 *******************************************************************************/
package org.eclipse.che.ide.extension.machine.client.targets.categories.docker;


import com.google.gwt.core.client.GWT;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;
import com.google.inject.Inject;
import com.google.inject.Singleton;

import org.eclipse.che.ide.ui.zeroclipboard.ClipboardButtonBuilder;

/**
 * The implementation of {@link DockerView}.
 *
 * @author Oleksii Orel
 */
@Singleton
public class DockerViewImpl implements DockerView {

    private static final DockerViewImplUiBinder UI_BINDER = GWT.create(DockerViewImplUiBinder.class);

    private final FlowPanel rootElement;

    private ActionDelegate delegate;

    @UiField
    TextBox targetName;

    @UiField
    TextBox owner;

    @UiField
    TextBox type;

    @UiField
    TextBox sourceType;

    @UiField
    TextBox sourceUrl;

    @UiField
    TextArea sourceContent;

    @Inject
    public DockerViewImpl(ClipboardButtonBuilder buttonBuilder) {
        this.rootElement = UI_BINDER.createAndBindUi(this);
        this.rootElement.setVisible(true);

        buttonBuilder.withResourceWidget(sourceUrl).build();
        buttonBuilder.withResourceWidget(sourceContent).build();
    }


    @Override
    public Widget asWidget() {
        return rootElement;
    }

    @Override
    public void setDelegate(ActionDelegate delegate) {
        this.delegate = delegate;
    }

    @Override
    public void setTargetName(String targetName) {
        this.targetName.setValue(targetName);
    }

    @Override
    public void setOwner(String owner) {
        this.owner.setValue(owner);
    }

    @Override
    public void setType(String type) {
        this.type.setValue(type);
    }

    @Override
    public void setSourceType(String sourceType) {
        this.sourceType.setValue(sourceType);
    }

    @Override
    public void setSourceUrl(String sourceUrl) {
        this.sourceUrl.setValue(sourceUrl);
    }

    @Override
    public void setSourceContent(String sourceContent) {
        this.sourceContent.setValue(sourceContent);
    }

    public void updateTargetFields(DockerMachineTarget target) {
        this.setTargetName(target.getName());
        this.setOwner(target.getOwner());
        this.setType(target.getType());
        this.setSourceType(target.getSourceType());

        final String sourceUrl = target.getSourceUrl();

        if (sourceUrl != null && sourceUrl.length() > 0) {
            this.setSourceUrl(target.getSourceUrl());
            this.sourceUrl.getParent().setVisible(true);
        } else {
            this.setSourceContent(target.getSourceContent());
            this.sourceContent.getParent().setVisible(true);
        }
    }

    @Override
    public boolean restoreTargetFields(DockerMachineTarget target) {
        return this.delegate != null && this.delegate.onRestoreTargetFields(target);
    }


    interface DockerViewImplUiBinder extends UiBinder<FlowPanel, DockerViewImpl> {
    }
}
