package @APP_PACKAGE@.content;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.MatrixCursor;
import android.net.Uri;
import android.os.ParcelFileDescriptor;
import android.util.Log;

import @APP_PACKAGE@.XBMCFile;
import @APP_PACKAGE@.XBMCJsonRPC;
import @APP_PACKAGE@.model.File;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;

public class XBMCFileContentProvider extends XBMCContentProvider
{
  private static String TAG = "@APP_NAME@_File_Provider";

  public static final String AUTHORITY = AUTHORITY_ROOT + ".file";

  private XBMCJsonRPC mJsonRPC = null;

  public static Uri buildUri(String surl)
  {
    if (surl == null)
      return null;
    if (surl.isEmpty())
      return null;

    Uri.Builder builder = new Uri.Builder();
    builder.scheme("content")
            .authority(AUTHORITY)
            .fragment(surl);

    Uri out = builder.build();
    // Log.d(TAG, "GetImageUri: in:" + surl + " out:" + out.toString());
    return out;
  }

  @Override
  public int delete(Uri arg0, String arg1, String[] arg2)
  {
    // TODO Auto-generated method stub
    return 0;
  }

  @Override
  public String getType(Uri arg0)
  {
    return "vnd.android.cursor.dir/xbmc_file";
  }

  @Override
  public Uri insert(Uri arg0, ContentValues arg1)
  {
    // TODO Auto-generated method stub
    return null;
  }

  @Override
  public boolean onCreate()
  {
    mJsonRPC = new XBMCJsonRPC();

    return true;
  }

  @Override
  public int update(Uri arg0, ContentValues arg1, String arg2, String[] arg3)
  {
    // TODO Auto-generated method stub
    return 0;
  }

  @Override
  public Cursor query(Uri uri, String[] projection, String selection,
                      String[] selectionArgs, String sortOrder)
  {
    String xbmcURL = uri.getFragment();

    List<File> files = mJsonRPC.getFiles(xbmcURL);
    if (files.isEmpty())
      return null;

    String[] fileCols = new String[]
            {
                    File.NAME,
                    File.CATEGORY,
                    File.URI,
                    File.ID,
                    File.MEDIATYPE
            };
    MatrixCursor mc = new MatrixCursor(fileCols);

    for (File file : files)
    {
      mc.addRow(new Object[]
              {
                      file.getName(),
                      file.getCategory(),
                      file.getUri(),
                      file.getId(),
                      file.getMediatype()
              });
    }
    return mc;
  }

  @Override
  public ParcelFileDescriptor openFile(Uri uri, String mode)
          throws FileNotFoundException
  {
    // Log.d(TAG, "openFile: " + uri.toString());

    ParcelFileDescriptor[] pipe = null;

    try
    {
      String decodedUrl = uri.getFragment();
      // Log.d(TAG, "  decodedUrl: " + decodedUrl);
      if (decodedUrl == null)
        return null;

      pipe = ParcelFileDescriptor.createPipe();

      new XBMCFileContentProvider.TransferThread(decodedUrl,
              new ParcelFileDescriptor.AutoCloseOutputStream(pipe[1])).start();
    }
    catch (IOException e)
    {
      Log.e(getClass().getSimpleName(), "Exception opening pipe", e);
      throw new FileNotFoundException("Could not open pipe for: "
              + uri.toString());
    }

    return (pipe[0]);
  }


  static class TransferThread extends Thread
  {
    String path;
    OutputStream out;

    TransferThread(String path, OutputStream out)
    {
      this.path = path;
      this.out = out;
    }

    @Override
    public void run()
    {
      try
      {
        XBMCFile in = new XBMCFile();
        if (!in.Open(path))
        {
          out.flush();
          out.close();
          return;
        }

        while (!in.Eof())
        {
          byte[] buf = in.Read();
          out.write(buf, 0, buf.length);
        }

        in.Close();
        out.flush();
        out.close();
      }
      catch (Exception e)
      {
        Log.e(getClass().getSimpleName(), "Exception transferring file", e);
      }
    }
  }

}
