
local function start_scan(self)
    if camera ~= nil then

        local sysinfo = sys.get_sys_info()

        local quality = camera.CAPTURE_QUALITY_HIGH
        local type = camera.CAMERA_TYPE_FRONT
        self.flip = 0
        if sysinfo.system_name == 'iPhone OS' then
            type = camera.CAMERA_TYPE_BACK
            quality = camera.CAPTURE_QUALITY_MEDIUM
            self.flip = 1
        end

        if camera.start_capture(type, quality) then
            self.cameraframe = camera.get_frame()
            self.camerainfo = camera.get_info()
            print("Initialized camera")
            self.cameratextureheader = {width=self.camerainfo.width,
            height=self.camerainfo.height,
            type=resource.TEXTURE_TYPE_2D,
            format=resource.TEXTURE_FORMAT_RGB,
            num_mip_maps=1 }
        end
    else
        print("could not start camera capture")
    end
end

local function end_scan(self)
    if self.cameraframe ~= nil then
        self.cameraframe = nil
        camera.stop_capture()
        self.first = 0
    end
end

local function update_scan(self)

    if self.cameraframe ~= nil then
        local texturepath = go.get("#sprite", "texture0")
        resource.set_texture(texturepath, self.cameratextureheader, self.cameraframe)

        local text = qrcode.scan(self.cameraframe, self.camerainfo.width, self.camerainfo.height, self.flip)

        if text ~= nil then
            msg.post("gui", "set_text", {text=text})
            end_scan(self)
            self.mode = "IDLE"
            return
        end
    end
end

local function generate_qrcode(self, text)
    local qrcode, qrsize = qrcode.generate(text)

    local qrcodetextureheader = {width=qrsize,
                                height=qrsize,
                                type=resource.TEXTURE_TYPE_2D,
                                format=resource.TEXTURE_FORMAT_LUMINANCE,
                                num_mip_maps=1 }

    local texturepath = go.get("#sprite", "texture0")
    resource.set_texture(texturepath, qrcodetextureheader, qrcode)
end

function init(self)
    local logosize = 256
    local screen_width = sys.get_config("display.width", 600)
    local screen_height = sys.get_config("display.height", 800)
    local scale_width = screen_width / logosize
    local scale_height = screen_height / logosize

    go.set("#sprite", "scale", vmath.vector3(scale_width, scale_height, 1) )

    start_scan(self)

    if qrcode == nil then
        print("could not find qrcode module")
    end

    self.mode = "SCAN"
end

function final(self)
    end_scan(self)
end

function update(self, dt)
    if self.mode == "SCAN" then
        update_scan(self)
    end
end

function on_input(self, action_id, action)
    if action_id == hash("click") or action_id == hash("touch") then
        print("foo")
    end
end

function on_message(self, message_id, message)
    if self.mode ~= message.mode then
        if message.mode == "SCAN" then
            start_scan(self)

            pprint(self.cameraframe)
        else
            end_scan(self)
            generate_qrcode(self, message.text)
        end
        self.mode = message.mode
    end
end


