
//////////////////
/// Telescope
//////////////////

/// Telescope init

$('[data-control="telescope-lamp"]').click(function(){
    $(this).crud("GET", "/api/telescope/lamp").then(function(e){
        $(this).crud("PUT", "/api/telescope/lamp", !e.response)
    })
})

$('[data-control="telescope-power"]').click(function(){
    $(this).crud("GET", "/api/telescope/power").then(function(e){
        $(this).crud("PUT", "/api/telescope/power", !e.response)
    })
})

$('[data-control="telescope-connection"]').click(function(){
    $(this).crud("GET", "/api/telescope/connection").then(function(e){
        $(this).crud("PUT", "/api/telescope/connection", !e.response)
    })
})

$('[data-control="telescope-unpark"]').click(function(){
    $(this).crud("POST", "/api/telescope/coordinates/movement/unpark")
})

$('[data-control="telescope-park"]').click(function(){
    $(this).crud("POST", "/api/telescope/coordinates/movement/park")
})

$('[data-control="telescope-cover"]').click(function(){
    var data = $(this).data("value")
    $(this).crud("POST", "/api/telescope/cover/movement", data)
})

$('[data-control="telescope-error"]').click(function(){
    var data = $('[data-parameter="telescope-error"]').val()
    $(this).crud("DELETE", "/api/telescope/error", data)
})

$('[data-control="telescope-offset-zd-az"]').click(function(e){
    var zd = $('[data-parameter="telescope-offset-zd"]').val() / 3600.0
    var az = $('[data-parameter="telescope-offset-az"]').val() / 3600.0
    $(this).crud("PUT", "/api/telescope/coordinates/offset", [zd, az])
})

$('[data-control="telescope-offset-rotator"]').click(function(e){
    var data = $('[data-parameter="telescope-offset-rotator"]').val()
    $(this).crud("POST", "/api/telescope/rotator/movement", data)
})

$('[data-control="telescope-focuser"]').click(function(e){
    var data = $('[data-parameter="telescope-focuser"]').val()
    $(this).crud("PUT", "/api/telescope/focuser/movement", data*1.0)
})

/// Telescope coordinates

$('[data-control="telescope-coordinates-radec"]').click(function(){
    var data_in_button = $(this).data("value")
    if (data_in_button) {
        var data = data_in_button
    } else {
        var data = [$('[data-parameter="telescope-coordinates-radec"]').val()]

    }
    $(this).crud("POST", "/api/telescope/coordinates/movement/radec", data)
})

$('[data-control="telescope-coordinates-altaz"]').click(function(){
    var data_in_button = $(this).data("value")
    if (data_in_button) {
        var data = data_in_button
    } else {
        var data = [$('[data-parameter="telescope-coordinates-altitude"]').val(),
                    $('[data-parameter="telescope-coordinates-azimuth"]').val()]

    }
    $(this).crud("POST", "/api/telescope/coordinates/movement/altaz", data)
})

$("#coordinates-slew").on("click", function(){

    if ($("#altaz_tab").hasClass("active")){
        var data = [$('[data-parameter="telescope-coordinates-altitude"]').val(),
                    $('[data-parameter="telescope-coordinates-azimuth"]').val()]
        $(this).crud("POST", "/api/telescope/coordinates/movement/altaz", data)
    }

    if ($("#radec_tab").hasClass("active")){
        var data = $('[data-parameter="telescope-coordinates-radec"]').val()
        $(this).crud("POST", "/api/telescope/coordinates/movement/radec", data)
    }
})

$("#coordinates-stop").on("click", function(){
    $(this).crud("DELETE", "/api/telescope/coordinates/movement")
})

//////////////////
/// Dome
//////////////////

$('[data-control="dome-connection"]').click(function(){
    $(this).crud("GET", "/api/dome/connection").then(function(e){
        $(this).crud("PUT", "/api/dome/connection", !e.response)
    })
})

$('[data-control="dome-light"]').click(function(){
    $(this).crud("GET", "/api/dome/light").then(function(e){
        $(this).crud("PUT", "/api/dome/light", !e.response)
    })
})

$('[data-control="dome-azimuth"]').click(function(){
    var data = $('[data-parameter="dome-azimuth"]').val()
    $(this).crud("POST", "/api/dome/position/movement/azimuth", data)
})

$('[data-control="dome-sync"]').click(function(){
    var data = $('[data-parameter="dome-azimuth"]').val()
    $(this).crud("PUT", "/api/dome/position/sync", data)
})

$('[data-control="dome-slave"]').change(function(){
    var data = $(this).prop("checked")

    $('[data-control="dome-azimuth"]').prop("disabled", data)
    $('[data-parameter="dome-azimuth"]').prop("disabled", data)

    $(this).crud("PUT", "/api/dome/position/slaved", data)
})

$('[data-control="dome-stop"]').click(function(){
    $(this).crud("DELETE", "/api/dome/position/movement")
})

$('[data-control="dome-park"]').click(function(){
    $(this).crud("POST", "/api/dome/position/movement/park")
})

$('[data-control="dome-shutter"]').click(function(){
    var data = $(this).data("value")
    $(this).crud("POST", "/api/dome/shutter/movement", data)
})

//////////////////
/// Camera
//////////////////

/// Camera init

$('[data-control="camera-power"]').click(function(){
    $(this).crud("GET", "/api/camera/power").then(function(e){
        $(this).crud("PUT", "/api/camera/power", !e.response)
    })
})

$('[data-control="camera-cooler"]').change(function(){
    var data = $(this).prop("checked")
    $(this).crud("PUT", "/api/camera/cooler", data)
})

$('[data-control="camera-temperature"]').click(function(){
    var data = $('[data-parameter="camera-temperature"]').val()
    $(this).crud("PUT", "/api/camera/cooler/temperature/setpoint", data)
})

/// Camera controls

function update_subframe(xrange, yrange, binning) {
    if (binning) {
        $("#binning").val(binning[0])
    }

    $("#x0").val(xrange[0])
    $("#xf").val(xrange[1])
    $("#y0").val(yrange[0])
    $("#yf").val(yrange[1])

}

$("#binning").on("change", function(){
    var data = $(this).val()
    $(this).crud("PUT", "/api/camera/frame/binning", [data, data]).then(function(b){
        var binning = b.response
        $(this).crud("GET", "/api/camera/settings").then(function(e){
            var frame = e.response
            update_subframe(frame.xrange, frame.yrange, binning)
        })
    })
})

$("#frame-full").on("click", function(){
    $(this).crud("PUT", "/api/camera/frame/full").then(function(e){
        var frame = e.response
        update_subframe(frame.xrange, frame.yrange)
    })
})

$("#frame-half").on("click", function(){
    $(this).crud("PUT", "/api/camera/frame/half").then(function(e){
        var frame = e.response
        update_subframe(frame.xrange, frame.yrange)
    })
})

$("#frame-small").on("click", function(){
    $(this).crud("PUT", "/api/camera/frame/small").then(function(e){
        var frame = e.response
        update_subframe(frame.xrange, frame.yrange)
    })
})

var exp_value = 0
$("#frametyp").on("load, change", function(){
    var frametyp = $(this).val()
    if (frametyp=="Bias") {
        exp_value = $("#exptime").val()
        $("#exptime").val(0).prop("disabled", true)
    } else {
        $("#exptime").val(exp_value).prop("disabled", false)
    }

    if (frametyp=="Light") {
        $('[data-control="recenter"]').prop("checked",true)
        $('[data-parameter="recenter-box"]').prop("disabled", false)
    } else {
        $('[data-control="recenter"]').prop("checked",false)
        $('[data-parameter="recenter-box"]').prop("disabled", true)
    }

    if (frametyp=="Light") {
        $('[data-control="domeslewing"]').prop("checked",true)
    } else {
        $('[data-control="domeslewing"]').prop("checked",false)
    }

})

$('[data-control="recenter"]').change(function(){

    var data = $(this).prop("checked")
    var box = $('[data-parameter="recenter-box"]')
    var size = Number(box.val())

    box.prop("disabled", !data)

    if(data){
        $(this).crud("POST", "/api/camera/snapshot/recenter").then(function(e){
            $(this).crud("PUT", "/api/camera/snapshot/recenter", size)
        })
    } else {
        $(this).crud("DELETE", "/api/camera/snapshot/recenter")
    }

})

$('[data-parameter="recenter-box"]').change(function(){

    var size = Number($(this).val())
    $(this).crud("PUT", "/api/camera/snapshot/recenter", size)

})

$('[data-control="domeslewing"]').change(function(){

    var data = $(this).prop("checked")

    if(data){
        $(this).crud("POST", "/api/camera/snapshot/domeslewing")
    } else {
        $(this).crud("DELETE", "/api/camera/snapshot/domeslewing")
    }

})

$("#camera-start").on("click", function(){
    var data = {
        "template": "observation",
        "params": {
            "object": $("#object").val(),
            "frametype": $("#frametyp").val(),
            "filter": $("#filter").val(),
            "exptime": Number($("#exptime").val()),
            "repeat": Number($("#repeat").val()),
        }
    }
    $(this).crud("POST", "/api/sequencer/run", data)
})

$("#camera-stop").on("click", function(){
    $(this).crud("DELETE", "/api/sequencer/run")
})


//////////////////
/// Sequencer
//////////////////

$('[data-control="start-sequencer"]').on("click", function(e){
    var data = $('[data-parameter="ob-list-options"]').val()
    $(this).crud("POST", "/api/sequencer/run", data)
})

$('[data-control="pause-sequencer"]').on("click", function(e){
    $(this).crud("PUT", "/api/sequencer/run", false)
})

$('[data-control="resume-sequencer"]').on("click", function(e){
    $(this).crud("PUT", "/api/sequencer/run", true)
})

$('[data-control="stop-sequencer"]').on("click", function(e){
    $(this).crud("DELETE", "/api/sequencer/run")
})

//////////////////
/// Blocks
//////////////////

$(document).on("click", "#create-ob", function(e){
    e.preventDefault();
    var data = $("#new-ob-name").val()
    $(this).crud("POST", "/api/blocks/", data).then(function(e){
        location.href = data;
    })
});

$(document).on("click","#update-ob", function(e){
    e.preventDefault();
    var data  = []
    $("fieldset").each(function() {
        data.push({"template": $(this).data("template"),
                   "params":  serializeObject($(this))
                  })
    })
    var name = $("form").data("name")
    $(this).crud("PUT", "/api/blocks/"+name, data)
});

$(document).on("click", "#delete-ob", function(e){
    e.preventDefault();
    var data = $("form").data("name")
    var form = $("form[data-name='"+name+"']")
    $(this).crud("DELETE", "/api/blocks/", data).then(function(e){
        form.fadeOut(500, function(){ $(this).remove()})
        location.href = "./"
    })
});

$(document).on("click",'[data-control="delete-template-instance"]', function(e){
    e.preventDefault();
    var name = $(this).data("name")
    var data = $(this).data("parameter") // starting from 0?
    console.log("clic", name, data)
    var fieldset = $("fieldset[data-parameter="+data+"]")
    $(this).crud("DELETE", "/api/blocks/"+name, data).then(function(e){
        $("input", fieldset).css("border","1px solid red")
        fieldset.fadeOut(500, function(){ $(this).remove()})
    })
});

// Reload on list selection
$(document).on("change","#tpl-list-options", function(e){
    e.preventDefault();
    var name = $("form").data("name")
    var data = $(this).val()

    $(this).crud("POST", "/api/blocks/"+name, data).then(function(e){
        location.reload()
    })

});

// Reload on list selection
$('[data-parameter="blocks-list-options"]').on("change", function(e){
    var val = $(this).val()
    if (val!=null){
        $.get(val, {included:true}, function(data){
            $("#ob-list").html(data)
        })
    }
}).trigger("change")

//////////////////
/// Display output test
//////////////////

