import { EventEmitter } from '@angular/core';
import { RotationDirectionType } from './rotation-direction-type';
import { RotationObserver } from './rotation-observer';
export class RotationBehavior {
    constructor(rotationModel, variableCommunicator, variableStore, signalRAppId) {
        this.doRotation = new EventEmitter();
        const rotationObserver = new RotationObserver(rotationModel, variableCommunicator, variableStore, signalRAppId);
        rotationObserver.rotationValueChanged.subscribe(rotationValue => {
            if (+rotationModel.rotationDirection === RotationDirectionType.ClockWise) {
                let newRotation;
                if (rotationModel.isProportion === true) {
                    const proportion = (rotationModel.maxAngle - rotationModel.minAngle)
                        / (rotationModel.maxProportionScale - rotationModel.minProportionScale);
                    if (rotationModel.isReverseProportion === true) {
                        if (rotationValue > rotationModel.maxProportionScale) {
                            newRotation = rotationModel.minAngle;
                        }
                        else if (rotationValue < rotationModel.minProportionScale) {
                            newRotation = rotationModel.maxAngle;
                        }
                        else {
                            newRotation = rotationModel.maxAngle - (rotationValue - rotationModel.minProportionScale) * proportion;
                        }
                    }
                    else {
                        if (rotationValue > rotationModel.maxProportionScale) {
                            newRotation = rotationModel.maxAngle;
                        }
                        else if (rotationValue < rotationModel.minProportionScale) {
                            newRotation = rotationModel.minAngle;
                        }
                        else {
                            newRotation = rotationModel.minAngle + (rotationValue - rotationModel.minProportionScale) * proportion;
                        }
                    }
                    this.doRotation.emit(newRotation);
                }
                else {
                    if (rotationValue < rotationModel.minAngle) {
                        this.doRotation.emit(rotationModel.minAngle);
                    }
                    else if (rotationValue > rotationModel.maxAngle) {
                        this.doRotation.emit(rotationModel.maxAngle);
                    }
                    else {
                        this.doRotation.emit(rotationValue);
                    }
                }
            }
            else if (+rotationModel.rotationDirection === RotationDirectionType.ReverseClockWise) {
                let newRotation;
                const nowMin = 360 - rotationModel.maxAngle;
                const nowMax = 360 - rotationModel.minAngle;
                if (rotationModel.isProportion === true) {
                    const proportion = (nowMax - nowMin) / (rotationModel.maxProportionScale - rotationModel.minProportionScale);
                    if (rotationModel.isReverseProportion === true) {
                        if (rotationValue > rotationModel.maxProportionScale) {
                            newRotation = nowMax;
                        }
                        else if (rotationValue < rotationModel.minProportionScale) {
                            newRotation = nowMin;
                        }
                        else {
                            newRotation = nowMin + (rotationValue - rotationModel.minProportionScale) * proportion;
                        }
                    }
                    else {
                        if (rotationValue > rotationModel.maxProportionScale) {
                            newRotation = nowMin;
                        }
                        else if (rotationValue < rotationModel.minProportionScale) {
                            newRotation = nowMax;
                        }
                        else {
                            newRotation = nowMax - (rotationValue - rotationModel.minProportionScale) * proportion;
                        }
                        if (newRotation > nowMax) {
                            newRotation = nowMax;
                        }
                        else if (newRotation < nowMin) {
                            newRotation = nowMin;
                        }
                    }
                    this.doRotation.emit(newRotation);
                }
                else {
                    newRotation = 360 - rotationValue;
                    if (newRotation < nowMin) {
                        this.doRotation.emit(nowMin);
                    }
                    else if (newRotation > nowMax) {
                        this.doRotation.emit(nowMax);
                    }
                    else {
                        this.doRotation.emit(newRotation);
                    }
                }
            }
        });
    }
}
