package com.algobase.share.app;


import java.io.File;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.FileOutputStream;

import android.content.Context;
import android.content.res.AssetManager;

import android.graphics.Typeface;

public class Assets
{
  Context context;
  AssetManager assetManager;


  public Assets(Context ctxt) { 
    context = ctxt;
    assetManager = ctxt.getResources().getAssets();
  }

  public void copyFile(String name, File outfile)
  { 
    byte[] buffer = new byte[1024];
    try {
       InputStream in = assetManager.open(name);
       OutputStream out = new FileOutputStream(outfile);
       int n;
       while((n = in.read(buffer)) != -1) out.write(buffer,0,n);
       in.close();
       out.close();
      } catch(Exception e) {}
   }


  public String getString(String name)
  {
    byte[] buffer = new byte[1024];
    int len = -1;

    try {
      InputStream in = assetManager.open(name);
      len = in.read(buffer);
      in.close();
     } catch(Exception e) {}

    if (len == -1)
      return "";
    else
      return new String(buffer,0,len).trim();
  }

  public int getInt(String name, int def_value)
  { int value = def_value;
    try { 
      value = Integer.parseInt(getString(name));
    } catch (Exception ex) {}
   return value;
  }

  public long getLong(String name, long def_value)
  { long value = def_value;
    try { 
      value = Long.parseLong(getString(name));
    } catch (Exception ex) {}
   return value;
  }



  public Typeface getTypeface(String name)
  { String fpath = "fonts/" + name;
    return Typeface.createFromAsset(assetManager,fpath);
   }


}
