(function () {
    // Mock repository
    let adverts = [
        {
            _id: 0,
            _acl: {
                creator: 0
            },
            title: "XBoss 1080",
            publisher: "Pesho",
            datePublished: "2017-06-04",
            price: 100
        }
    ];

    let users = [
        {
            _kmd: {
                authtoken: "mock_token0"
            },
            _id: 0,
            username: "Pesho",
            password: "p"
        },
        {
            _kmd: {
                authtoken: "mock_token1"
            },
            _id: 1,
            username: "Gosho",
            password: "g"
        },
        {
            _kmd: {
                authtoken: "mock_token2"
            },
            _id: 2,
            username: "Maria",
            password: "m"
        }
    ];

    // User login
    $.mockjax(function (requestSettings) {
        if (requestSettings.url === "https://mock.backend.com/user/kid_rk/login") {
            return {
                response: function (origSettings) {
                    if (requestSettings.headers["Authorization"] === "Basic a2lkX3JrOjczNjgwNGE2Njg=") {
                        let target = users.filter(u => u.username === requestSettings.data.username && u.password === requestSettings.data.password);
                        if (target.length === 0) {
                            this.status = 403;
                            this.responseText = "You are not authorized";
                        } else {
                            this.responseText = target[0];
                        }
                    } else {
                        this.status = 403;
                        this.responseText = "You are not authorized";
                    }
                }
            };
        }
    });

    // User create
    $.mockjax(function (requestSettings) {
        if (requestSettings.url === "https://mock.backend.com/user/kid_rk/" &&
            requestSettings.method === "POST") {
            return {
                response: function (origSettings) {
                    if (requestSettings.headers["Authorization"] === "Basic a2lkX3JrOjczNjgwNGE2Njg=") {
                        let data = requestSettings.data;
                        let lastId = 0;
                        if (users.length > 0) {
                            lastId = users.map(u => u._id).sort((a, b) => b - a)[0];
                        }
                        let user = {
                            _kmd: {
                                authtoken: `mock_token${lastId}`
                            },
                            _id: lastId,
                            username: data.username,
                            password: data.password
                        };
                        users.push(user);
                        this.responseText = user;
                    } else {
                        this.status = 403;
                        this.responseText = "You are not authorized";
                    }
                }
            };
        }
    });

        // Loading of adverts
    $.mockjax(function (requestSettings) {
        if (requestSettings.url==="https://mock.api.com/appdata/kid_rk/adverts" &&
            requestSettings.method === "GET") {
            return {
                response: function (origSettings) {
                    if (requestSettings.headers["Authorization"].includes("Kinvey mock_token")) {
                        this.responseText = adverts;
                    } else {
                        this.status = 403;
                        this.responseText = "You are not authorized";
                    }
                }
            };
        }
    });
})();


@@ -176,16 +176,66 @@
  1080%22,%0A
+            description: %22Modded gaming console%22,%0A
         
@@ -321,16 +321,59 @@
 ice: 100
+,%0A            image: %22./static/fuze-f1.png%22
 %0A       
@@ -3826,15 +3826,887 @@
     %7D);%0A
+        // Load single advert%0A    $.mockjax(function (requestSettings) %7B%0A        if (requestSettings.url.match(/https:%5C/%5C/mock%5C.api%5C.com%5C/appdata%5C/kid_rk%5C/adverts%5C/(.+)/) &&%0A            requestSettings.method === %22GET%22) %7B%0A            let advertId = Number(requestSettings.url.match(/https:%5C/%5C/mock%5C.api%5C.com%5C/appdata%5C/kid_rk%5C/adverts%5C/(.+)/)%5B1%5D);%0A            return %7B%0A                response: function (origSettings) %7B%0A                    if (requestSettings.headers%5B%22Authorization%22%5D.includes(%22Kinvey mock_token%22)) %7B%0A                        let advert = adverts.filter(a =%3E a._id === advertId);%0A                        this.responseText = advert.shift();%0A                    %7D else %7B%0A                        this.status = 403;%0A                        this.responseText = %22You are not authorized%22;%0A                    %7D%0A                %7D%0A            %7D;%0A        %7D%0A    %7D);%0A%0A
 %7D)();%0A%0A

task 6.3