/*
In NativeScript, a file with the same name as an XML file is known as
a code-behind file. The code-behind is a great place to place your view
logic, and to set up your page’s data binding.
*/

import { EventData, Observable } from 'data/observable';
import { Page } from 'ui/page';
import { HelloWorldModel } from './main-view-model';
import * as fs from "tns-core-modules/file-system";
let tmpObservable:Observable;
// Event handler for Page "navigatingTo" event attached in main-page.xml
export function navigatingTo(args: EventData) {
    /*
    This gets a reference this page’s <Page> UI component. You can
    view the API reference of the Page to see what’s available at
    https://docs.nativescript.org/api-reference/classes/_ui_page_.page.html
    */
    let page = <Page>args.object;
    tmpObservable = new Observable()
    tmpObservable.set("imageSource", "");
    /*
    A page’s bindingContext is an object that should be used to perform
    data binding between XML markup and TypeScript code. Properties
    on the bindingContext can be accessed using the {{ }} syntax in XML.
    In this example, the {{ message }} and {{ onTap }} bindings are resolved
    against the object returned by createViewModel().

    You can learn more about data binding in NativeScript at
    https://docs.nativescript.org/core-concepts/data-binding.
    */
    page.bindingContext = tmpObservable;
}
export function onTap(){
    console.log("onTap");
    var documents = fs.knownFolders.documents();
    var documentsPath = documents.path;
    console.log("documentsPath "+documentsPath);

    var fileName="icon.png"
    var sourceFile = fs.File.fromPath(fs.knownFolders.currentApp().path + "/images/" + fileName);
    var source = sourceFile.readSync(e=> { console.log("Error ",e); });
    
    var destinationFile = documents.getFile("test.png");
    destinationFile.writeSync(source, e=> { console.log("Error ", e); });
    
    var currentImagePath = documentsPath+"/" + "test.png"
    tmpObservable.set("imageSource", currentImagePath);
}