# See: http://wiki.flightgear.org/MapStructure
# Class things:
var name = 'HOLD';
var parents = [canvas.DotSym];
var __self__ = caller(0)[0];
canvas.DotSym.makeinstance( name, __self__ );
 
#canvas.SymbolLayer.get(name).df_style = {
#    radius: 13,
#};
 
var element_type = "group";

var draw_pattern = func(dist, ){
    var pattern = me.element.createChild("path", 'hold-pattern');
    pattern.set("z-index",4);
    var w = (dist * 2) / math.pi;
    var h = dist * 70;
    var r = w / 2;
    pattern.arcSmallCW(r,r,0,70 * w,0).
    line(0,h).
    arcSmallCW(r,r,0,-70 * w,0).
    line(0,-h).
    setStrokeLineWidth(5).
    setColor(me.active_color);
    return pattern;
};

var draw_hold_sym = func(turn){
    var hold_symbol = me.element.createChild("group", 'hold-symbol');
    var svg = "Nasal/canvas/map/Airbus/Images/airbus_hold"~turn~".svg";
    canvas.parsesvg(hold_symbol, svg);
    hold_symbol.setTranslation(-32,-64).set('z-index', 4);
    return hold_symbol;
}

var init = func {
    var type = me.model.type;
    var dist = me.model.dist;
    var turn = me.model.turn;
    me.active_color = me.getStyle('active_color',
                                  me.getOption('active_route_color', 
                                               [0.4,0.7,0.4]));
    me.inactive_color = me.getStyle('inactive_color',
                                    me.getOption('inactive_route_color', 
                                                 [0.95,0.95,0.21]));
    if(type == 'pattern'){
        me.hold_pattern = me.draw_pattern(dist);
        me.hold_symbol = nil;
    } else {
        #print('CREATING HOLD SYM');
        me.hold_pattern = nil;
        me.hold_symbol = me.draw_hold_sym(turn);
    }
}

var draw = func{
    var type = me.model.type;
    var map = me.layer.map;
    var pos = map.getPos();
    if(type == 'pattern'){
        var dist = me.model.dist;
        if(me.hold_symbol != nil){
            me.hold_symbol.hide();
        }
        if(me.hold_pattern == nil)
            me.hold_pattern = me.draw_pattern(dist);
        if(me.hold_pattern == nil) return;
        var radial = me.model.radial;
        if(radial != nil){

            var hdg = pos[2];
            if(hdg == nil) hdg = 0;
            radial -= hdg;
            if(radial < 0) radial = 360 + radial; 
            me.element.setRotation(radial*D2R);
        }
        var rng = pos[3];
        if(rng == nil) rng = 10;
        rng =  10 / rng;
        me.element.setScale(rng,rng);
        var lat_ctrl = getprop(me.getOption('lat_ctrl'));
        var lnav = (lat_ctrl == me.getOption('lat_ctrl_managed','fmgc'));
        var actv = getprop(me.getOption('fplan_active'));
        #print('HOLD ' ~ lnav ~ ',' ~ actv);
        if(!lnav or !actv)
            me.hold_pattern.setStrokeDashArray([32, 16]);
        else
            me.hold_pattern.setStrokeDashArray([1,0]);
        if(!actv)
            me.hold_pattern.setColor(me.inactive_color);
        else 
            me.hold_pattern.setColor(me.active_color);
    } else {
        var turn = me.model.turn;
        if(me.hold_pattern != nil){
            me.hold_pattern.hide();
        }
        if(me.hold_symbol == nil)
            me.hold_symbol = me.draw_hold_sym(turn);

        me.hold_symbol.show();
        me.element.setScale(1,1);
        me.element.setRotation(0);
    }
}