"use strict";

import {
    MapScreen, Tutorial, Windows,
    AnimationController
} from "./loadResources";
import { Connection, PLAYER } from './Connection';
import { Observer } from "./Observer";
import { CONFIG, TEXTURES } from "./configs/Config";
import { TYPES } from "./tiles/TypesTiles";
import { OK_methods } from "./OK";
import { Util } from "./Util";
import { Components } from "./Components";
import { TRANSLATION } from "./configs/Languages";


export class MapInit extends PIXI.Sprite {
    constructor() {
        super();

        this.rewards = [];
        this.numberMap = 0;
        this.countMaps = CONFIG.json_configs.info.countMaps;


        this._width = CONFIG.global.sizeScreen.width;
        this._height = CONFIG.global.sizeScreen.height;

        this.layerLevel = new PIXI.Container();
        this.panelFriends = new PanelFriendsInit();
        this.layerTooltip = new PIXI.Container();
        this.layerPlaces = new PIXI.Container();
        this.layerPhoto = new PIXI.Container();

        this.levels = [];
        this.isOpen = false;
        this.infoMaps = {
            map_1: [
                {
                    x: 245, y: 315,
                    tooltip: {
                        offsetPointer: 0,
                        width: 260,
                        x: 30, y: -120,
                    }
                },
                {
                    x: 585, y: 145,
                    tooltip: {
                        offsetPointer: -40,
                        width: 260,
                        x: -50, y: -30,
                    }
                },
                {
                    x: 560, y: 410,
                    tooltip: {
                        offsetPointer: 50,
                        width: 240,
                        x: -80, y: -140,
                    }
                }
            ],
            map_2: [
                {
                    x: 240, y: 318,
                    tooltip: {
                        offsetPointer: 30,
                        x: 35, y: -170,
                    }
                },
                {
                    x: 580, y: 160,
                    tooltip: {
                        width: 270,
                        offsetPointer: -40,
                        x: -85, y: -30,
                    }
                },
                {
                    x: 535, y: 390,
                    tooltip: {
                        offsetPointer: 30,
                        width: 270,
                        x: -80, y: -140,
                    }
                }
            ],
            map_3: [
                {
                    x: 222, y: 292,
                    tooltip: {
                        offsetPointer: 30,
                        width: 280,
                        x: 40, y: -160,
                    }
                },
                {
                    x: 576, y: 174,
                    tooltip: {
                        offsetPointer: -40,
                        width: 320,
                        x: -80, y: -50,
                    }
                },
                {
                    x: 560, y: 410,
                    tooltip: {
                        offsetPointer: 50,
                        width: 300,
                        x: -100, y: -170,
                    }
                }
            ],
            map_4: [
                {
                    x: 225, y: 292,
                    tooltip: {
                        offsetPointer: 30,
                        width: 260,
                        x: 40, y: -160,
                    }
                },
                {
                    x: 570, y: 194,
                    tooltip: {
                        offsetPointer: -20,
                        width: 240,
                        x: -100, y: -70,
                    }
                },
                {
                    x: 530, y: 412,
                    tooltip: {
                        offsetPointer: 40,
                        width: 255,
                        x: -105, y: -160,
                    }
                }
            ],
            map_5: [
                {
                    x: 225, y: 336,
                    tooltip: {
                        offsetPointer: 20,
                        width: 220,
                        x: 65, y: -110,
                    }
                },
                {
                    x: 577, y: 168,
                    tooltip: {
                        offsetPointer: -40,
                        width: 270,
                        x: -70, y: -50,
                    }
                },
                {
                    x: 559, y: 396,
                    tooltip: {
                        offsetPointer: 30,
                        width: 250,
                        x: -65, y: -160,
                    }
                }
            ],
            map_6: [
                {
                    x: 202, y: 325,
                    tooltip: {
                        offsetPointer: 20,
                        width: 220,
                        x: 75, y: -110,
                    }
                },
                {
                    x: 580, y: 158,
                    tooltip: {
                        offsetPointer: -40,
                        width: 220,
                        x: -65, y: -50,
                    }
                },
                {
                    x: 564, y: 404,
                    tooltip: {
                        offsetPointer: 30,
                        width: 230,
                        x: -75, y: -150,
                    }
                }
            ]
        };

        this.background = Components.BitmapData('', { anchor: 0 });

        this.graph = Components.Graphics(this._width, this._height);
        this.graph.animationLoad = Components.Animation(TEXTURES.animations.loadingTrain, {
            loop: true,
            layer: this.graph,
            x: this._width / 2,
            y: this._height / 2
        });
        this.graph.animationLoad.visible = false;

        this.name = Components.Text('', CONFIG.fonts.brown24);
        this.name.x = this._width / 2;
        this.name.y = 90;

        this.labelCloseMap = Components.Text(TRANSLATION['newEpisodes'](), CONFIG.fonts.brown22);
        this.labelCloseMap.x = this._width / 2;
        this.labelCloseMap.y = 145;

        this.playerPhoto = new PhotoOnMap(this.levels);

        //#region кнопки вперед и назад
        this.btnPrev = CONFIG.buttons.map_next();
        this.btnPrev.x = this._width / 2 - 140;
        this.btnPrev.y = this.name.y;
        this.btnPrev.pointertap = () => {
            if (this.numberMap > 1)
                this.setMap(this.numberMap - 1);
        };

        this.btnNext = CONFIG.buttons.map_next();
        this.btnNext.scale.set(-1, 1);
        this.btnNext.x = this._width / 2 + 140;
        this.btnNext.y = this.name.y;
        this.btnNext.pointertap = () => {
            if (this.numberMap <= this.countMaps)
                this.setMap(this.numberMap + 1);
        };
        //#endregion

        for (let i = 0; i < 19; i++) {
            this.levels[i] = new ButtonLevel(i);
            this.layerLevel.addChild(this.levels[i]);
        }

        this.setLevelsPosition();

        this.addChild(this.background);
        this.background.addChild(this.layerLevel);
        this.background.addChild(this.layerPlaces);
        this.background.addChild(this.name);
        this.background.addChild(this.labelCloseMap);
        this.background.addChild(this.layerPhoto);
        this.background.addChild(this.btnPrev);
        this.background.addChild(this.btnNext);
        this.background.addChild(this.layerTooltip);
        this.background.addChild(this.graph);
        this.background.addChild(this.panelFriends);

        //#region кнопка окна исследований
        this.positionBtnResearch = {
            x: CONFIG.global.sizeScreen.width - 50,
            y: this.panelFriends.y - 50
        };
        let btnResearch = Components.Button(TEXTURES.mapScreen.btnResearch_1, TEXTURES.mapScreen.btnResearch_2, { filter: CONFIG.filters.bright_12 });
        btnResearch.x = this.positionBtnResearch.x;
        btnResearch.y = this.positionBtnResearch.y;
        btnResearch.pointertap = () => {
            Windows.Research.updateInfo();
            btnResearch.notif.visible = false;
            btnResearch.notif.value.text = 0;
            Observer.dispatch(Observer.Map_OpenResearch);
        };
        this.background.addChild(btnResearch);

        btnResearch.notif = Components.BitmapData(TEXTURES.mapScreen.count);
        btnResearch.notif.visible = false;
        btnResearch.notif.x = 30;
        btnResearch.notif.y = 10;
        btnResearch.notif.scale.set(0.9);
        btnResearch.addChild(btnResearch.notif);
        btnResearch.notif.value = Components.Text('0', CONFIG.fonts.white20);
        btnResearch.notif.addChild(btnResearch.notif.value);
        this.checkNewRewards = () => {
            btnResearch.notif.value.text = Number(btnResearch.notif.value.text) + 1;
            btnResearch.notif.visible = true;
        };
        //#endregion

        this.layerRewards = new PIXI.Container();
        this.background.addChild(this.layerRewards);
    }

    updateLevels() {
        return new Promise((resolve) => {
            let index = this.numberMap * this.levels.length - this.levels.length;

            this.levels.forEach(level => {
                level.setData(index + 1);
                index++;
            });

            resolve();
        });
    }

    setMap(number = PLAYER.playerMap) {
        return new Promise((resolve) => {
            this.graph.show();

            this.numberMap = number <= this.countMaps
                ? number
                : this.countMaps + 1;

            if (this.numberMap - 1 === 0) {
                this.btnPrev.alpha = 0.5;
                this.btnPrev.interactive = false;
            } else {
                this.btnPrev.alpha = 1;
                this.btnPrev.interactive = true;
            }

            this.isOpen = (PLAYER.level >= this.numberMap * this.levels.length - this.levels.length) && number <= this.countMaps;

            if (!this.isOpen || number > this.countMaps) {
                this.btnNext.alpha = 0.5;
                this.btnNext.interactive = false;
            } else {
                this.btnNext.alpha = 1;
                this.btnNext.interactive = true;
            }

            this.getTextureBackground(this.numberMap)
                .then((result) => setTimeout(() => {
                    Promise.all([
                        Util.clearLayer(this.layerPlaces),
                        Util.clearLayer(this.layerTooltip)
                    ]).then(() => {
                        this.graph.hide();

                        this.graph.animationLoad.visible = false;
                        this.graph.animationLoad.stop();

                        this.background.setTexture(result);

                        this.labelCloseMap.visible = number > this.countMaps;
                        this.name.visible = !this.labelCloseMap.visible;

                        if (TRANSLATION.hasOwnProperty(`map_${number}`))
                            this.name.text = TRANSLATION[`map_${number}`].name();
                        if (this.isOpen) {
                            let infoMap = this.infoMaps['map_' + this.numberMap];

                            if (infoMap != null)
                                for (let i = 0; i < infoMap.length; i++)
                                    this.layerPlaces.addChild(new InterestingPlace(i, infoMap, this.layerTooltip, this.numberMap));

                            this.playerPhoto.checkPosition(this.numberMap);
                            this.updateLevels();
                            this.layerLevel.visible = true;
                            this.layerPlaces.visible = true;
                            this.layerPhoto.visible = true;
                        } else {
                            this.layerLevel.visible = false;
                            this.layerPlaces.visible = false;
                            this.layerPhoto.visible = false;
                        }
                        resolve();
                    });
                }, 150));
        });
    }

    getTextureBackground(number) {
        return new Promise((resolve) => {
            let textures = TEXTURES.mapScreen.maps;

            if (!this.isOpen)
                resolve(TEXTURES.mapScreen.map_closed);
            else if (textures[`map_${number}`] != null)
                resolve(textures[`map_${number}`]);
            else {
                this.graph.animationLoad.visible = true;
                this.graph.animationLoad.play();

                let loader = new PIXI.loaders.Loader();
                loader.add(`map_${number}`, `../Data/sprites/maps/map_${number}.png`);
                loader.load(function (loader, resources) {
                    textures[`map_${number}`] = resources[`map_${number}`].texture;
                    resolve(textures[`map_${number}`]);
                });
            }
        });
    }

    setFriends() {
        for (let i = 0; i < PLAYER.friends.info.length; i++) {
            let info = PLAYER.friends.info[i], index;
            if (info.level < 19)
                index = info.level;
            else
                index = info.level % 19;

            this.levels[index].setPhotoFriend(info.level, info.photoUrl, info.firstName, this.layerPhoto);
        }
        this.layerPhoto.addChild(this.playerPhoto);
    }

    showRewards() {
        Util.clearLayer(this.layerRewards)
            .then(() => {
                for (let i = 0; i < this.rewards.length; i++) {
                    let texture = null;

                    if (TYPES.rewardsStars.hasOwnProperty(this.rewards[i]))
                        texture = TEXTURES.gameObjects['rewards_stars'][this.rewards[i]];
                    else if (TYPES.rewardsWin.hasOwnProperty(this.rewards[i])) {
                        texture = TEXTURES.gameObjects['rewards_win'][this.rewards[i]];
                        this.checkNewRewards();
                    }

                    let item = this.createReward(texture);
                    this.layerRewards.addChild(item);

                    setTimeout(() => PixiApp.ticker.add(item.moveToCenter), i * 50);
                }
            });
    }

    createReward(texture) {
        let endPosition = {
            x: this.positionBtnResearch.x,
            y: this.positionBtnResearch.y
        },
            offsetX = Util.getRandomInt(-5, 5) * 10,
            offsetY = Util.getRandomInt(-5, 5) * 10 - 50;

        let item = Components.BitmapData(texture);
        item.y = Util.getRandomInt(0, this._height);
        item.x = Util.getRandomInt(0, 1) === 0
            ? -100
            : this._width + 100;
        item.tick = 0;
        item.speed = Util.getSpeed(item, {
            x: this._width / 2 + offsetX,
            y: this._height / 2 + offsetY
        }, 30);

        item.moveToCenter = () => {
            if (item.tick < item.speed.tick) {
                item.tick++;
                item.x += item.speed.x;
                item.y += item.speed.y;
            } else {
                PixiApp.ticker.remove(item.moveToCenter);
                item.x = this._width / 2 + offsetX;
                item.y = this._height / 2 + offsetY;
                item.speed = Util.getSpeed(item, endPosition, 20);
                item.tick = 0;

                setTimeout(() => PixiApp.ticker.add(item.move), 1000);
            }
        };
        item.move = () => {
            if (item.tick < item.speed.tick) {
                item.tick++;
                item.x += item.speed.x;
                item.y += item.speed.y;

                if (item.scale.x - 0.02 > 0.3) {
                    item.scale.x -= 0.02;
                    item.scale.y -= 0.02;
                }
            } else {
                PixiApp.ticker.remove(item.move);
                this.layerRewards.removeChild(item);
            }
        };

        return item;
    }

    setLevelsPosition() {
        this.levels[0].setPosition(58, 384, { x_way: 0, y_way: 2 });
        this.levels[1].setPosition(106, 412, { x_way: -5, y_way: -2 });
        this.levels[2].setPosition(161, 430, { x_way: 0, y_way: 0 });
        this.levels[3].setPosition(215, 440, { x_way: 5, y_way: -5 });
        this.levels[4].setPosition(268, 437, { x_way: 0, y_way: -25 });
        this.levels[5].setPosition(320, 416, { x_way: 3, y_way: -45 });
        this.levels[6].setPosition(346, 369, { x_way: -10, y_way: -55 });
        this.levels[7].setPosition(339, 315, { x_way: -40, y_way: -50 });
        this.levels[8].setPosition(305, 263, { x_way: -25, y_way: -55 });
        this.levels[9].setPosition(283, 208, { x_way: -3, y_way: -50 });
        this.levels[10].setPosition(314, 161, { x_way: 2, y_way: -10 });
        this.levels[11].setPosition(369, 154, { x_way: 2, y_way: 4 });
        this.levels[12].setPosition(410, 194, { x_way: 0, y_way: 1 });
        this.levels[13].setPosition(449, 237, { x_way: 0, y_way: 2 });
        this.levels[14].setPosition(499, 268, { x_way: 0, y_way: 0 });
        this.levels[15].setPosition(558, 274, { x_way: 0, y_way: -6 });
        this.levels[16].setPosition(616, 271, { x_way: 5, y_way: -30 });
        this.levels[17].setPosition(671, 246, { x_way: -2, y_way: -52 });
        this.levels[18].setPosition(696, 199, { x_way: 0, y_way: 0 });
    }
}

class PanelFriendsInit extends PIXI.Sprite {
    constructor() {
        super();

        this.stage = 0;
        this.items = [];
        this.infoFriends = PLAYER.friends.info
            .concat(PLAYER.friends.all_info)
            .sort((friend) => {
                if (friend.web === true)
                    return -1;
                else
                    return 1;
            });

        let background = Components.BitmapData(TEXTURES.panelFriends.background);
        background.anchor.set(0);
        this.addChild(background);

        this.y = CONFIG.global.sizeScreen.height - background.height;

        let btnPrev = Components.Button(TEXTURES.panelFriends.btnNext_1, TEXTURES.panelFriends.btnNext_2);
        btnPrev.x = 35;
        btnPrev.y = background.height / 2;
        btnPrev.pointertap = () => {
            if (this.stage > 1)
                this.setStage(this.stage - 1);
        };
        background.addChild(btnPrev);

        let btnNext = Components.Button(TEXTURES.panelFriends.btnNext_1, TEXTURES.panelFriends.btnNext_2);
        btnNext.scale.set(-1, 1);
        btnNext.x = CONFIG.global.sizeScreen.width - btnPrev.x;
        btnNext.y = background.height / 2;
        btnNext.pointertap = () => {
            if ((this.stage + 1) * this.items.length < this.infoFriends.length)
                this.setStage(this.stage + 1);
        };
        background.addChild(btnNext);

        for (let i = 0; i < 8; i++) {
            this.items[i] = new ItemPanelFriends();
            this.items[i].x = 100 + i * 80;
            this.items[i].y = 7;
            background.addChild(this.items[i]);
        }

        this.setStage(0);
    }

    setStage(number) {
        let index = number * this.items.length,
            numberItem = 0;

        this.numberStage = number;

        for (let i = index; i < this.infoFriends.length; i++) {
            if (numberItem === this.items.length)
                break;
            this.items[i].setInfo(this.infoFriends[i]);
            numberItem++;
        }

        if (numberItem < this.items.length)
            for (let i = numberItem; i < this.items.length; i++)
                this.items[i].setInfo('default');
    }
}

class ItemPanelFriends extends PIXI.Sprite {
    constructor() {
        super();

        this.interactive = true;

        this.uid = null;

        this.background = Components.BitmapData(TEXTURES.panelFriends.photo_invite);
        this.background.anchor.set(0.5, 0);
        this.addChild(this.background);

        this.photo = Components.BitmapData('');
        this.photo.y = 33;

        this.addChild(this.photo);

        this.button = CONFIG.buttons.green_Mini();
        this.button.y = this.background.height - 12;
        this.button.setText(TRANSLATION.invite(), {
            fontFamily: 'GothicB',
            fill: '#ffffff',
            fontSize: '10px',
            align: 'center',
            stroke: 'rgba(0,0,0,0.35)',
            strokeThickness: 3
        });
        this.button.pointertap = () => {
            OK_methods.invite(this.uid, TRANSLATION['text_inviteFriend']());
        };
        this.addChild(this.button);

        this.star = Components.BitmapData(TEXTURES.panelFriends.level_star);
        this.star.scale.set(0.95);
        this.star.x = 24;
        this.star.y = 47;
        this.addChild(this.star);
        this.star.level = Components.Text('1', {
            fontFamily: 'GOTHICB',
            fontStyle: 'normal',
            fontWeight: 'bold',
            fontSize: '14px',
            fill: '#ae5e2e',
            align: 'center'
        });
        this.star.addChild(this.star.level);

        //#region паутина
        this.web = Components.BitmapData(TEXTURES.panelFriends.web);
        this.web.anchor.set(0);
        this.web.x = -this.background.width / 2 - 4;
        this.web.y = -4;
        this.web.interactive = true;
        this.web.mouseover = () => {
            this.web.filters = [CONFIG.filters.bright_11];
        };
        this.web.mouseout = () => {
            this.web.filters = [];
        };
        this.web.pointertap = () => {
            OK_methods.send_notification(this.uid, TRANSLATION['notification_web']());
        };
        this.addChild(this.web);
        //#endregion

        //#region монетка
        this.coin = Components.BitmapData(TEXTURES.panelFriends.coin);
        this.coin.anchor.set(0);
        this.coin.x = - this.background.width / 2 - 4;
        this.coin.y = -4;
        this.coin.interactive = true;
        this.coin.mouseover = () => {
            this.coin.filters = [CONFIG.filters.bright_11];
        };
        this.coin.mouseout = () => {
            this.coin.filters = [];
        };
        this.coin.pointertap = () => {
            OK_methods.send_notification(this.uid, TRANSLATION['notification_coin'](), () => {
                this.coin.visible = false;
                for (let i = 0; i < 5; i++) {
                    let position = {
                        x: this.coin.worldTransform.tx + i * Util.getRandomInt(0, 6),
                        y: this.coin.worldTransform.ty - i * Util.getRandomInt(0, 6)
                    };
                    let map = Util.getMapsForBezier([
                        position.x, position.y,
                        position.x, position.y - 150,
                        position.x, position.y + 90,
                        position.x, position.y + 15
                    ]);
                    AnimationController.createAnimationMoveBezier(this.coin.texture, position, { x: 215, y: 28 }, map, i * 60);
                }
                this.coin.visible = false;
                PLAYER.friends.giftUsed.push(this.uid);

                Connection.addCoins(5);
            });
        };
        this.addChild(this.coin);
        //#endregion

        this.label = Components.Text('', {
            fontFamily: 'GothicB',
            fill: '#4F241B',
            fontSize: '11px',
            align: 'center'
        });
        this.label.y = this.button.y;
        this.addChild(this.label);
    }

    setInfo(info) {
        if (info === 'default') {
            this.interactive = true;
            this.photo.visible = false;
            this.button.visible = false;
            this.star.visible = false;
            this.label.visible = true;
            this.label.text = TRANSLATION.invite();
            this.background.setTexture(TEXTURES.panelFriends.photo_invite);
            this.coin.visible = false;
            this.web.visible = false;
            return;
        }

        this.background.setTexture(TEXTURES.panelFriends.photo_default);

        this.interactive = false;

        if (info.nid != null) { // друг в игре
            this.uid = info.nid;
            this.button.visible = false;
            this.label.visible = true;
            this.star.visible = true;
            this.label.text = info.firstName;
            this.star.level.text = info.level;

            if (info.web === 'true') {
                this.web.visible = true;
                this.coin.visible = false;
            } else {
                this.web.visible = false;
                this.coin.visible = PLAYER.friends.giftUsed.indexOf(info.nid) === -1;
            } if (info.photoUrl == null
                || info.photoUrl.indexOf('stub') !== -1
                || info.photoUrl.length === 0
                || info.photoUrl === 'foto') {
                this.photo.visible = false;
            } else {
                this.photo.visible = true;
                this.photo.setTexture(new PIXI.Texture.fromImage(info.photoUrl));
                this.photo.width=50;
                this.photo.height=50;

            }
        }

        if (info.uid != null) { // друг не в игре
            this.photo.setTexture(new PIXI.Texture.fromImage(info.pic50x50));
            this.uid = info.uid;
            this.button.visible = true;
            this.label.visible = false;
            this.star.visible = false;
            this.coin.visible = false;
            this.web.visible = false;
        }
    }

    mouseover() {
        this.filters = [CONFIG.filters.bright_11];
    }

    mouseout() {
        this.filters = [];
    }

    pointertap() {
        OK_methods.inviteAll(TRANSLATION['text_inviteAll']());
    }
}

class ButtonLevel extends PIXI.Sprite {
    constructor(index) {
        super();

        this.friendsOnLevel = [];

        this.number = 0;
        this.index = index;

        this.way_brown = Components.BitmapData(TEXTURES.mapScreen.way.brown[this.index]);
        this.way_brown.anchor.set(0);
        this.addChild(this.way_brown);

        this.way_blue = Components.BitmapData(TEXTURES.mapScreen.way.blue[this.index]);
        this.way_blue.anchor.set(0);
        this.addChild(this.way_blue);

        this.button = Components.Button(TEXTURES.mapScreen.btnLevel_1, TEXTURES.mapScreen.btnLevel_2);
        this.button.visible = false;
        this.button.pointertap = () => {
            Windows.LevelStart.updateInfo(CONFIG.json_configs.levels[this.number - 1]);
        };
        this.addChild(this.button);

        this.stars = Components.BitmapData(TEXTURES.mapScreen.star_0);
        this.stars.y = 14;
        this.button.addChild(this.stars);

        this.lock = Components.BitmapData(TEXTURES.mapScreen.btnLevel_close);
        this.addChild(this.lock);

        this.label = Components.Text('', CONFIG.fonts.white14);
        this.label.y = -20;
        this.addChild(this.label);
    }

    setData(level) {
        this.number = level;
        this.label.text = level;

        if (this.number <= PLAYER.level + 1) {
            this.button.visible = true;
            this.lock.visible = false;

            if (this.number >= PLAYER.progress.length + 1) {
                this.stars.visible = false;
                this.way_blue.visible = false;
            } else {
                this.way_blue.visible = true;
                this.setStars();
            }
        } else {
            this.button.visible = false;
            this.lock.visible = true;
            this.way_blue.visible = false;
        }

        this.way_brown.visible = !this.way_blue.visible;

        for (let i = 0; i < this.friendsOnLevel.length; i++)
            this.friendsOnLevel[i].friend.visible = this.friendsOnLevel[i].level + 1 === this.number;
    }

    setPhotoFriend(level, url, name, layer) {
        let index = level <  19
            ? level
            : level % 19;
        let friend = Components.BitmapData('');
        if (index === 5 || index === 10 || index === 17 || index === 18) {
            friend.setTexture(TEXTURES.mapScreen.photoFriend['3']);
        } else if (index === 11) {
            friend.setTexture(TEXTURES.mapScreen.photoFriend['4']);

        } else if (index >= 6 && index <= 9) {
            friend.setTexture(TEXTURES.mapScreen.photoFriend['1']);
        } else {
            friend.setTexture(TEXTURES.mapScreen.photoFriend['2']);
        }


        friend.interactive = true;
        friend.mouseover = () => {
            friend.filters = [CONFIG.filters.bright_109];
            friend_name.visible = true;
        };
        friend.mouseout = () => {
            friend.filters = [];
            friend_name.visible = false;
        };
        layer.addChild(friend);
        let photo = Components.BitmapData('');
        friend.addChild(photo);

        let mask = Components.BitmapData(TEXTURES.mapScreen.photoFriend.mask);
        photo.mask = mask;
        photo.addChild(mask);

        if (url == null || url.indexOf('stub') !== -1 || url.length === 0 || url === 'foto') {
            photo.setTexture(TEXTURES.mapScreen.photoFriend.photo_default);
            photo.height = 50;
            photo.width = 50;
        }
        else
            photo.setTexture(new PIXI.Texture.fromImage(url));

        if (this.index === 5 || this.index === 10) {
            friend.setTexture(TEXTURES.mapScreen['3']);
            friend.x = this.x - 44;
            friend.y = this.y - 32;
        } else if (this.index === 11) {
            friend.setTexture(TEXTURES.mapScreen['4']);
            friend.x = this.x + 44;
            friend.y = this.y - 32;
        } else if (this.index >= 6 && this.index <= 9) {
            friend.setTexture(TEXTURES.mapScreen['1']);
            friend.x = this.x - 52;
            friend.y = this.y - 8;
        } else {
            friend.setTexture(TEXTURES.mapScreen['2']);
            friend.x = this.x;
            friend.y = this.y - 62;
        }

        let friend_name = Components.Text(name, {
            fontFamily: 'GothicB',
            fill: '#4F241B',
            fontSize: '12px',
            align: 'center'
        });
        friend_name.y = -friend.height / 2 - 2;
        friend_name.visible = false;
        friend.addChild(friend_name);
        friend.visible = level + 1 === this.number;
        this.friendsOnLevel.push({ level: level, friend: friend });
    }

    setStars() {
        let scorePlayer = +PLAYER.progress[this.number - 1].result, scoreLevel = CONFIG.json_configs.levels[this.number - 1].scoreStars;

        if (scorePlayer >= +scoreLevel.star3)
            this.stars.setTexture(TEXTURES.mapScreen.star_3);
        else if (scorePlayer >= +scoreLevel.star2)
            this.stars.setTexture(TEXTURES.mapScreen.star_2);
        else if (scorePlayer >= +scoreLevel.star1)
            this.stars.setTexture(TEXTURES.mapScreen.star_1);
        else
            this.stars.setTexture(TEXTURES.mapScreen.star_0);
    }

    showWay() {
        let update = () => {
            if (this.way_blue.alpha + 0.1 < 1) {
                this.way_blue.alpha += 0.1;
                this.way_brown.alpha -= 0.1;
            } else {
                this.way_blue.alpha = 1;
                this.way_brown.visible = false;
                this.way_brown.alpha = 1;
                PixiApp.ticker.remove(update);
            }
        };

        this.way_blue.alpha = 0;
        this.way_blue.visible = true;
        PixiApp.ticker.add(update);
    }

    setPosition(x, y, { x_way = 0, y_way = 0 } = {}) {
        this.x = x;
        this.y = y;
        this.way_blue.x = x_way;
        this.way_blue.y = y_way;
        this.way_brown.x = x_way;
        this.way_brown.y = y_way;
    }
}

class InterestingPlace extends PIXI.Sprite {
    constructor(index, infoMap, layer, numberMap) {
        super();

        let infoPlace = infoMap[index],
            wTooltip = infoPlace.tooltip.width || 250,
            hTooltip = infoPlace.tooltip.width || 250,
            sMap = 'map_' + numberMap;

        this.x = infoPlace.x;
        this.y = infoPlace.y;

        let direction = index === 0
            ? Util.directions.LEFT
            : Util.directions.RIGHT;

        let image = Components.BitmapData(TEXTURES.mapScreen[sMap]['place_' + (index + 1)]);
        image.interactive = true;
        image.mouseover = () => {
            image.filters = [CONFIG.filters.bright_11, CONFIG.filters.glow_white];
            tooltip.show();
        };
        image.mouseout = () => {
            image.filters = [];
            tooltip.hide();
        };
        this.addChild(image);

        let tooltip = Components.Tooltip(direction, wTooltip, hTooltip, { offsetPointer: infoPlace.tooltip.offsetPointer });
        tooltip.y = this.y + infoPlace.tooltip.y;
        tooltip.x = direction === Util.directions.LEFT
            ? this.x + infoPlace.tooltip.x
            : this.x - tooltip._width + infoPlace.tooltip.x;

        let textName = Components.Text(TRANSLATION[sMap].places[index]().name, CONFIG.fonts.red14);
        textName.style.wordWrapWidth = tooltip._width;
        textName.y = 15;
        textName.x = tooltip._width / 2;
        textName.anchor.y = 0;
        tooltip.addChild(textName);

        tooltip.setText('— ' + TRANSLATION[sMap].places[index]().desc, {
            y: textName.y + textName.height + 6,
            anchorY: 0,
            style: CONFIG.fonts.brown12
        });
        tooltip.reset({ height: tooltip.text.height + textName.height + 40 });
        layer.addChild(tooltip);
    }
}

class PhotoOnMap extends PIXI.Sprite {
    constructor(levels) {
        super();

        this.levels = levels;
        this.offset = { x: -35, y: -35 };

        this.background = Components.BitmapData();
        this.addChild(this.background);

        this.photo = Components.BitmapData('');
        this.background.addChild(this.photo);
        let mask = Components.BitmapData(TEXTURES.mapScreen.photoPlayer.mask);
        this.photo.mask = mask;
        this.photo.addChild(mask);
    }

    moveTo(nextMap = false) {
        let index = PLAYER.level < this.levels.length
            ? PLAYER.level
            : PLAYER.level % this.levels.length;

        if (!nextMap && index === 0) {
            this.moveToNextMap();
            return;
        }

        this.setBackground(index);

        if (index - 1 >= 0)
            this.levels[index - 1].showWay();
        let endPosition = {
            x: (this.levels[index].x + this.offset.x),
            y: (this.levels[index].y + this.offset.y)
        };
        if (this.x === endPosition.x && this.y === endPosition.y) {
            this.interactiveChildren = true;
            return;
        }

        let speed = Util.getSpeed({ x: this.x, y: this.y }, endPosition, 12),
            tick = 0;
        let update = () => {
            if (tick < speed.tick) {
                this.y += speed.y;
                this.x += speed.x;
                tick++;
            } else {
                this.y = endPosition.y;
                this.x = endPosition.x;
                PixiApp.ticker.remove(update);
                if (Tutorial == null || !Tutorial.visible) {
                    this.levels[index].button.pointertap();
                    this.interactiveChildren = true;
                }
            }
        };

        PixiApp.ticker.add(update);
    }

    moveToNextMap() {
        if (MapScreen.numberMap + 1 > CONFIG.json_configs.info.countMaps)
            return;

        let endPosition = { x: CONFIG.global.sizeScreen.width + 30, y: this.y },
            speed = Util.getSpeed({ x: this.x, y: this.y }, endPosition, 12),
            tick = 0;

        let update = () => {
            if (tick < speed.tick) {
                this.x += speed.x;
                tick++;
            } else {
                this.x = -20;
                this.y = 384;
                PixiApp.ticker.remove(update);
                MapScreen.setMap(MapScreen.numberMap + 1)
                    .then(() => this.moveTo(true));
            }
        };

        PixiApp.ticker.add(update);
    }

    setPhoto() {
        if (PLAYER.photoUrl == null
            || PLAYER.photoUrl.indexOf('stub') !== -1
            || PLAYER.photoUrl.length === 0
            || PLAYER.photoUrl === 'foto')
            this.photo.setTexture(TEXTURES.mapScreen.photoPlayer.photo_default);
        else
            {
            this.photo.setTexture(new PIXI.Texture.fromImage(PLAYER.photoUrl));

            this.photo.height = 50;
                this.photo.width = 50;
                console.log('foro = ' + PLAYER.photoUrl+' размер ' + this.photo.width + ' '+this.photo.height );
            }
    }

    setBackground(index) {
        if (index === 5 || index === 10 || index === 17 || index === 18) {
            this.background.setTexture(TEXTURES.mapScreen.photoPlayer['3']);
            this.offset.x = -44;
            this.offset.y = -32;
        } else if (index === 11) {
            this.background.setTexture(TEXTURES.mapScreen.photoPlayer['4']);
            this.offset.x = 44;
            this.offset.y = -32;
        } else if (index >= 6 && index <= 9) {
            this.background.setTexture(TEXTURES.mapScreen.photoPlayer['1']);
            this.offset.x = -52;
            this.offset.y = -8;
        } else {
            this.background.setTexture(TEXTURES.mapScreen.photoPlayer['2']);
            this.offset.x = 0;
            this.offset.y = -62;
        }
    }

    setPosition() {
        let index = PLAYER.level < this.levels.length
            ? PLAYER.level
            : PLAYER.level % this.levels.length;

        this.setBackground(index);

        this.x = this.levels[index].x + this.offset.x;
        this.y = this.levels[index].y + this.offset.y;
    }

    checkPosition(numberMap) {
        this.visible = numberMap * this.levels.length > PLAYER.level;
    }
}

