/*
 * Copyright (C) 2013 Snowsun <http://wyrez.org> and contributors
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package org.wyrez.shootingstars.gui;

import com.jme3.input.event.MouseButtonEvent;
import com.jme3.math.Vector2f;
import org.wyrez.shootingstars.gui.controls.ButtonBase;
import org.wyrez.shootingstars.gui.listener.YTDownloadListener;
import org.wyrez.shootingstars.helper.ScreenHelper;
import tonegod.gui.controls.buttons.Button;
import tonegod.gui.controls.text.Label;
import tonegod.gui.controls.text.TextField;
import tonegod.gui.controls.windows.Panel;
import tonegod.gui.core.Screen;

/**
 *
 * @author Snowsun
 */
public class YTDownloadGUI extends Panel implements Gui {

    private YTDownloadListener listener;
    private ScreenHelper screenHelper;

    public YTDownloadGUI(Screen screen, YTDownloadListener listener, ScreenHelper screenHelper) {
        super(screen, new Vector2f(0f, 0f), new Vector2f(1280f, 720f)); //create for full hd
        this.listener = listener;
        this.screenHelper = screenHelper;
        this.setIgnoreMouse(true);
        this.setIsVisible(false);
        create();
    }

    private void create() {
        float startPointx = screenHelper.calcX(384f);
        float startPointy = screenHelper.calcY(324f);
        float labelFontSize = screenHelper.calcX(51.2f);
        float buttonWidth = screenHelper.calcX(153f);
        float textFieldWitdh = screenHelper.calcX(409f);
        float marginTextFieldLeft = screenHelper.calcX(153f);
        float marginTextFieldTop = screenHelper.calcY(8.64f);
        float marginYTButtonLeft = screenHelper.calcX(250f);
        float marginCancelButtonLeft = screenHelper.calcX(409f);
        float marginYTButtonTop = screenHelper.calcY(72f);
        float labelWitdh = screenHelper.calcX(160f);
        float labelHeight = screenHelper.calcY(40f);

        float buttonFontSize = screenHelper.calcX(20f);
        float buttonHeight = screenHelper.calcY(40f);
        float textFieldHeight = screenHelper.calcY(40f);


        Label lblYTLink = new Label(screen, new Vector2f(startPointx, startPointy), new Vector2f(labelWitdh, labelHeight));
        lblYTLink.setFontSize(labelFontSize);
        lblYTLink.setText("YT Link");

        final TextField txtYTLink = new TextField(screen, "txtYTLink", new Vector2f(startPointx + marginTextFieldLeft, startPointy + marginTextFieldTop), new Vector2f(textFieldWitdh, textFieldHeight));
        txtYTLink.setType(TextField.Type.DEFAULT);
        txtYTLink.setFontSize(buttonFontSize);

        Button btnYTDownload = new ButtonBase(screen, new Vector2f(startPointx + marginYTButtonLeft, startPointy + marginYTButtonTop), new Vector2f(buttonWidth, buttonHeight)) {
            @Override
            public void onButtonMouseLeftUp(MouseButtonEvent evt, boolean toggled) {
                listener.downloadYTVideo(txtYTLink.getText());
            }
        };
        btnYTDownload.setText("Download");
        btnYTDownload.setFontSize(buttonFontSize);

        Button btnCancel = new ButtonBase(screen, new Vector2f(startPointx + marginCancelButtonLeft, startPointy + marginYTButtonTop), new Vector2f(buttonWidth, buttonHeight)) {
            @Override
            public void onButtonMouseLeftUp(MouseButtonEvent evt, boolean toggled) {
                listener.cancel();
            }
        };
        btnCancel.setText("Cancel");
        btnCancel.setFontSize(buttonFontSize);

        this.addChild(lblYTLink);
        this.addChild(txtYTLink);
        this.addChild(btnYTDownload);
        this.addChild(btnCancel);
    }

    public void refresh() {
    }

    public void attach() {
        screen.addElement(this);
    }

    public void detach() {
        screen.removeElement(this);
    }
}
