/**
 * CLASS body
 */

var shapedAsRectangle=true;

function body(left,top,length,height,weight,maxWeight,Fext,maxFext,N,T,cntx,rectangleShape,sizeable,lockedX,lockedY){
    this.cntx=cntx;
    this.left=left;
    this.top=top;
    this.length=length;
    this.height=height;
    this.weight=weight;
    this.maxWeight=maxWeight;
    this.Fext=Fext;
    this.maxFext=maxFext;
    this.N=N; //δύναμη στήριξης
    this.T=T; //τριβή
    this.showLength=false;
    this.bodyRed=0;
    this.bodyGreen=0;
    this.bodyBlue=0;
    this.blackBorder=false;
    this.transparencyPercent=0.3;
    this.visible=false;
    this.grabbed=false;
    //this.orientation=0;
    this.showOrientation=false;
    this.orientationPercent=0.2;
    this.drawWidth=1;
    this.sizeable=sizeable;
    this.rotateable=false;
    this.lockedX=lockedX;
    this.lockedY=lockedY;
    this.triangleSupport=false;
    this.rectangleShape=rectangleShape;
    this.supporting=false;
    this.szHover=false;
    this.mvHover=false;
    this.rotHover=false;
    this.showValue=false;

    var orientation=0;
    
    this.rotate=function(X1,Y1){
        var X2,Y2;
        X2=X1*Math.cos(orientation)-Y1*Math.sin(orientation);
        Y2=X1*Math.sin(orientation)+Y1*Math.cos(orientation);
        return{
            x2 : X2,
            y2 : Y2
        };
    };

    this.initialiseWeight=function(){
        var CM=this.rotate(this.length/2,0);
        var newVectorW=new vector(this.left+CM.x2,this.top+CM.y2,this.weight,this.maxWeight,this.cntx,vctrSizeable,!vctrRotatable,coordLocked,coordLocked,"N");
        newVectorW.vecRed=wRed;
        newVectorW.vecGreen=wGreen;
        newVectorW.vecBlue=wBlue;
        newVectorW.visible=true;
        newVectorW.showValue=true;
        newVectorW.showOrientation=false;
        newVectorW.orientationPercent=0.25;
        newVectorW.setOrientation(3*Math.PI/2);
        newVectorW.drawWidth=vectorDrawWidth;
        newVectorW.showSelectedComponent=false;
        newVectorW.selectedComponentOrientation=0;
        newVectorW.linked=true;
        return newVectorW;
    };
    this.weightVctr=this.initialiseWeight();

    this.initialiseN=function(){
        var Ntop=this.top;
        if(this.triangleSupport){
            Ntop-=this.height/2;
        }
        var newVectorN=new vector(this.left+this.length/2,Ntop,this.N,1000,this.cntx,!vctrSizeable,!vctrRotatable,coordLocked,coordLocked,"N");
        newVectorN.vecRed=NRed;
        newVectorN.vecGreen=NGreen;
        newVectorN.vecBlue=NBlue;
        newVectorN.visible=true;
        newVectorN.showValue=true;
        newVectorN.showOrientation=false;
        newVectorN.orientationPercent=0.25;
        newVectorN.setOrientation(Math.PI/2);
        newVectorN.drawWidth=vectorDrawWidth;
        newVectorN.linked=true;
        return newVectorN;
    };
    this.N=this.initialiseN();
    
    this.initialiseFext=function(){
        var CM=this.rotate(this.length/2,0);
        var newVectorFext=new vector(this.left+CM.x2,this.top+CM.y2,this.Fext,this.maxFext,this.cntx,vctrSizeable,vctrRotatable,coordLocked,coordLocked,"N");
        newVectorFext.vecRed=FRed;
        newVectorFext.vecGreen=FGreen;
        newVectorFext.vecBlue=FBlue;
        newVectorFext.showXComponent=true;
        newVectorFext.showYComponent=true;
        newVectorFext.visible=true;
        newVectorFext.showValue=true;
        newVectorFext.showOrientation=false;
        newVectorFext.orientationPercent=0.25;
        newVectorFext.setOrientation(Math.PI/3);
        newVectorFext.drawWidth=vectorDrawWidth;
        newVectorFext.linked=true;
        return newVectorFext;
    };
    this.FextVctr=this.initialiseFext();
    
    this.initialiseT=function(){
        var CM=this.rotate(this.length/2,0);
        var newVectorT=new vector(this.left+CM.x2,this.top+CM.y2,this.T,1000,this.cntx,!vctrSizeable,!vctrRotatable,coordLocked,coordLocked,"N");
        newVectorT.vecRed=TRed;
        newVectorT.vecGreen=TGreen;
        newVectorT.vecBlue=TBlue;
        newVectorT.visible=true;
        newVectorT.showValue=true;
        newVectorT.showOrientation=false;
        newVectorT.orientationPercent=0.25;
        newVectorT.setOrientation(Math.PI);
        newVectorT.drawWidth=vectorDrawWidth;
        newVectorT.linked=true;
        return newVectorT;
    };
    this.TVctr=this.initialiseT();

    this.setOrientation=function(newOrientation){
        orientation=2*Math.PI-newOrientation;
        var CM=this.rotate(this.length/2,0);
        this.weightVctr.left=this.left+CM.x2;
        this.weightVctr.top=this.top+CM.y2;
    };

    this.getOrientation=function(){
        return orientation;
    };

    this.setLength=function(newLength){
        this.length=newLength;
        var CM=this.rotate(this.length/2,0);
        this.weightVctr.left=this.left+CM.x2;
        this.weightVctr.top=this.top+CM.y2;
    };

    this.getBodyEnd=function(){
        var x2=0;
        var y2=0;
        if(this.triangleSupport || this.rectangleShape){//Για σώμα τριγωνικού / παραλληλόγραμμου σχήματος
            var bodyEnd=this.rotate(this.length,this.height/2);
            x2=this.left+bodyEnd.x2;
            y2=this.top+bodyEnd.y2;
        }
        else{//Για σώμα κυκλικού σχήματος επιστρέφουμε το κέντρο του σχήματος
            x2=this.left+this.length/2;
            y2=this.top;
        }
        return {
            endX: x2,
            endY: y2
        };
    };
    
    this.movable=function(){
        return !(this.lockedX && this.lockedY);  
    };

    this.move=function(newLeft,newTop,forceMovement){
        if(forceMovement || this.movable){
            if(!this.lockedX || forceMovement){
                this.left=newLeft;
            }
            if(!this.lockedY || forceMovement){
                this.top=newTop;
            }
            this.weightVctr.move(this.left+this.length/2,this.top,forceMovement);
            this.FextVctr.move(this.left+this.length/2,this.top,forceMovement);
            this.N.move(this.left+this.length/2,this.top,forceMovement);
            this.TVctr.move(this.left+this.length/2,this.top,forceMovement);
        }
    };

    this.render=function(){
        var X1,Y1,X2,Y2,X3,Y3,X4,Y4,xRight,yMiddle;
        if (this.visible){
            this.cntx.lineWidth=1;
            var topLeft=this.rotate(0,-this.height/2);
            X1=topLeft.x2;
            Y1=topLeft.y2;
            var endPos=this.getBodyEnd();
            xRight=endPos.endX;
            yMiddle=endPos.endY;
            var topRight=this.rotate(this.length,-this.height/2);
            X2=topRight.x2;
            Y2=topRight.y2;
            var bottomRight=this.rotate(this.length,this.height/2);
            X3=bottomRight.x2;
            Y3=bottomRight.y2;
            var bottomLeft=this.rotate(0,this.height/2);
            X4=bottomLeft.x2;
            Y4=bottomLeft.y2;
            //ΣΧΕΔΙΑΣΗ
            //1.Σχεδίαση σώματος
            if(this.triangleSupport){
                this.cntx.lineWidth=this.drawWidth;
                this.cntx.setLineDash([]);
                var transparencyPrct=this.transparencyPercent;
                if(this.blackBorder){
                    this.cntx.strokeStyle="rgb(0,0,0)";
                    transparencyPrct=1;
                }
                else{
                    this.cntx.strokeStyle="rgb("+this.bodyRed+","+this.bodyGreen+","+this.bodyBlue+")";
                }
                this.cntx.fillStyle="rgba("+this.bodyRed+","+this.bodyGreen+","+this.bodyBlue+","+transparencyPrct+")";
                this.cntx.beginPath();
                this.cntx.moveTo(this.left+this.length/2,this.top-this.height/2);
                this.cntx.lineTo(this.left+this.length,this.top+this.height/2);
                this.cntx.lineTo(this.left,this.top+this.height/2);
                this.cntx.lineTo(this.left+this.length/2,this.top-this.height/2);
                this.cntx.fill();
                this.cntx.stroke();
                this.cntx.lineWidth=1;
            }
            else if(this.rectangleShape){
                this.cntx.lineWidth=this.drawWidth;
                this.cntx.setLineDash([]);
                var transparencyPrct=this.transparencyPercent;
                if(this.blackBorder){
                    this.cntx.strokeStyle="rgb(0,0,0)";
                    transparencyPrct=1;
                }
                else{
                    this.cntx.strokeStyle="rgb("+this.bodyRed+","+this.bodyGreen+","+this.bodyBlue+")";
                }
                this.cntx.fillStyle="rgba("+this.bodyRed+","+this.bodyGreen+","+this.bodyBlue+","+transparencyPrct+")";
                this.cntx.beginPath();
                this.cntx.moveTo(this.left+X1,this.top+Y1);
                this.cntx.lineTo(this.left+X2,this.top+Y2);
                this.cntx.lineTo(this.left+X3,this.top+Y3);
                this.cntx.lineTo(this.left+X4,this.top+Y4);
                this.cntx.lineTo(this.left+X1,this.top+Y1);
                this.cntx.fill();
                this.cntx.stroke();
                this.cntx.lineWidth=1;
            }
            else{ //κυκλικό σχήμα
                this.cntx.lineWidth=this.drawWidth;
                this.cntx.setLineDash([]);
                var transparencyPrct=this.transparencyPercent;
                if(this.blackBorder){
                    this.cntx.strokeStyle="rgb(0,0,0)";
                    transparencyPrct=1;
                }
                else{
                    this.cntx.strokeStyle="rgb("+this.bodyRed+","+this.bodyGreen+","+this.bodyBlue+")";
                }
                this.cntx.fillStyle="rgba("+this.bodyRed+","+this.bodyGreen+","+this.bodyBlue+","+transparencyPrct+")";
                this.cntx.beginPath();
                this.cntx.arc(this.left+this.length/2,this.top,this.height/2,0,2*Math.PI);
                this.cntx.fill();
                this.cntx.stroke();
                this.cntx.lineWidth=1;
            }
            //2. Εμφάνιση μήκους
            this.cntx.strokeStyle="rgb("+this.bodyRed+","+this.bodyGreen+","+this.bodyBlue+")";
            if(this.showValue){
                var valX=this.length+10,valY=-10;
                var newPos=this.rotate(valX,valY);
                this.cntx.fillText(parseInt(100*this.length)/100,this.left+newPos.x2,this.top+newPos.y2);
            }
            //3. Εμφάνιση κατεύθυνσης
            if(this.showOrientation){
                this.cntx.strokeStyle="rgb("+this.bodyRed+","+this.bodyGreen+","+this.bodyBlue+")";
                this.cntx.fillStyle="rgba("+this.bodyRed+","+this.bodyGreen+","+this.bodyBlue+","+this.transparencyPercent+")";
                this.cntx.beginPath();              
                this.cntx.moveTo(this.left,this.top);//Η μετακίνηση χρειάζεται για το ορθό γέμισμα
                var orientationRadius=this.orientationPercent*this.length;
                orientationRadius=10;
                this.cntx.arc(this.left,this.top,orientationRadius,0,orientation,true);
                this.cntx.fill();
                this.cntx.stroke();
                this.cntx.fillStyle="rgb(0,0,0)";
                this.cntx.fillText(Math.round((2-orientation/Math.PI)*180)+"◦",this.left+10,this.top-5);
            }
            //4. Εμφάνιση χειριστηρίου περιστροφής
            if(this.rotateable){
                var rotRed=255;
                var rotGreen=255;
                var rotBlue=255;
                var handleRadius=3;
                if(this.rotHover){
                    rotRed=0;
                    rotGreen=255;
                    rotBlue=255;
                    handleRadius=5;
                }
                this.cntx.fillStyle="rgba("+rotRed+","+rotGreen+","+rotBlue+",0.8)";
                this.cntx.beginPath();
                if(this.triangleSupport){
                    //να ελεγχθεί αν έχει νόημα και να συμπληρωθεί καταλλήλως
                }
                else{
                    this.cntx.arc(this.left,this.top,handleRadius,0,2*Math.PI,true);
                }
                this.cntx.fill();
                this.cntx.stroke();
            }
            //5. Εμφάνιση χειριστηρίου μετακίνησης
            if(this.movable){
                var szRed=255;
                var szGreen=255;
                var szBlue=255;
                var handleRadius=4;
                if(this.mvHover){
                    szRed=0;
                    szGreen=255;
                    szBlue=0;
                    handleRadius=6;
                }
                this.cntx.fillStyle="rgba("+szRed+","+szGreen+","+szBlue+",0.8)";
                this.cntx.beginPath();
                if(this.triangleSupport){
                    this.cntx.arc(this.left+this.length/2,this.top,handleRadius,0,2*Math.PI,true);
                }
                else{
                    this.cntx.arc(this.weightVctr.left,this.weightVctr.top,handleRadius,0,2*Math.PI,true);
                }
                this.cntx.fill();
                this.cntx.stroke();
            }
            //6. Εμφάνιση χειριστηρίου τροποποίησης μεγέθους
            if(this.sizeable){
                var szRed=255;
                var szGreen=255;
                var szBlue=255;
                var handleRadius=3;
                if(this.szHover){
                    szRed=255;
                    szGreen=255;
                    szBlue=0;
                    handleRadius=5;
                }
                this.cntx.fillStyle="rgba("+szRed+","+szGreen+","+szBlue+",0.8)";
                this.cntx.beginPath();              
                if(this.triangleSupport){
                    this.cntx.arc(this.left+this.length,this.top+this.height,handleRadius,0,2*Math.PI,true);
                }
                else{
                    this.cntx.arc(xRight,yMiddle,handleRadius,0,2*Math.PI,true);
                }
                this.cntx.fill();
                this.cntx.stroke();
            }
            //7. Εμφάνιση διανυσμάτων
            this.TVctr.render();
            this.N.render();
            this.FextVctr.render();
            this.weightVctr.render();
        }
    };
}