﻿function PlayController(brdWrapper) {
    var board = brdWrapper.board;
    var gui = brdWrapper.gui;
    var Counter = $(brdWrapper).data('counter');
    var TaskID = $(brdWrapper).data('task-id');
    var TaskFinished = false;
    var Type = $(brdWrapper).data('type');
    var attemps = 0;
    var curRight = 0;
    var curWrong = 0;
    var askedMoves = 1;
    var aUrl = $(brdWrapper).data('move-callback');
    var lang = chessBoardLang;

    this.MoveMade =
    function (aMove, callback) {
        if (Type == "ChessTask") {
            var context = $(document).find("#puzzleMessage")[0];
            var showLoader = setTimeout(function () { context.innerHTML = '<div class=\"ajaxload\">' + lang.checkingMove + '</div>' }, 300);
            $.ajax({
                type: "Post",
                url: aUrl,
                dataType: "json",
                data: {
                    Move: aMove,
                    CurCounter: Counter,
                    CurTaskID: TaskID,
                    CurAttemps: attemps,
                    CurRight: curRight,
                    CurWrong: curWrong,
                    AskedMoves: askedMoves
                },
                context: context,
                complete: function () { clearTimeout(showLoader); },
                success: function (msg, status) {
                    var Result = false;
                    var Actions = [];

                    Result = msg["Result"];
                    if (Result)
                        curRight++;
                    else
                        curWrong++;
                    Actions = msg["Actions"];
                    attemps = msg['curAttemps'];
                    if (Counter != msg["Counter"]) {
                        Counter = msg["Counter"];
                        askedMoves++;
                    }
                    gui.BoardDisabled = msg["IsFinished"];
                    TaskFinished = msg["IsFinished"];
                    gui.TaskFinished = TaskFinished;
                    gui.ShowActions(Actions);

                    var textMessage;

                    if (!msg["IsFinished"]) {
                        if (Result) {
                            this.className = 'correct';
                            textMessage = lang.correctMove;
                        }
                        else {
                            this.className = 'incorrect';
                            textMessage = lang.incorrectMove;
                        }
                    }
                    else {
                        var infoElement = $(document).find('#puzzle_information')[0];

                        if (msg["IsSolved"]) {
                            textMessage = "<span class=\"accept_icon\">" + lang.problemSolved + " (" + msg["Score"] + ")</span>";
                            $(infoElement).addClass('solved');
                        }
                        else {
                            textMessage = "<span class=\"cancel_icon\">" + lang.problemUnsolved + " (" + msg["Score"] + ")</span>"
                                + '<br/><span style="font-size:12px; font-weight: normal;">' + lang.tooManyMistakes + '</span>';
                            $(infoElement).addClass('unsolved');
                        }                        
                        this.className = 'solved';

                        if ($('#rate').length != 0) {
                            $.fn.raty.readOnly(false, '#rate');
                        }

                        if ($('.next_practice'))
                            $('.next_practice').show();

                        var obtainedElo = parseInt(msg['ObtainedElo']);
                        var c = obtainedElo >= 0 ? '+' : '';
                        if (msg['IsUserCheater'])
                            $('#elo_change').html(lang.UserIsCheater);
                        else
                            if (msg['IsRatingChanged'])
                                $('#elo_change').html(lang.eloChange + ' ' + msg["NewRating"] + ' ( ' +
                                    c + msg['ObtainedElo'] + ' )');
                            else if (msg['IsUserAuthenticated'])
                                $('#elo_change').html(lang.eloNotChanged);

                        $('#task_info').hide();
                        $('#user_elo').html('(' + msg["NewRating"] + ')');

                    }
                    this.innerHTML = textMessage;
                    callback(Result);

                    if (Actions.length == 0 && msg["IsFinished"]) {
                        $('.task_notation .line').addClass('active').live('click', function () {
                            loadMove($(this).attr('data-fen'), this, $(this).attr('data-move'));
                        });
                    }
                },
                error: function (jqXHR, textStatus, errorThrown) {
                    this.innerHTML = lang.internalServerError;
                    callback(false);
                }
            });
        }
        else {
            callback(true);
        }
    }
      
}
