using System.Collections ;

using System.Collections.Generic ;

using UnityEngine ;

using UnityEngine.UI ;

using UnityEngine.Events ;

public class PositionManager : MonoBehaviour

{

[ Header ( "Jacket and TopSite gameobjects" ) ]

public GameObject TopSiteTopLeft ;

public GameObject TopSiteTopRight ;

public GameObject TopSiteLowLeft, TopSiteLowRight, JacketTopLeft, JacketTopRight, JacketLowLeft, JacketLowRight ;

public GameObject TopSiteHolder ;

public GameObject TopSiteSideView, JacketSideView ;

public GameObject SlopeMarker ;

public GameObject SlopeReference ;

private Vector3 SlopeMarkerOriginal ;

public Text SideViewText ;

[ Header ( "Real-life Jacket and TopSite data - Informational only" ) ]

public Vector3 TopSitePosition ;

public Vector3 JacketPosition ;

public int TopSiteRotation ;

public int JacketRotation = 0 ;

public Vector2 TopSiteSlope ;

public Vector2 JacketSlope ;

[ Header ( "Thresholds" ) ]

[ Tooltip ( "If all data falls within the thresholds, the radars turn green" ) ]

public float XLowerRangeThreshold ;

public float XUpperRangeThreshold, YLowerRangeThreshold, YUpperRangeThreshold, ZLowerRangeThreshold, ZUpperRangeThreshold ;

public float rotationLowerRangeThreshold, rotationUpperRangeThreshold ;

[ Header ( "Boundaries" ) ]

[ Tooltip ( "Boundary ranges are used to set outer ranges for the radarpoints" ) ]

public float LowerRangeTwoBlockRadar ;

public float UpperRangeTwoBlockRadar ;

public float LowerRangeFourBlockRadarX, LowerRangeFourBlockRadarY ;

public float UpperRangeFourBlockRadarX, UpperRangeFourBlockRadarY ;

public float LeftRangeSlope, RightRangeSlope, TopRangeslope, BottomRangeSlope ;

public float offSetTwoBlockRadar = 0 . 5f ;

private GameObject [ ] topSiteArray ;

private GameObject [ ] jacketArray ;

private bool withinThreshold = false ;

Vector3 startPos, endPos ;

void Start ( )

{

//4-block radar

topSiteArray = new GameObject [ 4 ] ;

jacketArray = new GameObject [ 4 ] ;

topSiteArray [ 0 ] = TopSiteTopLeft ;

topSiteArray [ 1 ] = TopSiteTopRight ;

topSiteArray [ 2 ] = TopSiteLowLeft ;

topSiteArray [ 3 ] = TopSiteLowRight ;

jacketArray [ 0 ] = JacketTopLeft ;

jacketArray [ 1 ] = JacketTopRight ;

jacketArray [ 2 ] = JacketLowLeft ;

jacketArray [ 3 ] = JacketLowRight ;

SlopeMarkerOriginal = new Vector3 ( SlopeMarker . transform . position . x , SlopeMarker . transform . position . y , SlopeMarker . transform . position . z ) ;

}

void Update ( )

{

TwoBlockRadar ( ) ;

FourBlockRadar ( ) ;

SlopeRadar ( ) ;

}

private void TwoBlockRadar ( )

{

if ( TopSitePosition != new Vector3 ( 0 , 0 , 0 ) )

{

//Sets the Y position TO its boundaries when it exceeded the boundaries.

if ( TopSitePosition . z < LowerRangeTwoBlockRadar )

{

TopSitePosition . z = LowerRangeTwoBlockRadar ;

}

if ( TopSitePosition . z > UpperRangeTwoBlockRadar )

{

TopSitePosition . z = UpperRangeTwoBlockRadar ;

}

SideViewText . text = TopSitePosition . z . ToString ( ) + "M" ;

//Smooth transitioning to new location

startPos = TopSiteSideView . transform . localPosition ;

endPos = new Vector3 ( TopSiteSideView . transform . localPosition . x , ( ( TopSitePosition . z / 100 ) - offSetTwoBlockRadar ) , 0 ) ;

float lerpTime = 1 ;

float currentLerpTime = 0 ;

currentLerpTime += Time . deltaTime ;

if ( currentLerpTime >= lerpTime )

{

currentLerpTime = lerpTime ;

}

float perc = currentLerpTime / lerpTime ;

TopSiteSideView . transform . localPosition = Vector3 . Lerp ( startPos, endPos, perc ) ;

}

float ZDiff = TopSitePosition . z - JacketPosition . z ;

checkTwoRadarThreshold ( ZDiff ) ;

//Checks if the blocks should be colored green.

if ( withinThreshold )

{

TopSiteSideView . GetComponent < Renderer > ( ) . material . color = Color . green ;

JacketSideView . GetComponent < Renderer > ( ) . material . color = Color . green ;

}

else

{

TopSiteSideView . GetComponent < Renderer > ( ) . material . color = Color . red ;

JacketSideView . GetComponent < Renderer > ( ) . material . color = Color . red ;

}

}

private void FourBlockRadar ( )

{

if ( TopSitePosition != new Vector3 ( 0 , 0 , 0 ) )

{

//Sets any X or Y position TO its boundaries when it exceeded the boundaries.

if ( TopSitePosition . x < LowerRangeFourBlockRadarX )

{

TopSitePosition . x = LowerRangeFourBlockRadarX ;

}

if ( TopSitePosition . x > UpperRangeFourBlockRadarX )

{

TopSitePosition . x = UpperRangeFourBlockRadarX ;

}

if ( TopSitePosition . y < LowerRangeFourBlockRadarY )

{

TopSitePosition . y = LowerRangeFourBlockRadarY ;

}

if ( TopSitePosition . y > UpperRangeFourBlockRadarX )

{

TopSitePosition . y = UpperRangeFourBlockRadarX ;

}

//Smooth transitioning to new location

startPos = TopSiteHolder . transform . localPosition ;

endPos = new Vector3 ( ( ( TopSitePosition . x / 100 ) ) , - ( ( TopSitePosition . y / 100 ) ) , TopSiteHolder . transform . localPosition . z ) ;

float lerpTime = 1 ;

float currentLerpTime = 0 ;

currentLerpTime += Time . deltaTime ;

if ( currentLerpTime >= lerpTime )

{

currentLerpTime = lerpTime ;

}

float perc = currentLerpTime / lerpTime ;

TopSiteHolder . transform . localPosition = Vector3 . Lerp ( startPos, endPos, perc ) ;

TopSiteHolder . transform . rotation = Quaternion . Lerp ( TopSiteHolder . transform . rotation , ( Quaternion . AngleAxis ( TopSiteRotation, Vector3 . back ) ) , perc ) ;

}

float XDiff = TopSitePosition . x - JacketPosition . x ;

float YDiff = TopSitePosition . y - JacketPosition . y ;

float RotationDiff = TopSiteRotation - JacketRotation ;

//Checks if the Topsite is equal enough to the Jacket to see if it's a hit (withinthreshold = true means hit).

checkFourRadarThreshold ( XDiff, YDiff, RotationDiff ) ;

//Color blocks green if they're on the same position, else color them red

if ( withinThreshold )

{

for ( int i = 0 ; i < topSiteArray . Length ; i ++ )

{

topSiteArray [ i ] . GetComponent < Renderer > ( ) . material . color = Color . green ;

jacketArray [ i ] . GetComponent < Renderer > ( ) . material . color = Color . green ;

}

}

else

{

for ( int i = 0 ; i < topSiteArray . Length ; i ++ )

{

topSiteArray [ i ] . GetComponent < Renderer > ( ) . material . color = Color . red ;

jacketArray [ i ] . GetComponent < Renderer > ( ) . material . color = Color . red ;

}

}

}

private void SlopeRadar ( )

{

//Sets position of the marker according to the relative position of TopSiteSlipe against JacketSlope. Normalized by 100 for testing purposes - depends on what values are sent through.

Debug . Log ( "Xtopsite:" + TopSiteSlope . x + " Ytopsite: " + TopSiteSlope . y + " XJacket: " + JacketSlope . x + " YJacket:" + JacketSlope . y ) ;

float SlopeOffsetX = TopSiteSlope . x - JacketSlope . x ;

float SlopeOffsetY = TopSiteSlope . y - JacketSlope . y ;

if ( SlopeOffsetX < LeftRangeSlope )

{

SlopeOffsetX = LeftRangeSlope ;

}

if ( SlopeOffsetX > RightRangeSlope )

{

SlopeOffsetX = RightRangeSlope ;

}

if ( SlopeOffsetY < BottomRangeSlope )

{

SlopeOffsetY = BottomRangeSlope ;

}

if ( SlopeOffsetY > TopRangeslope )

{

SlopeOffsetY = TopRangeslope ;

}

float SlopeMarkerX = SlopeMarkerOriginal . x - ( SlopeOffsetX ) / 100 ;

float SlopeMarkerY = SlopeMarkerOriginal . y - ( SlopeOffsetY ) / 100 ;

//Debug.Log(SlopeMarkerX + " " + SlopeMarkerY);

//SlopeMarker.transform.position = new Vector3(SlopeMarkerX, SlopeMarkerY, SlopeMarkerOriginal.z);

//Smooth transitioning to new location

startPos = SlopeMarker . transform . position ;

endPos = new Vector3 ( SlopeMarkerX, SlopeMarkerY, SlopeMarkerOriginal . z ) ;

float lerpTime = 1 ;

float currentLerpTime = 0 ;

currentLerpTime += Time . deltaTime ;

if ( currentLerpTime >= lerpTime )

{

currentLerpTime = lerpTime ;

}

float perc = currentLerpTime / lerpTime ;

SlopeMarker . transform . position = Vector3 . Lerp ( startPos, endPos, perc ) ;

//Color of blocks

if ( TopSiteSlope . x - JacketSlope . x < 0.15 && TopSiteSlope . x - JacketSlope . x > - 0.15 && TopSiteSlope . y - JacketSlope . y < 0.15 && TopSiteSlope . y - JacketSlope . y > - 0.15 )

{

SlopeMarker . GetComponent < Renderer > ( ) . material . color = Color . green ;

}

else

{

SlopeMarker . GetComponent < Renderer > ( ) . material . color = Color . red ;

}

}

private bool checkTwoRadarThreshold ( float z )

{

if ( z > ZLowerRangeThreshold && z < ZUpperRangeThreshold )

{

withinThreshold = true ;

}

else

{

withinThreshold = false ;

}

return withinThreshold ;

}

private bool checkFourRadarThreshold ( float x, float y, float rotation )

{

if ( x > XLowerRangeThreshold && x < XUpperRangeThreshold && y > YLowerRangeThreshold && y < YUpperRangeThreshold && rotation > rotationLowerRangeThreshold && rotation < rotationUpperRangeThreshold )

{

withinThreshold = true ;

}

else

{

withinThreshold = false ;

}

return withinThreshold ;

}

public void getTaddeData ( CamData data )

{

TopSiteSlope = data . Slope ;

}

public void getJaceData ( CamData data )

{

TopSitePosition = new Vector3 ( ( ( data . Cam1 . x + data . Cam2 . x ) / 2 ) , ( ( data . Cam1 . y + data . Cam2 . y ) / 2 ) , ( data . Cam1 . z + data . Cam2 . z ) / 2 ) ;

TopSiteRotation = data . Rotation ;

JacketSlope = data . Slope ;

}