/*
 *  Angular implementation of the logic
 */
var app = angular.module('app', []);
app.controller('BookingController', function ($scope, $http) {

    $scope.isAvailabilityRequestViewVisible = true;
    $scope.isAvailabilityResponseViewVisible = false;
    $scope.isLastRequestsViewVisible = false;
    $scope.lastRequests = [];
    $scope.inboundJourneys = [];
    $scope.outboundJourneys = [];

    $scope.lastRequests_Click = function () {

        loading.show();

        $http({ method: AvailabilityRequest.serviceCallVerb, url: AvailabilityRequest.baseServiceUrl + "/GetLastRequests" }).
        success(function (data, status, headers, config) {
            loading.hide();

            $scope.lastRequests = data.GetLastRequestsResult;
            $scope.isAvailabilityRequestViewVisible = false;
            $scope.isAvailabilityResponseViewVisible = false;
            $scope.isLastRequestsViewVisible = true;
        }).
        error(function (data, status, headers, config) {
            loading.hide();
            errorModal.show(data);
        });
    };

    $scope.getAvailability_Click = function () {

        if ($scope.selectedOrigin && $scope.selectedDestination && $scope.selectedOrigin != $scope.selectedDestination && AvailabilityRequest.departureDate && AvailabilityRequest.returnDate) {

            loading.show();
            msgError.hide();
            var data = {
                request: {
                    Origin: $scope.selectedOrigin,
                    Destination: $scope.selectedDestination,
                    DepartureDate: AvailabilityRequest.departureDate.toJSON(),
                    ReturnDate: AvailabilityRequest.returnDate.toJSON()
                }
            };

            $http({ method: AvailabilityRequest.serviceCallVerb, url: AvailabilityRequest.baseServiceUrl + "/GetAvailability", data: data }).
                success(function (res, status, headers, config) {
                    
                    loading.hide();
                    $scope.inboundJourneys = res.GetAvailabilityResult.InboundJourneys;
                    $scope.outboundJourneys = res.GetAvailabilityResult.OutboundJourneys;

                    $scope.isAvailabilityRequestViewVisible = false;
                    $scope.isAvailabilityResponseViewVisible = true;
                    $scope.isLastRequestsViewVisible = false;
                }).
                error(function (res, status, headers, config) {
                    loading.hide();
                    errorModal.show(res);
                });
        } else {
            msgError.show();
        }
    };

    $scope.btnNewSearch_Click = function () {

        $scope.isAvailabilityRequestViewVisible = true;
        $scope.isAvailabilityResponseViewVisible = false;
        $scope.isLastRequestsViewVisible = false;
    };

    $scope.btnBackLastRequests_Click = function () {
        $scope.isAvailabilityRequestViewVisible = true;
        $scope.isAvailabilityResponseViewVisible = false;
        $scope.isLastRequestsViewVisible = false;
    };

    //  helper functions
    $scope.formatDate = function (date) {
        return moment(date).format("DD-MM-YYYY, hh:mm:ss");
    };

});