/*
 * Copyright (c) 2002-2007 TeamDev Ltd. All rights reserved.
 *
 * Use is subject to license terms.
 *
 * The complete licence text can be found at
 * http://www.teamdev.com/comfyj/license.jsf
 */
package com.jniwrapper.win32.samples.demo;

import com.jniwrapper.win32.automation.OleContainer;
import com.jniwrapper.win32.automation.Automation;
import com.jniwrapper.win32.automation.types.Variant;
import com.jniwrapper.win32.ole.types.OleVerbs;
import com.jniwrapper.util.Logger;

import javax.swing.*;
import javax.imageio.ImageIO;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.*;

/**
 @author Vladimir Kondrashchenko
 */
public class OCXMediaPlayerContainer extends OleContainerInfoBean
{
    private static final Logger LOG = Logger.getInstance(OCXMediaPlayerContainer.class);


    private static final int STATE_PLAYING = 3;

    private JLabel lblClipName;
    private JLabel lblClipDuration;
    private JCheckBox cbMute;
    private JCheckBox cbFitToWindow;
    private JPanel _fileInfo;

    public OCXMediaPlayerContainer()
    {
        super("WMPlayer.OCX",
                "Media Files (*.avi; *.mpg; *.mpeg; *.wmv; *.asf; *.mp3)|*.avi;*.mpg;*.mpeg;*.wmv;*.asf; *.mp3",
                "This page demonstrates the MediaPlayer ActiveX component embedded into OleContainer.""avi");
    }

    public void loadFile(String fileName)
    {
        final OleContainer container = getContainer();
        final Automation mediaPlayer = new Automation(container.getOleObject());
        mediaPlayer.setProperty("URL", fileName);

        new Thread(new Runnable()
        {
            public void run()
            {
                final Automation media = new Automation(mediaPlayer.getProperty("currentMedia").getPdispVal());
                final Automation settings = new Automation(mediaPlayer.getProperty("settings").getPdispVal());

                do
                {
                    try
                    {
                        Thread.sleep(100);
                    }
                    catch (InterruptedException e)
                    {
                    }
                while (mediaPlayer.getProperty("playState").getIntVal().getValue() != STATE_PLAYING);

                String name = media.getProperty("Name").getBstrVal().getValue();
                String duration = media.getProperty("durationString").getBstrVal().getValue();

                lblClipName.setText(name);
                lblClipDuration.setText(duration);
                cbMute.setSelected(settings.getProperty("mute").getBoolVal().getBooleanValue());
                cbMute.addActionListener(new ActionListener()
                {
                    public void actionPerformed(ActionEvent e)
                    {
                        settings.setProperty("mute"new Variant(cbMute.isSelected()));
                    }
                });

                cbFitToWindow.setSelected(mediaPlayer.getProperty("stretchToFit").getBoolVal().getBooleanValue());
                cbFitToWindow.addActionListener(new ActionListener()
                {
                    public void actionPerformed(ActionEvent e)
                    {
                        mediaPlayer.setProperty("stretchToFit"new Variant(cbFitToWindow.isSelected()));
                    }
                });

                _fileInfo.revalidate();
                _fileInfo.repaint();
            }
        }).start();
    }

    public ImageIcon getIcon()
    {
        ImageIcon icon = null;
        try
        {
            icon = new ImageIcon(ImageIO.read(ExcelContainer.class.getResourceAsStream("res/wmp.png")));
        }
        catch (Exception e)
        {
            LOG.error("", e);
        }
        return icon;
    }

    public JPanel createFileInfoPanel()
    {
        lblClipDuration = new JLabel();
        lblClipName = new JLabel();
        cbMute = new JCheckBox("Mute");
        cbFitToWindow = new JCheckBox("Fit To Window");

        _fileInfo = new JPanel(new GridBagLayout());
        _fileInfo.setBorder(BorderFactory.createTitledBorder("Media File Info"));
        _fileInfo.setPreferredSize(new Dimension(10075));

        JLabel lblClipNameLabel = new JLabel("Media Name:");
        JLabel lblClipDurationLabel = new JLabel("Media Duration:");

        _fileInfo.add(lblClipNameLabel, new GridBagConstraints(00110.00.0
                , GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(10101010)00));
        _fileInfo.add(lblClipName, new GridBagConstraints(10111.00.0
                , GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(1001010)00));
        _fileInfo.add(cbMute, new GridBagConstraints(20110.00.0
                , GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(001010)00));
        _fileInfo.add(lblClipDurationLabel, new GridBagConstraints(01110.00.0
                , GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0101010)00));
        _fileInfo.add(lblClipDuration, new GridBagConstraints(11111.00.0
                , GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(001010)00));
        _fileInfo.add(cbFitToWindow, new GridBagConstraints(21110.00.0
                , GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(001010)00));

        _fileInfo.add(new Panel()new GridBagConstraints(02311.01.0
                , GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets(0000)00));

        return _fileInfo;
    }

    public void activate()
    {
        final OleContainer container = getContainer();
        container.doVerb(OleVerbs.INPLACEACTIVATE);
    }
}