import QtQuick 2.0
import AndroidNative 1.0

Item {

    /// Set it to true if multiple images should be picked.
    property bool multiple: false

    /// If it is true, it will broadcast the taked photo to other application (e.g Let it show in Google Photos)
    property bool broadcast: true

    /// The URL of the image chosen. If multiple images are picked, it will be equal to the first image.
    property string imageUrl: ""

    /// A list of images chosen
    property var imageUrls: []

    /// It is emitted whatever photo(s) are picked/taken.
    signal ready();

    function pickImage() {
        SystemDispatcher.dispatch(m_PICK_IMAGE_MESSAGE,{ multiple: multiple});
    }

    function takePhoto() {
        SystemDispatcher.dispatch(m_TAKE_PHOTO_MESSAGE,{
                                      broadcast: broadcast
                                  })
    }

    property string m_PICK_IMAGE_MESSAGE: "androidnative.ImagePicker.pickImage";

    property string m_TAKE_PHOTO_MESSAGE: "androidnative.ImagePicker.takePhoto";

    property string m_CHOSEN_MESSAGE: "androidnative.ImagePicker.chosen";


    Connections {
        target: SystemDispatcher
        onDispatched: {
            if ((type === m_CHOSEN_MESSAGE) && (root.imagePicking==true)) {
                var h=[];
                for (var n in message.imageUrls){
                    h.push("file://"+ decodeURIComponent(message.imageUrls[n]).substring(5))
                }
                imageUrls=h;
                imageUrl=h[0];
                ready();
                root.imagePicking=false
            }
        }
    }

    Component.onCompleted: {
        SystemDispatcher.loadClass("androidnative.ImagePicker");
    }
}