define(["xaml!app/view/Panel"], function (Panel) {

    var ratings = [
        {
            d: 0,
            s: 0.5
        },
        {
            d: 20,
            s: 0.53
        },
        {
            d: 40,
            s: 0.58
        },
        {
            d: 60,
            s: 0.62
        },
        {
            d: 80,
            s: 0.66
        },
        {
            d: 100,
            s: 0.69
        },
        {
            d: 120,
            s: 0.73
        },
        {
            d: 140,
            s: 0.76
        },
        {
            d: 160,
            s: 0.79
        },
        {
            d: 180,
            s: 0.82
        },
        {
            d: 200,
            s: 0.84
        },
        {
            d: 300,
            s: 0.93
        },
        {
            d: 400,
            s: 0.97
        }
    ];


    return Panel.inherit({
        defaults: {
            inbox: null,
            taskA: null,
            taskB: null
        },
        _commitSelected: function () {
            if (this.$.catalog && this.$.catalog.size()) {
                this._selectNextTasks();
            }
        },

        _selectNextTasks: function () {
            var taskA = this.$.catalog.getNextTask();
            var taskB, found = false;

            this.set({
                taskA: taskA,
                taskB: this.$.catalog.getComparableTask(taskA)
            });


        },

        hasTwoTasked: function () {
            return this.$.taskA && this.$.taskB;
        }.onChange("taskA", "taskB"),

        rankTask: function (winner, loser) {
//            var weightDiff = winner.$.weight - loser.$.weight;
//            var score = this.getRatingForDifference(weightDiff);
//            console.log(weightDiff, score);

//            winner.set('weight', Math.round(winner.$.weight + 30 * (1 - score)));
//            loser.set('weight', Math.round(loser.$.weight - 30 * (1 - score)));

            this.$.catalog.priorizeTasks(winner, loser);
//            this.$.catalog.sort(function (a, b) {
//                return Math.random() < 0.5;
//            });

            this.$.catalog.sortTasks();

            this._selectNextTasks();

//            console.log(this.$.catalog.toArray(function (task) {
//                return {
//                    title: task.$.title,
//                    weight: task.$.weight,
//                    d: task.$.duration
//                }
//            }).sort(function (a, b) {
//                return a.weight <= b.weight;
//            }));

            console.log(this.$.catalog.toArray(function (t) {
                return t.$.title + " " + t.priority();
            }));
        },
        getRatingForDifference: function (diff) {
            var f = diff < 0 ? -1 : 1;
            diff = Math.abs(diff);
            var score;

            for (var i = 0; i < ratings.length; i++) {
                var rating = ratings[i];
                if (rating.d >= diff) {
                    score = rating.s;
                    break;
                }
            }

            if (!score) {
                score = ratings[ratings.length - 1];
            }

            if (f < 0) {
                return 1 - score;
            }
            return score;


        }

    })
});