package info.knacki.pass.io;

import android.content.res.Resources;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import info.knacki.gitdroid.callback.OnResponseListener;
import info.knacki.pass.R;

class RawFileInterface implements IFileInterface {
    private static File fFile;
    private final String fMethodName;

    RawFileInterface(Resources r, File f) {
        fFile = f;
        fMethodName = r.getString(R.string.pref_enc_type_title_none);
    }

    @Override
    public void ReadFile(OnResponseListener<String> resp) {
        try {
            resp.OnResponse(FileUtils.ReadAllFile(fFile));
        }
        catch (IOException e) {
            resp.OnError(e.getMessage(), e);
        }
    }

    @Override
    public void WriteFile(String content, OnResponseListener<Void> resp) {
        try {
            FileWriter writer = new FileWriter(fFile);
            writer.write(content);
            writer.close();
            resp.OnResponse(null);
        }
        catch (IOException e) {
            resp.OnError(e.getMessage(), e);
        }
    }

    @Override
    public String GetMethodName() {
        return fMethodName;
    }
}
