import {
    MySprite, tweenChange,
    ShapeRect,
    Label
} from './Unit';

import {
    playAudio
} from './AudioUtil';

export class Player {
    /**背景 */
    bg;

    playerPic;

    /**汗水 */
    sweatPic;

    //图片数组
    images = new Map();

    scaleX = 1;

    scaleY = 1;

    initX = 0;

    initY = 0;

    sweatAudio;

    pullAudio;

    car: ShapeRect;

    cargos = [];

    constructor(images) {
        this.images = images;
    }

    setScaleXY(scale) {
        this.scaleX = this.scaleY = scale;
    }

    init() {
        const playerPic = new MySprite();
        playerPic.init(this.images.get('player'));
        playerPic.scaleX = this.scaleX;
        playerPic.scaleY = this.scaleY;
        playerPic.x = playerPic.getBoundingBox().width / 2;
        playerPic.y = playerPic.getBoundingBox().height / 2;
        this.playerPic = playerPic;
        

        let width = playerPic.getBoundingBox().width;
        let height = playerPic.getBoundingBox().height;

        const bg = new ShapeRect();
        bg.setSize(width, height);
        bg.alpha = 0;
        this.bg = bg;

        const car = new ShapeRect();
        car.setSize(160 * this.scaleX, 10 * this.scaleY);
        car.x = 20 * this.scaleX;
        car.y = 90 * this.scaleY;
        car.alpha = 0;
        this.car = car;
        bg.addChild(car);

        bg.addChild(playerPic);

        const sweatPic = new MySprite();
        sweatPic.init(this.images.get('sweat'));
        sweatPic.scaleX = this.scaleX;
        sweatPic.scaleY = this.scaleY;
        sweatPic.x = 300 * this.scaleX + sweatPic.getBoundingBox().width / 2;
        sweatPic.y = 20 * this.scaleY + sweatPic.getBoundingBox().height / 2;
        sweatPic.alpha = 0;
        this.sweatPic = sweatPic;
        bg.addChild(sweatPic);
    }

    pullCar(toX: number, toY: number, callback = null, time = 0.3, ) {

        playAudio(this.pullAudio, true);

        tweenChange(this.bg, { x: toX, y: toY }, time, () => {
            if (callback) {
                callback();
            }
        })
    }

    reset() {
        this.bg.x = this.initX;

        this.bg.y = this.initY;
    }

    flowSweat(callback = null) {

        playAudio(this.sweatAudio, true);

        this.sweatPic.alpha = 1;

        let y = this.sweatPic.y;

        tweenChange(this.sweatPic, { y: y + 30 * this.scaleY }, 0.3, () => {

            this.sweatPic.alpha = 0;

            this.sweatPic.y = y;

            if (callback) {
                callback();
            }
        });
    }

    getCargoY = (x, b) => {
        //斜率
        let k = 0.195;

        let c = - b * this.scaleY;

        let y = k * x + c;

        return -y;

    }

    loading(cargo) {
        if (!cargo) {
            return;
        }

        let x = 10 * this.scaleX;

        let length = this.cargos.length;

        if ( length > 0) {

            if(length % 4 != 0){
                let lastCargo = this.cargos[length - 1];

                x = lastCargo.x;
    
                x += lastCargo.getBoundingBox().width;
            }
            else{
                x = 10 * this.scaleX + Math.floor(length / 4) * 5 * this.scaleX;
            }
            
        }

        let startY = Math.floor(length / 4) * 20 * this.scaleY;

        let c = Math.random() * this.car.height;

        let y = this.getCargoY(x, c) - startY; 
        cargo.x = x - cargo.getBoundingBox().width / 2;
        cargo.y = y - cargo.getBoundingBox().height / 2;

        this.cargos.push(cargo);

        this.car.addChild(cargo, -length);
    }

    unloading() {
        this.car.removeChildren();
        this.cargos = [];
    }
}