/*
 * Copyright (c) 2012-2016. Sencha Inc.
 */

"use strict";

var Fashion = require('../Base.js');
var Type = require('./Type.js');

class LinearGradient extends Type {
    constructor(position, stops) {
        super();
        this.position = position;
        this.stops = stops;
    }

    doVisit(visitor) {
        visitor.lineargradient(this);
    }

    descend(visitor) {
        visitor.visit(this.position);
        visitor.visit(this.stops);
    }

    copy() {
        return new LinearGradient(this.position, this.stops);
    }

    gradientPoints(position) {

    }

    operate(operation, right) {
        switch (operation) {
            case "!=":
                if (right.type == 'literal' && (right.value == 'null' || right.value == 'none')) {
                    return true;
                }
            case "==":
                if (right.type == 'literal' && (right.value == 'null' || right.value == 'none')) {
                    return false;
                }
        }
        return super.operate(operation, right);
    }

    supports(prefix) {
        return !!(this.vendorPrefixes[prefix.toLowerCase()]);
    }

    toString() {
        var string = 'linear-gradient(';
        if (this.position) {
            string += (this.position + ', ');
        }
        return string + this.stops + ')';
    }

    toOriginalWebkitString() {
        // args = []
        // args << grad_point(position_or_angle || Sass::Script::String.new("top"))
        // args << linear_end_position(position_or_angle, color_stops)
        // args << grad_color_stops(color_stops)
        // args.each{|a| a.options = options}
        // Sass::Script::String.new("-webkit-gradient(linear, #{args.join(', ')})")
        //this.gradientPoints(this.position);
        var args = [],
            stops = this.stops.items,
            ln = stops.length,
            i;

        args.push('top');
        args.push('bottom');

        for (i = 0; i < ln; i++) {
            args.push(stops[i].toOriginalWebkitString());
        }

        return '-webkit-gradient(linear, ' + args.join(', ') + ')';
    }

    toPrefixedString(prefix) {
        if (prefix === 'owg') {
            return this.toOriginalWebkitString();
        }
        return prefix + this.toString();
    }
}

Fashion.apply(LinearGradient.prototype, {
    type: 'lineargradient',
    $isFashionLinearGradient: true,
    $canUnbox: false,
    position: null,
    stops: null,
    vendorPrefixes: {
        webkit: true,
        moz: true,
        svg: true,
        pie: true,
        css2: true,
        o: true,
        owg: true
    }
});

module.exports = LinearGradient;
