/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package de.nplusc.izc.iZpl.extensions;

import com.sun.jna.Native;
import com.sun.jna.PointerType;
import com.sun.jna.Structure;
import com.sun.jna.WString;
import com.sun.jna.platform.win32.User32;
import com.sun.jna.platform.win32.WinDef.HWND;
import com.sun.jna.platform.win32.WinUser.MSG;
import de.nplusc.izc.tools.IOtools.FileTK;
import de.nplusc.izc.tools.WinToolz.User32Extended;
import de.nplusc.izc.tools.baseTools.Tools;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.prefs.Preferences;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import org.jawin.COMException;
//import org.jawin.donated.win32.MiscellaneousConstants;
//import org.jawin.donated.win32.User32;
//import org.jawin.donated.win32.W32Process;
//import org.jawin.donated.win32.WS_Constants;
//import org.jawin.donated.win32.User32;

/**
 *
 * @author LH
 */
public class AudioSurfCore
{

    private static HWND AudioSurf;
    private static JFrame target;

    public static void getAudioSurfWindow()
    {
        AudioSurf = User32Extended.INSTANCE.FindWindow(null, "Audiosurf");//new WString("Audiosurf"));
    }

    public static void launchAudioSurf(JFrame t)
    {
        target=t;
            new Thread(new Runnable()
            {
                @SuppressWarnings("ResultOfObjectAllocationIgnored")
                @Override
                public void run()
                {
                    if (Preferences.userRoot().get("izpl.audioSurfPath", "...").equals("..."))
                    {
                        String targetDirectory = Tools.FileChooseDlg(null, true, false, new String[]
                        {
                            "exe"
                        });
                        Preferences.userRoot().put("izpl.audioSurfPath", targetDirectory);
                    }
                    String tdir = Preferences.userRoot().get("izpl.audioSurfPath", "...");
                    System.out.println(tdir);
                    //HAXTST
                    tdir = FileTK.getFilePath(tdir);
                    //tdir+="\\engine";
                    String texe = tdir + "\\Audiosurf.exe";
                    Tools.runSingleCmd(true, true, false, true, new JTextArea(), System.out, tdir, true, texe);
                }
            }).start();
            try
            {
                Thread.sleep(4500);
            }
            catch (InterruptedException ex)
            {
                ex.printStackTrace();
            }
            try
            {
                Thread.sleep(500);
            }
            catch (InterruptedException ex)
            {
                ex.printStackTrace();
            }
           /* 
            * int hInst = W32Process.GetModuleHandleW(null);
            int hwnd = User32.CreateWindowExW(0, "Communicator", // window class name
                    "Communicating with AudioSurf. . .", // window caption
                    WS_Constants.WS_OVERLAPPEDWINDOW, // window style
                    MiscellaneousConstants.CW_USEDEFAULT, // initial x position
                    MiscellaneousConstants.CW_USEDEFAULT, // initial y position
                    MiscellaneousConstants.CW_USEDEFAULT, // initial x size
                    MiscellaneousConstants.CW_USEDEFAULT, // initial y size
                    MiscellaneousConstants.NULL, // parent window handle
                    MiscellaneousConstants.NULL, // window menu handle
                    hInst, // program instance handle
                    MiscellaneousConstants.NULL); // creation parameters*/

            getAudioSurfWindow();
            System.out.println(AudioSurf);
            //System.out.println(AudioSurf.getPointer().getLong(0));
            startSong("D:\\mp3\\assorted Earworms\\22 - StarDisc - Fernando Pepe.flac", "eraser");
    }

    public static void startSong(String songPath, String charaname)
    {

        String message_i = "ascommand quickstartqueuecommand ascommand playsongmono"/*+charaname*/ + " " + songPath;
        WString message = new WString(message_i);
        System.out.println(message_i);
        WM_COPYDATA.ByReference data = new WM_COPYDATA.ByReference();
        data.dwData = 1;
        data.lpData = message;
        data.cbData = (message.length() * 2) + 2;
        System.out.println(User32Extended.INSTANCE.SendMessageW(AudioSurf, 74, (int)Native.getWindowID(target), data));
    }

    //repeat: ascommand playsongcurrentcharacter c:\my music\nin\the way out is through.mp3
    ///init
    /*
     ascommand playsongmono c:\music\somesong.mp3
     ascommand playsongpointman c:\music\somesong.mp3
     ascommand playsongdoublevision c:\music\somesong.mp3
     ascommand playsongmonopro c:\music\somesong.mp3
     ascommand playsongvegas c:\music\somesong.mp3
     ascommand playsongeraser c:\music\somesong.mp3
     ascommand playsongpointmanpro c:\music\somesong.mp3
     ascommand playsongpusher c:\music\somesong.mp3
     ascommand playsongdoublevisionpro c:\music\somesong.mp3
     ascommand playsongninjamono c:\music\somesong.mp3
     ascommand playsongeraserelite c:\music\somesong.mp3
     ascommand playsongpointmanelite c:\music\somesong.mp3
     ascommand playsongpusherelite c:\music\somesong.mp3
     ascommand playsongdoublevisionelite c:\music\somesong.mp3
     // backgroundmode
     ascommand playsongfreeride d:\music\09 Eternal Life.wma
     */
    public static boolean isReadyForSong()
    {
        return false;
    }

    public static class WM_COPYDATA extends Structure
    {

        @Override
        protected List getFieldOrder()
        {
            return Arrays.asList(new String[]
            {
                "dwData", "cbData", "lpData"
            });
        }

        public static class ByReference extends WM_COPYDATA implements Structure.ByReference
        {
        };
        public int dwData;
        public int cbData;
        public WString lpData;

        protected ByReference newByReference()
        {
            return new ByReference();
        }
    }
}
