using System.Collections.Generic;
using UnityEngine;
using B83.Win32;


public class FileDragAndDrop : MonoBehaviour
{
    List<string> log = new List<string>();
    void OnEnable ()
    {
        // must be installed on the main thread to get the right thread id.
        UnityDragAndDropHook.InstallHook();
        UnityDragAndDropHook.OnDroppedFiles += OnFiles;
    }
    void OnDisable()
    {
        UnityDragAndDropHook.UninstallHook();
    }

    void OnFiles(List<string> aFiles, POINT aPos)
    {
        string file = "";
        // scan through dropped files and filter out supported image types
        foreach (var f in aFiles)
        {
            var fi = new System.IO.FileInfo(f);
            var ext = fi.Extension.ToLower();
            if (ext == ".png" || ext == ".jpg" || ext == ".jpeg")
            {
                file = f;
                break;
            }
        }
        // If the user dropped a supported file, create a DropInfo
        if (file != "")
        {
            var data = System.IO.File.ReadAllBytes(file);
            var tex = new Texture2D(1, 1);
            tex.LoadImage(data);
            tex.name = new System.IO.FileInfo(file).Name;
            Error.Log(Color.green, "Loading file " + tex.name);
            Error.Log(Color.green, "Not implemented!");
            //ModelViewerMain.GetInstance().Builder.SpawnReference(tex);
        }
        else
        {
            Error.Log(Color.yellow, "Dropped file is not supported");
        }
    }
}
