/*
 * 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.Automation;
import com.jniwrapper.win32.automation.OleContainer;
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.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

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

    private JLabel lblClipFileName;
    private JLabel lblClipDuration;
    private JLabel lblVideoSize;
    private JCheckBox cbMute;
    private JPanel _fileInfo;

    public MediaPlayerContainer()
    {
        super("MediaPlayer.MediaPlayer.1",
                "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");

        lblClipDuration = new JLabel();
        lblClipFileName = new JLabel();
        lblVideoSize = new JLabel();
        cbMute = new JCheckBox("Mute");
    }

    public void loadFile(String fileName)
    {
        final OleContainer container = getContainer();
        Automation automation = new Automation(container.getOleObject());
        automation.setProperty("FileName", fileName);
        container.doVerb(OleVerbs.INPLACEACTIVATE);

        final Automation mediaPlayer = new Automation(getContainer().getOleObject());
        fileName = mediaPlayer.getProperty("FileName").getBstrVal().getValue();
        String duration = parseClipDuration((longmediaPlayer.getProperty("Duration").getDblVal().getValue());
        long videoWidth = mediaPlayer.getProperty("ImageSourceWidth").getIntVal().getValue();
        long videoHeight = mediaPlayer.getProperty("ImageSourceHeight").getIntVal().getValue();
        String videoSize = "";
        if (videoWidth != && videoHeight != 0)
        {
             videoSize = "Video Size: " + String.valueOf(videoWidth"x" + String.valueOf(videoHeight);
        }
        lblClipFileName.setText(fileName);
        lblClipDuration.setText(duration);
        lblVideoSize.setText(videoSize);
        cbMute.setSelected(mediaPlayer.getProperty("Mute").getBoolVal().getBooleanValue());
        cbMute.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                mediaPlayer.setProperty("Mute"new Variant(cbMute.isSelected()));
            }
        });

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

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

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

        JLabel lblClipFileNameLabel = new JLabel("Clip File Name:");
        JLabel lblClipDurationLabel = new JLabel("Clip Duration:");

        _fileInfo.add(lblClipFileNameLabel, new GridBagConstraints(00110.00.0
                , GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(10101010)00));
        _fileInfo.add(lblClipFileName, new GridBagConstraints(10110.00.0
                , GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(1001010)00));
        _fileInfo.add(cbMute, new GridBagConstraints(20110.00.0
                , GridBagConstraints.EAST, 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(11110.00.0
                , GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(001010)00));
        _fileInfo.add(lblVideoSize, new GridBagConstraints(21110.00.0
                , GridBagConstraints.EAST, 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;
    }

    private String parseClipDuration(long seconds)
    {
        StringBuffer result = new StringBuffer();
        long hh = seconds / (60*60);
        long mm = (seconds - (hh*60*60)) 60;
        long ss = seconds - (hh*60*60- mm*60;
        if (hh < 10)
        {
            result.append("0").append(String.valueOf(hh));
        }
        else
        {
            result.append(String.valueOf(hh));
        }
        result.append(":");
        if (mm < 10)
        {
            result.append("0").append(String.valueOf(mm));
        }
        else
        {
            result.append(String.valueOf(mm));
        }
        result.append(":");
        if (ss < 10)
        {
            result.append("0").append(String.valueOf(ss));
        }
        else
        {
            result.append(String.valueOf(ss));
        }
        return result.toString();
    }

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