"""
Pattern Presentation
Object Oriented Programming
Pattern: Mediator
Name: Claire Herdeman
Date: February 4, 2019

This document contains a simple "ground control" simulation to
model the Mediator OO design pattern.
"""


class GroundControlMediator:
	"""
	The mediator class in this example represents ground control.
	Registers both colleagues (instances of the Flight and  Runway classes)
	and manages communication around ability of the flight to land on
	the runway.
	"""
	def __init__(self):
		self._flight = Flight(self)
		self._runway = Runway(self)
		self.land = False

	def registerRunway(self, runway):
		self._runway = runway
		print("Ground Control registers new runway.")

	def registerFlight(self, flight):
		self._flight = flight
		print("Ground Control registers new flight.")

	def isLandingOk(self):
		return self.land

	def setLandingStatus(self, status):
		self.land = status

class AbstractAir:
	"""
	Abstract inheritence class
	"""
	def __init__(self, mediator):
		self._mediator = mediator

	# Abstract land method
	def land(self):
		pass

class Flight(AbstractAir):
	"""
	Subclass of AbstractAir represents a flight preparing for landing.
	"""
	def land(self):
		if self._mediator.isLandingOk():
			print("Flight landed successfully")
			self._mediator.setLandingStatus(False)
		else:
			print("Flight waiting for permission to land.")

	def getReady(self):
		print("Flight preparing for landing.")
	

class Runway(AbstractAir):
	"""
	Subclass of AbstractAir represents a runway.
	"""
	def land(self):
		print("Runway grants permission to land.")
		self._mediator.setLandingStatus(True)


def main():
	# Initialization
	groundControlMediator = GroundControlMediator()
	flight1 = Flight(groundControlMediator)
	runway1 = Runway(groundControlMediator)
	groundControlMediator.registerRunway(runway1)
	groundControlMediator.registerFlight(flight1)
	# Requests
	flight1.getReady()
	flight1.land()
	runway1.land()
	flight1.land()


if __name__ == "__main__":
    main()

