package tvark.old;

import java.io.File;
import java.io.Serializable;

public class Schedule implements Serializable {

	/**
	 * 
	 */
//	private Long id;
	private static final long serialVersionUID = -2456493396322851990L;
	private Subject[][][] SubjectSchedule;
	private Teacher[][][] TeacherSchedule;
	private Student[][][][] StudentSchedule;
	private String[][][] GroupSchedule;
	private Room[][][] RoomSchedule;
	private int[][][] StudentCount;
	private boolean[][][] placeholder;
//	private boolean[][][][] placeholderSt;

	
	private String folder;
	private ScheduleData data;
	private int lpd = 24; // lessons per day;
	private int maxClasses = 60;


	transient private StudTeachSchedule TL;
	private Teacher[] teachers;
	private Student[] students;
    private Room[] rooms;
	private PointL lastFrom, lastTo;
//	private PointSt lastFromSt, lastToSt;
	private String rootdir;
	private int maxpergr;


	Schedule() {

	}

	public Schedule(String Path, String rootdir, int maxclass, String folder1) {
		if(maxclass!=0)maxClasses =maxclass;
		this.rootdir=rootdir;
		
		this.folder = folder1;
		data = new ScheduleData(Path);
		teachers = data.getTeachers();
		students = data.getStudents();
	    rooms = data.getRooms();
		System.out.println("Data ready... Compiling Initial Schedule ");
		TL = new StudTeachSchedule(data, lpd, maxClasses);
		SubjectSchedule = TL.getSubjectSchedule();
		TeacherSchedule = TL.getTeacherSchedule();
		StudentSchedule = TL.getStudentSchedule();
		GroupSchedule = TL.getGroupSchedule();
		RoomSchedule = TL.getRoomSchedule();
		StudentCount = TL.getStudentCount();
		placeholder = TL.getPlaceholder();
		maxpergr = TL.getMaxPerGroup();
		System.out.println("Compiling Complete ... ");
		getGroups();
	// printSubjectsScreen();
    //   printRoomsScreen();	

		System.out.print("Testing Teachers: ");
		if (testTeach()) {
			System.out.println("Pass");
		} else {
			System.out.println("Fail");
		}

		System.out.print("Testing Students: ");
		if (testStud()) {
			System.out.println("Pass");
		} else {
			System.out.println("Fail");
		}

		System.out.print("Testing Rooms: ");
		if (testRoom()) {
			System.out.println("Pass");
		} else {
			System.out.println("Fail");
		}
		
	//---------------------	
		System.out.println("Parsing results ...");
		printGroupsXML("Groups_initial");
		printSubjectsXML("Subjects_initial");
		printTeachersXML("Teacher_initial");
		printRoomXML("Room_initial");
		printSubjGroupsXML(null);
		
		
		File f = new File(rootdir+"/"+folder+"/students_initial");
		if (f.mkdir()) {
			System.out.println("Dir /"+folder+"/students_initial Created");
		} else {
			System.out
					.println("Dir /"+folder+"/students_initial NOT Created OR it already  Exists");
		}

		for (int i = 0; i < data.getStudents().length; i++) {
			this.printStudentXML(data.getStudents()[i], "students_initial");
		}

		f = new File(rootdir+"/"+folder+"/teachers_initial");
		if (f.mkdir()) {
			System.out.println("Dir  c:/"+folder+"/teachers_initial Created");
		} else {
			System.out
					.println("Dir c:/"+folder+"/teachers_initial NOT Created OR it already Exists");
		}
		for (int i = 0; i < data.getTeachers().length; i++) {
			this.printTeacherXML(data.getTeachers()[i], "teachers_initial");
		}
	
		 f = new File(rootdir+"/"+folder+"/rooms_initial");
		if (f.mkdir()) {
			System.out.println("Dir  c:/"+folder+"/rooms_initial Created");
		} else {
			System.out
					.println("Dir c:/"+folder+"/rooms_initial NOT Created OR it already Exists");
		}
		for (int i = 0; i < data.getRooms().length; i++) {
			this.printRoomXML(data.getRooms()[i], "rooms_initial");
		}		
		
		System.out.println("Parse Complete... ");
		System.out.println("Initial  Schedule Process Complete...");
		

		
		
	}
//------------------------------------------------------------------------------
//-------------------Mokinių langų uždarymas ----------------------------
//	-----------------kai buvau ikelusi pries optimizavimo metoda---------------------------
	public void StudentWindowClose(Property prop) {

		System.out.println("Pradedamas mokiniu langu uzdarinejimas... ");
		
		int Penalty = this.countPenalty(prop);
		System.out.println("Pradzioje baudos tasku... "+ Penalty);		
		int CurPenalty = 0;
		for (int j = 0; j < students.length; j++) 
		{
			//	boolean inFlag =false;
			PointSt[] wind = this.findStudentWindow(j);
			for(int w=0; w< wind.length; w++)
			{	
			
				if (wind[w].isPointSt())
				{
					PointSt[] points = this.findAnyFillStud(j);
					
					boolean flag = true;
					for (int k = 0; k < points.length; k++) 
					{
			//			System.out.println("tikriname ar gali perkelti"+ k);		
						if (this.testMoveStudent(points[k], wind[w]) && flag )
								{
									flag = false;
									this.moveStudent(points[k], wind[w]);
									CurPenalty =this.countPenalty(prop);
									if (Penalty < CurPenalty){
										this.UnDoMoveStud(wind[w], points[k]);
									}	
									break;
								}
					}
								
			//		closeWindowSt(j, wind, prop);
			//		if(!inFlag)
			//		{
			//			inFlag=true;
			//		}
				}
				CurPenalty =this.countPenalty(prop);
				
		//	System.out.println("Baudos taskai "+ Penalty+" Baudos taskai perkelus "+CurPenalty);
				
				if (Penalty > CurPenalty) 
				{
					System.out.println("		             Got better: -"+ (Penalty-CurPenalty)+ " Points");
					Penalty = CurPenalty;
				}else if(Penalty < CurPenalty){
					System.out.println("		             Got worse: -"+ (Penalty-CurPenalty)+ " Points");
				}		
			}
		}
	
	System.out.println("Mokiniu langu uzdarymas baigtas ");	
	System.out.println("Pabaigus baudos tasku... "+ Penalty);	
		}	

//------------------------------------------------------------------------------	
	public boolean isBeforeLast(int teach, PointL wind) {

		return teachers[teach].isBusy(wind.getX(), wind.getY() + 1)
				&& !teachers[teach].isBusy(wind.getX(), wind.getY() + 2);

	}
//----------------------------------------------------------------
	public boolean testMoveTeacher(PointL from, PointL to) {
		boolean ret = false;

		for (int i = 0; i < maxClasses; i++) {
			if (TeacherSchedule[to.getX()][to.getY()][i] == null )
				ret = true;
		}

		if (!TeacherSchedule[from.getX()][from.getY()][from.getZ()].isBusy(to.getX(), to.getY()) 
			&& !RoomSchedule[from.getX()][from.getY()][from.getZ()].isBusy(to.getX(), to.getY()))	{	
			
			for (int i = 0; i < maxpergr; i++) {
				if (StudentSchedule[from.getX()][from.getY()][from.getZ()][i] != null
						&& StudentSchedule[from.getX()][from.getY()][from
								.getZ()][i].isBusy(to.getX(), to.getY()))
					ret = false;
			}
		
		} else {
			ret = false;
		}
		return ret;
	}

	
//----------kabineto perkelimo tikrinimas---------------
	
	public boolean testMoveRoom(PointL from, PointL to) 
	{
		boolean ret = true;
		int roomid = RoomSchedule[from.getX()][from.getY()][from.getZ()].getRoom();
		for(int i=0; i<RoomSchedule[to.getX()][to.getY()].length;i++)
		{
			if(RoomSchedule[to.getX()][to.getY()][i]!=null)
			if(RoomSchedule[to.getX()][to.getY()][i].getRoom()== roomid)
			{
				ret =  false;
			}		
		}
	return ret;
}	
	
//--------------------------------------------------------------	
		
	public boolean moveTeacher(PointL from, PointL to) {
		lastFrom = from;
		if (to==null || !to.isPointL()) return false;
		int day = to.getX();
		int lesson = to.getY();
		int parallel = -1;
		boolean flag = true;
		
		
		for (int i = 0; i < maxClasses; i++)
		{
			if (TeacherSchedule[day][lesson][i] == null && flag 
				&& RoomSchedule[day][lesson][i]==null) {
				flag = false;
				parallel = i;
			}
		}
		
		to.setZ(parallel);
		lastTo = to;
		if (flag) {
			System.out.println("Imposible to move");
			return false;
		}

		int fromd = from.getX();
		int froml = from.getY();
		int fromp = from.getZ();
		if (TeacherSchedule[fromd][froml][fromp] == null
				&& TeacherSchedule[day][lesson][parallel].isBusy(day, lesson)) {
			System.out.println("Imposible to move");
			return false;
		}
	
		
		TeacherSchedule[day][lesson][parallel] = TeacherSchedule[fromd][froml][fromp];
		TeacherSchedule[day][lesson][parallel].setBusy(day, lesson);
		// TeacherSchedule[day][lesson][parallel].unsetBusy(fromd, froml);
		
		RoomSchedule[day][lesson][parallel] = RoomSchedule[fromd][froml][fromp];
		RoomSchedule[day][lesson][parallel].setBusy(day, lesson);		
		

		for (int i = 0; i < StudentSchedule[day][lesson][parallel].length; i++) {
			StudentSchedule[day][lesson][parallel][i] = StudentSchedule[fromd][froml][fromp][i];
			if (StudentSchedule[day][lesson][parallel][i] != null
					&& !StudentSchedule[day][lesson][parallel][i].isBusy(day,
							lesson)) {
				StudentSchedule[day][lesson][parallel][i].setBusy(day, lesson);
				// StudentSchedule[day][lesson][parallel][i].unsetBusy(fromd,
				// froml);
			}
		}
		SubjectSchedule[day][lesson][parallel] = SubjectSchedule[fromd][froml][fromp];
		StudentCount[day][lesson][parallel] = StudentCount[fromd][froml][fromp];
		GroupSchedule[day][lesson][parallel] = GroupSchedule[fromd][froml][fromp];
		this.setPlaceholder(to);
		unSet(from);
		return true;

	}

	public boolean UnDoMove() {
		boolean ret = false;

		ret = moveTeacher(lastTo, lastFrom);

		return ret;
	}

	public void unSet(PointL point) {
		int day = point.getX();
		int lesson = point.getY();
		int parallel = point.getZ();

		TeacherSchedule[day][lesson][parallel].unsetBusy(day, lesson);
		TeacherSchedule[day][lesson][parallel] = null;
		
		RoomSchedule[day][lesson][parallel].unsetBusy(day, lesson);
		RoomSchedule[day][lesson][parallel] = null;
		
		for (int i = 0; i < StudentSchedule[day][lesson][parallel].length; i++) {
			if (StudentSchedule[day][lesson][parallel][i] != null) {
				StudentSchedule[day][lesson][parallel][i]
						.unsetBusy(day, lesson);
				StudentSchedule[day][lesson][parallel][i] = null;
			}
		}
		SubjectSchedule[day][lesson][parallel] = null;
		StudentCount[day][lesson][parallel] = 0;
		GroupSchedule[day][lesson][parallel] = null;
		this.unsetPlaceholder(point);
	}

	public boolean testSwitchTeacher(PointL from, PointL to) {
		boolean ret = true;
		if (!TeacherSchedule[from.getX()][from.getY()][from.getZ()].isBusy(to
				.getX(), to.getY())
				&& !TeacherSchedule[to.getX()][to.getY()][to.getZ()].isBusy(
						from.getX(), from.getY())) {

			for (int i = 0; i < maxpergr; i++) {

				if (StudentSchedule[from.getX()][from.getY()][from.getZ()][i] != null
						&& StudentSchedule[from.getX()][from.getY()][from
								.getZ()][i].isBusy(to.getX(), to.getY()))
					ret = false;

				if (StudentSchedule[to.getX()][to.getY()][to.getZ()][i] != null
						&& StudentSchedule[to.getX()][to.getY()][to.getZ()][i]
								.isBusy(from.getX(), from.getY()))
					ret = false;

			}

		} else {
			ret = false;
		}

		return ret;

	}
//--------------baudos tasku skaiciavimui-----------------------
	public int teachWindow() {
		int ret = 0;
		boolean inBusy = false, outBusy = false, inAgain = false;

		int tempCount = 0;
		int temp = 0;

		for (int teach = 0; teach < teachers.length; teach++) {

			for (int day = 0; day < 5; day++) {
				inBusy = false;
				outBusy = false;
				inAgain = false;
				temp = 0;
				for (int lesson = 0; lesson < lpd; lesson++) {

					if (teachers[teach].isBusy(day, lesson)) {
						inBusy = true;
					}

					if (inBusy && !teachers[teach].isBusy(day, lesson)) {
						temp++;
						outBusy = true;
					}

					if (outBusy && teachers[teach].isBusy(day, lesson))
						inAgain = true;

					if (inBusy && outBusy && inAgain) {
						tempCount += temp;

						outBusy = false;
						inAgain = false;
						inBusy = false;
					}

				}
			}
		}
		ret = tempCount;

		return ret;
	}
//--------------------is optimizavimo------------------------------
	public PointL[] findTeachWindow(int teach) {

		int tempCount = 0;	
		for (int day = 0; day < 5; day++)
		{
			boolean inBusy = false;
			boolean outBusy = false;
			boolean inAgain = false;
			int temp = 0;
			
			for (int lesson = 0; lesson < lpd; lesson++)
			{
				if (teachers[teach].isBusy(day, lesson))
				{
					inBusy = true;
				}
				if (inBusy && !teachers[teach].isBusy(day, lesson))
				{
					temp++;
					outBusy = true;
				}
				if (outBusy && teachers[teach].isBusy(day, lesson))
					inAgain = true;
				if (inBusy && outBusy && inAgain) 
				{
					tempCount += temp;
					outBusy = false;
					inAgain = false;
				}
			}	
		}
			
		PointL[] ret = new PointL[tempCount];
		for(int i=0; i<ret.length; i++){
			ret[i] = new PointL(-1, -1, -1);
		}
		tempCount=0;

		for (int day = 0; day < 5; day++) {
			boolean inBusy = false;
			boolean outBusy = false;
			boolean inAgain = false;
			int temp = 0;
			
			for (int lesson = 0; lesson < lpd; lesson++) {

				if (teachers[teach].isBusy(day, lesson)) {

					inBusy = true;
				}

				if (inBusy && !teachers[teach].isBusy(day, lesson)) {
					temp++;
					outBusy = true;
				}

				if (outBusy && teachers[teach].isBusy(day, lesson))
					inAgain = true;

				if (inBusy && outBusy && inAgain) {
					ret[tempCount].setPointL(day, lesson - 1, 0);
					tempCount += temp;
					outBusy = false;
					inAgain = false;
			//		System.out.println("langas " + teachers[teach].getName()+"  langas"+ tempCount+" diena " + (day+1) +" pamoka "+lesson);
				}
			}
		}
		for(int m=0; m<ret.length; m++)
		{
		boolean flag = false;
		for (int i = 0; i < maxClasses; i++) {
			if (ret[m].isPointL()
					&& TeacherSchedule[ret[m].getX()][ret[m].getY()][i] == null
					&& !flag) {
				ret[m].setZ(i);
				flag = true;
			}
		}

		if (!flag) {
			ret[m].setZ(-1);
		}
		}
		return ret;
	}

//------------------------n--------------------------
	public PointL findWindFill(PointL wind, int teach) {
		PointL ret = new PointL();
		PointL fill = new PointL();
		boolean test = false;
		for (int day = 0; day < 5; day++) {
			if (!test) {
				fill = getLastLesson(teach, day);
				test = testMoveTeacher(wind, fill);

			}
			if (!test) {
				fill = getFirstLesson(teach, day);
				test = testMoveTeacher(wind, fill);

			}
		}

		if (test)
			ret = fill;

		return ret;
	}
//--------------------------------------------------------
	public PointL[] findAnyFill(PointL wind, int teach) {

		int temp = 0;
		for (int day = 0; day < 5; day++) {
			for (int lesson = 0; lesson < lpd; lesson++) {
				if (teachers[teach].isBusy(day, lesson))
					temp++;
			}

		}
		PointL[] ret = new PointL[temp];
		temp = 0;

		for (int lesson = 0; lesson < lpd; lesson++) {
			for (int day = 0; day < 5; day++) {
				if (teachers[teach].isBusy(day, lesson)) {

					for (int parallel = 0; parallel < maxClasses; parallel++) {
						if (TeacherSchedule[day][lesson][parallel] != null
								&& TeacherSchedule[day][lesson][parallel]
										.getName().equals(
												teachers[teach].getName())) {
							ret[temp] = new PointL(day, lesson, parallel);

							temp++;

						}
					}

				}
			}

		}

		return ret;
	}
//------------------------is optimizavimo------------------------------------
	public PointL FindBestFill(PointL wind, int teach,Property prop) {
		PointL[] points = findAnyFill(wind, teach);
		// boolean[][][] testMatrix = new boolean[5][lpd][maxClasses];
		int Penalty = countPenalty(prop);
		int CurPenalty = 0;
		int best = -1;
		for (int i = 0; i < points.length; i++) {
			if (this.testMoveTeacher(points[i], wind)) {
				System.out.println("Salygos patenkintos mokytojas keliamas");
				boolean flag = this.moveTeacher(points[i], wind);
				CurPenalty = this.countPenalty(prop);
				if (CurPenalty < Penalty) {
					best = i;
					Penalty = CurPenalty;
				}
				if (flag)
					this.UnDoMove();
			}

		}

		if (best == -1) {
			return new PointL();
		} else {
			return points[best];
		}
	}
//----------------------------------------------
	public int countOff() {
		int ret = 0;
		for (int i = 0; i < teachers.length; i++) {
			int off[] = teachers[i].getOff();
			int count = 0;
			for (int k = 0; k < off.length; k++) {
				if (off[k] != 0) {
					for (int lesson = 0; lesson < lpd; lesson++) {
						if (teachers[i].isBusy(off[k] - 1, lesson))
							count++;
					}
				}
			}
			ret += count;
		}

		return ret;
	}
//-------------------is optimizavimo----------------------------------
	public void FreeDay(int teach, PointL err, Property prop) {

		for (int i = 0; i < maxClasses; i++) {
			if (TeacherSchedule[err.getX()][err.getY()][i] != null
					&& TeacherSchedule[err.getX()][err.getY()][i].getNr() == teach)
				err.setZ(i);
		}

		int fillCount = 0;
		for (int day = 0; day < 5; day++) {
			if (day != err.getX()) {
				for (int lesson = 0; lesson < lpd; lesson++) {
					for (int parallel = 0; parallel < maxClasses; parallel++) {
						boolean flag = false;

						for (int i = 0; i < StudentSchedule[day][lesson][parallel].length; i++) {
							if (StudentSchedule[err.getX()][err.getY()][err
									.getZ()][i] != null
									&& StudentSchedule[err.getX()][err.getY()][err
											.getZ()][i].isBusy(day, lesson))
								flag = true;
						}

						if (!TeacherSchedule[err.getX()][err.getY()][err.getZ()]
								.isBusy(day, lesson)
								&& !flag
								&& TeacherSchedule[day][lesson][parallel] == null) {
							fillCount++;
						}

					}

				}
			}
		}
		PointL[] fill = new PointL[fillCount];

		fillCount = 0;

		for (int day = 0; day < 5; day++) {
			if (day != err.getX()) {
				int n = 0;
				for (int lesson = 0; lesson < lpd; lesson++) {
					boolean Lflag = false;

					for (int parallel = 0; parallel < maxClasses; parallel++) {
						boolean flag = false;
						//boolean Pflag = false;

						for (int i = 0; i < StudentSchedule[day][lesson][parallel].length; i++) {
							if (StudentSchedule[err.getX()][err.getY()][err
									.getZ()][i] != null
									&& StudentSchedule[err.getX()][err.getY()][err
											.getZ()][i].isBusy(day, lesson))
								flag = true;
						}

						if (!TeacherSchedule[err.getX()][err.getY()][err.getZ()]
								.isBusy(day, lesson)
								&& !flag
								&& TeacherSchedule[day][lesson][parallel] == null) {
							fill[fillCount] = new PointL();
							fill[fillCount].setPointL(day, lesson, parallel);
							fillCount++;
							break;
						} else {
							Lflag = true;
						}

					}
					if (!Lflag)
						n++;
					if (n > 2)
						break;
				}
			}
		}

		this.findBestPoint(err, fill, prop);

	}
//--------------------------------------------------------------
	public void findBestPoint(PointL err, PointL[] fill,Property prop) {

		int Penalty = this.countPenalty(prop);
		int CurPenalty = Penalty;
		int bestFill = -1;
		//boolean flag = false;
		for (int j = 0; j < fill.length; j++) {
			if (fill[j] != null && fill[j].isPointL()) {
				// /////////////////////////////////////////////////////////
			//	if (!flag && this.testMoveTeacher(err, fill[j])) {
				//	flag = true;
					//bestFill = j;
				//}
				
				//tikrinu ar kiti to kabineto tuo metu neuzeme
				if(testMoveRoom(err, fill[j]))
				{	 
				  moveTeacher(err, fill[j]);
				  CurPenalty = countPenalty(prop); 
				  if (CurPenalty > Penalty) {
				  //if (flag)
					  UnDoMove();
					  } else { //if (flag)
						  UnDoMove();
				  Penalty = CurPenalty; 
				  bestFill = j; }
				} 
			}
		}

		if (bestFill != -1)
			moveTeacher(err, fill[bestFill]);

		CurPenalty = countPenalty(prop);
		if (CurPenalty > Penalty) {
			// if (flag)
			UnDoMove();
		}

	}
//-------------baudos taskai-------------------------------
//---------------------------------------------------------
	public int countPenalty(Property prop)
	{
		return this.teachWindow() * prop.getPTeach() + this.studWindow() * prop.getPStud()
				+ this.countOff() * prop.getPOff() + overMaxLimit(prop.getMaxLimit()) * prop.getPMax()
				+this.PedagDidPazeid()*prop.getPDidakt();
		
	}

	
//-----------------skaiciuojami pedagoginių didaktikų pazeidimui baudos taskai -----------	
	public int PedagDidPazeid()
	{
		int ret = 0;
		for (int day = 0; day < 5; day++) 
		{
			for (int lesson = 0; lesson < lpd; lesson++) 
			{
				for (int c = 0; SubjectSchedule[day][lesson][c] != null; c++) 
				{
					if (SubjectSchedule[day][lesson][c] != null) 
					{
						int code =Integer.parseInt(SubjectSchedule[day][lesson][c].getPriority());
						
						if(lesson>=5 && code>=4)
						ret=ret+(lesson*code);
					  //ret=ret+(lesson/10*code);
					}
				}
			}
		}
//	System.out.println("pazeidus pedagogines didaktikas taskai = " +ret);
//	PamokAntDienPus();
	return ret;
	}
//------------------------------------------------------
public void	PamokAntDienPus()
{
	
	for (int day = 0; day < 5; day++) 
	{
		for (int lesson = 0; lesson < lpd; lesson++) 
		{
			for (int c = 0; SubjectSchedule[day][lesson][c] != null; c++) 
			{
				if (SubjectSchedule[day][lesson][c] != null) 
				{
					int code =Integer.parseInt(SubjectSchedule[day][lesson][c].getPriority());
					
					if(lesson>=5 && code>=4)
					System.out.println("Antroje dienos puseje " +SubjectSchedule[day][lesson][c].getSubjCode());
				}
			}
		}
	}
}


//------------------Mokinio lango radimas-----------------------------
	public PointSt[] findStudentWindow(int stud) 
	{
		int tempCount = 0;	
		for (int day = 0; day < 5; day++)
		{
			boolean inBusy = false;
			boolean outBusy = false;
			boolean inAgain = false;
			
			int temp = 0;
			
			for (int lesson = 0; lesson < lpd; lesson++)
			{
				if (students[stud].isBusy(day, lesson))
				{
					inBusy = true;
				}
				if (inBusy && !students[stud].isBusy(day, lesson))
				{
					temp++;
					outBusy = true;
				}
				if (outBusy && students[stud].isBusy(day, lesson))
					inAgain = true;
				if (inBusy && outBusy && inAgain) 
				{
					tempCount += temp;
					outBusy = false;
					inAgain = false;
				}
			}	
		}
		PointSt[] ret = new PointSt[tempCount];
	for(int i=0; i<ret.length; i++){
		ret[i] = new PointSt(-1, -1, -1, -1);
	}
	tempCount=0;
	
		for (int day = 0; day < 5; day++)
		{
			boolean inBusy = false;
			boolean outBusy = false;
			boolean inAgain = false;
			
			int temp = 0;
			
			for (int lesson = 0; lesson < lpd; lesson++)
			{
				if (students[stud].isBusy(day, lesson))
				{
					inBusy = true;
				}
				if (inBusy && !students[stud].isBusy(day, lesson))
				{
					temp++;
					outBusy = true;
				}
				if (outBusy && students[stud].isBusy(day, lesson))
					inAgain = true;
				if (inBusy && outBusy && inAgain) 
				{
					ret[tempCount].setPointSt(day, lesson - 1, 0,0);
					tempCount += temp;
					outBusy = false;
					inAgain = false;
				//	arRado = true;
	//	System.out.println("langas " + students[stud].getName()+"  langas"+ tempCount+" diena " + day+" pamoka "+lesson);
				//	break;
				}
			}	
		}
		
		for(int m=0; m<ret.length; m++)
		{
			boolean flag = false;
			for (int i = 0; i < maxClasses; i++) {
			
				if (ret[m].isPointSt()
						&& SubjectSchedule[ret[m].getX()][ret[m].getY()][i] == null
						&& !flag) {
					ret[m].setZ(i);
				}
			}

			for (int s = 0; s < 30; s++) {
				if (ret[m].isPointSt()
						&& StudentSchedule[ret[m].getX()][ret[m].getY()][ret[m].getZ()][s] == null
						&& !flag) {
					ret[m].setZ(s);
					flag=true;
				}
			}
			
			if (!flag) 
			{
				ret[m].setZ(-1);
				ret[m].setS(-1);
			}
		}	
		return ret;
	}	
//------------------suraso visas mokinio uzimtas pamokas----------------------------------------	
	public PointSt[] findAnyFillStud(int stud)
	{
		int temp = 0;
		for (int day = 0; day < 5; day++) {
			for (int lesson = 0; lesson < lpd; lesson++)
			{
				if (students[stud].isBusy(day, lesson))
					temp++;
			}
		}
		PointSt[] ret = new PointSt[temp];
		temp = 0;
//-------pamokos surasomos nuo paskutines
		for (int lesson = lpd-1; lesson >= 0; lesson --) 
		{
//---------dienos surasomos nuo paskutines
			for (int day = 4; day >= 0; day--) 
			{
				if (students[stud].isBusy(day, lesson)) 
				{

					for (int parallel = 0; parallel < maxClasses; parallel++)
					{
						for(int sk=0; sk<30; sk++)
						{
							if (StudentSchedule[day][lesson][parallel][sk] != null
								&& StudentSchedule[day][lesson][parallel][sk]
										.getName().equals(
												students[stud].getName())) 
							{
									ret[temp] = new PointSt(day, lesson, parallel,sk);
			//		System.out.println("diena "+day+ " pamoka "+lesson);
								temp++;

							}
						}
					}
				}
			}
		}
		
		return ret;
	}
//------gauti kokios pamokos vyksta mokinio lango metu-------------	
	public String[] MokLangMetVykstPam(PointSt wind)
//	public void MokLangMetVykstPam(PointSt wind)
	{
		int temp=0;
		int day = wind.getX();
		int lesson = wind.getY();
		
		    //--suskaiciuojame kiek tuo metu vyksta pamoku----------------
		for (int parallel = 0; parallel < maxClasses; parallel++)
		{
			if(SubjectSchedule[day][lesson][parallel]!=null)
			{
				temp++;
			}
		}
		String[] langDal = new String[temp];
		int[] mok = new int[30];
		String[] grupem = new String[temp];
		if(temp!=0)
		{
		//------pamokos surasomos i masyva----------------
		 for (int parallel = 0; parallel < temp; parallel++)
			{
			 if(SubjectSchedule[day][lesson][parallel]!=null)
			 {
				langDal[parallel] = SubjectSchedule[day][lesson][parallel].getSubjCode();
				mok[parallel] = StudentCount[day][lesson][parallel];
				grupem[parallel] = GroupSchedule[day][lesson][parallel];
			//	System.out.println("Lango metu vyksta pamoka "+ langDal[parallel]+" Mokiniu "+ mok[parallel]+" grupei "+grupem[parallel]);
			 }
			 }
		}
		return langDal;
	}
	
//------------suranda geriausia varianta studentui perkelti ----------------------
	public PointSt FindBestFillStud(PointSt wind, int stud, Property prop) 
	{
		PointSt[] points = findAnyFillStud(stud);

		int Penalty = countPenalty(prop);

		int CurPenalty = 0;
		int best = -1;

		for (int i = 0; i < points.length; i++) 
		{

			if (this.testMoveStudent(points[i], wind)) 
			{
				boolean flag = this.moveStudent(points[i], wind);
	
				CurPenalty = this.countPenalty(prop);
				if (CurPenalty < Penalty) {
					best = i;
					Penalty = CurPenalty;
				}
				if (flag)
					
					this.UnDoMoveStud(wind,points[i] );
				System.out.println("Mokinio perkelimas.............."+ flag);
			}

		}

		if (best == -1) 
		{
			return new PointSt();
		} else 
		{
			return points[best];
		}
	}

	
//------------------------------------------------------------------
	public boolean testMoveStudent(PointSt from, PointSt to) 
	{
		boolean ret = false;
		
     String[] langoDalykai = MokLangMetVykstPam(to);
	//	System.out.println("lange yra dalyku"+ langoDalykai.length);
    
		for (int i = 0; i < langoDalykai.length; i++) 
		{
	//		System.out.println("Lango dalykas.............."+langoDalykai[i]);
	//		System.out.println("Norim perkelti dalyka.............."+SubjectSchedule[from.getX()][from.getY()][from.getZ()].getSubjCode());
			if (langoDalykai[i] == SubjectSchedule[from.getX()][from.getY()][from.getZ()].getSubjCode()
						&& StudentCount[to.getX()][to.getY()][i]<
							SubjectSchedule[from.getX()][from.getY()][from.getZ()].getMaxStud())
				{
					//jei rado nors viena destoma dalyka kaip ir norima perkelti
					//ir jei nevirsytas dar max to dalyko mokiniu skaicius
					to.setZ(i);
					ret = true;
					break;
				} else {
					ret = false;
				}	
		}
	//	System.out.println("ar rado, kad gali perkelti"+ ret);
		return ret;
	}	
	
	//--------------------------------------------------------------	
	
	public boolean moveStudent(PointSt from, PointSt to) 
	{
		//lastFromSt = from;
		int day = to.getX();
		int lesson = to.getY();
		int parallel = to.getZ();
		int studkl =-1;
		boolean flag = true;
		
	
		for (int i = 0; i < 30; i++)
		{
			if (StudentSchedule[day][lesson][parallel][i] == null && flag) 
		    {
				flag = false;
				studkl=i;
			}
		}
		
		to.setS(studkl);
		
			if (flag) {
				System.out.println(" Perkelimas neimanomas");
				return false;
			}
	if(!flag)
	{
		int fromd = from.getX();
		int froml = from.getY();
		int fromp = from.getZ();
		int froms = from.getS();
		
		if (StudentSchedule[fromd][froml][fromp][froms] == null)
		{
			System.out.println("Perkelimas neimanomas");
			return false;
		}
	
		StudentSchedule[day][lesson][parallel][studkl] = StudentSchedule[fromd][froml][fromp][froms];
		StudentSchedule[day][lesson][parallel][studkl].setBusy(day, lesson);
		StudentCount[day][lesson][parallel]++;

		boolean fg =true;
		String temp = StudentSchedule[fromd][froml][fromp][froms].getKlass();
		if(GroupSchedule[day][lesson][parallel]!= null)
		{
			String klas[]=GroupSchedule[day][lesson][parallel].split(";");
		//	System.out.println("klasiu sarasas "+GroupSchedule[day][lesson][parallel]);
		//	System.out.println("kelia klase "+ temp+ " sarase klasiu "+klas.length );
			for(int a=0; a<klas.length; a++)
			{
			//	System.out.println("klase "+ temp+" = klasei "+klas[a]);
				if(klas[a].equals(temp))
				{
					fg = false;
				}
			}	
		//	System.out.println("veliavele liko "+ fg);
				if (fg==true)
				{
					GroupSchedule[day][lesson][parallel] = GroupSchedule[day][lesson][parallel] + ";" + temp;	
				}
			
		}
			//	this.setPlaceholderSt(to);
	}
		unSetSt(from);
		return true;

	}
//-------------perkelus studenta-------------------------	
	public void unSetSt(PointSt point) 
	{
		int day = point.getX();
		int lesson = point.getY();
		int parallel = point.getZ();
		int st = point.getS();
		boolean flag = false;
		String studgr = StudentSchedule[day][lesson][parallel][st].getKlass();
		StudentSchedule[day][lesson][parallel][st].unsetBusy(day, lesson);
		StudentSchedule[day][lesson][parallel][st] = null;
		
		for (int i = 0; i < StudentSchedule[day][lesson][parallel].length; i++)
		{
			if (StudentSchedule[day][lesson][parallel][i] != null) {
				flag= true;
			}
		}
		// Jei studentas buvo vienintelis toi vietoje
		if (flag == false)
		{
			RoomSchedule[day][lesson][parallel].unsetBusy(day, lesson);
			RoomSchedule[day][lesson][parallel] = null;
			TeacherSchedule[day][lesson][parallel].unsetBusy(day, lesson);
			TeacherSchedule[day][lesson][parallel] = null;
			SubjectSchedule[day][lesson][parallel] = null;
			StudentCount[day][lesson][parallel] = 0;
			GroupSchedule[day][lesson][parallel] = null;
		}else{
			StudentCount[day][lesson][parallel] --;
			//---------studentu sarasa likusi susitvarkome
			if (st!=29){
				for (int i = st; i < StudentSchedule[day][lesson][parallel].length-1; i++)
				{
					if (StudentSchedule[day][lesson][parallel][i] == null &&
						StudentSchedule[day][lesson][parallel][i+1] != null) 
					{
						StudentSchedule[day][lesson][parallel][i]=StudentSchedule[day][lesson][parallel][i+1];
						StudentSchedule[day][lesson][parallel][i+1] = null;
					}
				}
			}
			//----------grupiu sarasa likusi susitvarkome------------
			boolean fg =false;

			for (int i = 0; i < StudentSchedule[day][lesson][parallel].length; i++)
			{
				if (StudentSchedule[day][lesson][parallel][i]!=null)
				{
					if (StudentSchedule[day][lesson][parallel][i].getKlass().equals(studgr)) 
					{
						fg= true;
						if(fg==true)
							break;
					}
				}
			}	
			if(fg==false)
			{
				//------is tos grupes jis buvo vienintelis----------------------
				String groups = null;
				String klas[]=GroupSchedule[day][lesson][parallel].split(";");
				for(int a=0; a<klas.length; a++)
				{
					if(klas[a].equals(studgr))
					{
						
					}else{
						groups = groups + ";" + klas[a];	
					}
				}
				GroupSchedule[day][lesson][parallel]=groups;
			}			
		}
	//	this.unsetPlaceholderSt(point);
	}
//----------------------------------------------------	
	public boolean UnDoMoveStud(PointSt from, PointSt to) 
	{

		boolean ret = false;
		ret = moveStudent(from, to);
		return ret;
	}	
	
	
//-------------baudos tasku skaiciavimui------------------------	
	public int overMaxLimit(int maxLimit) {
		int ret = 0;

		for (int day = 0; day < 5; day++) {

			//int tempMax = 0;

			for (int parallel = 0; parallel < maxClasses; parallel++) {
				int temp = 0;
				for (int lesson = 0; lesson < lpd; lesson++) {
					PointL point = new PointL(day, lesson, parallel);
					if (this.isPlaceholder(point)) {
						temp++;
					}

				}
				if (temp - maxLimit > 0)
					ret = ret + (temp - maxLimit);
			}
			// if((tempMax-maxLimit)>0) ret=ret + (tempMax-maxLimit);
		}

		return ret;
	}
//------------------------------------------------------
	public PointL getLastLesson(int teach, int day) {
		PointL ret = new PointL();
		for (int lesson = 0; lesson < lpd; lesson++) {
			for (int parallel = 0; parallel < maxClasses; parallel++) {
				if (TeacherSchedule[day][lesson][parallel] != null
						&& TeacherSchedule[day][lesson][parallel].getNr() == teach
						&& teachers[teach].isBusy(day, lesson)) {
					ret.setPointL(day, lesson, parallel);
				}
			}
		}
		return ret;
	}
//----------------------------------------------------
	public PointL getFirstLesson(int teach, int day) {
		PointL ret = new PointL();
		boolean in = false;
		for (int lesson = 0; lesson < lpd; lesson++) {
			if (teachers[teach].isBusy(day, lesson) && !in) {
				ret.setPointL(day, lesson, 0);
				in = true;
			}
		}
		return ret;
	}
//----------------baudos taskams skaiciuoti-----------------------------------------
	public int studWindow() {
		int ret = 0;
		boolean inBusy = false, outBusy = false, inAgain = false;

		int tempCount = 0;
		int temp = 0;

		for (int teach = 0; teach < students.length; teach++) {

			for (int day = 0; day < 5; day++) {
				inBusy = false;
				outBusy = false;
				inAgain = false;
				temp = 0;
				for (int lesson = 0; lesson < lpd; lesson++) {

					if (students[teach].isBusy(day, lesson)) {
						inBusy = true;
					}

					if (inBusy && !students[teach].isBusy(day, lesson)) {
						temp++;
						outBusy = true;
					}

					if (outBusy && students[teach].isBusy(day, lesson))
						inAgain = true;

					if (inBusy && outBusy && inAgain) {
						tempCount += temp;
						outBusy = false;
						inAgain = false;
						inBusy = false;
					}

				}
			}
		}
		ret = tempCount;

		return ret;
	}
//-------------------------------------------------
	public boolean testTeach() {
		boolean ret = true;
		int count;
		for (int day = 0; day < 5; day++) {
			for (int lesson = 0; lesson < lpd; lesson++) {

				for (int teach = 0; teach < teachers.length; teach++) {
					count = 0;
					for (int i = 0; i < maxClasses; i++) {
						if (TeacherSchedule[day][lesson][i] != null
								&& teachers[teach].getName().equals(
										TeacherSchedule[day][lesson][i]
												.getName())) {
							count++;
						}
					}
					if (count > 1)
						ret = false;
				}
			}
		}
		return ret;
	}
//-----------------------------------------------------------------
	public boolean testStud() {
		boolean ret = true;
		int count;

		for (int day = 0; day < 5; day++) {
			for (int lesson = 0; lesson < lpd; lesson++) {

				for (int teach = 0; teach < students.length; teach++) {
					count = 0;
					for (int i = 0; i < maxClasses; i++) {

						for (int j = 0; j < StudentSchedule[day][lesson][i].length; j++) {
							if (StudentSchedule[day][lesson][i][j] != null
									&& students[teach].getName().equals(
											StudentSchedule[day][lesson][i][j]
													.getName())) {
								count++;
							}
						}
					}
					if (count > 1) {
						ret = false;
					}
				}
			}
		}
		return ret;
	}
//-----------------------------------------------------------------------------
	public boolean testRoom() 
	{
		boolean ret = true;
		int count;
		for (int day = 0; day < 5; day++)
		{
			for (int lesson = 0; lesson < lpd; lesson++)
			{
				for (int r = 0; r < rooms.length; r++)
				{
					count = 0;
					for (int i = 0; i < maxClasses; i++) {
						if (RoomSchedule[day][lesson][i] != null
								&& rooms[r].getRoom()==	RoomSchedule[day][lesson][i].getRoom()) 
						{
							count++;
						}
					}
					if (count > 1)
						ret = false;
				}
			}
		}
		return ret;
	}
	
//------------------------------------------------------------------------------	
	
	public void printSubjectsScreen() {
		for (int a = 0; a < 5; a++) {
			System.out.println("========================================= "
					+ (a + 1) + "-dienis ========");

			for (int b = 0; b < lpd; b++) {
				System.out.println("------------------------------ " + (b + 1)
						+ "-oji pamoka ----");

				for (int c = 0; SubjectSchedule[a][b][c] != null; c++) {

					if (SubjectSchedule[a][b][c] != null) {
						System.out.print(SubjectSchedule[a][b][c].getSubjCode()
								+ "; ");

					}
				}
				System.out.println();

			}
		}
	}
//----------------------------------------------
public void printSubjectsXML(String name) {
        
		String DayNames [] = new String [5];
		DayNames[0] = "Monday";
		DayNames[1] = "Tuesday";
		DayNames[2] = "Wednesday";
		DayNames[3] = "Thursday";
		DayNames[4] = "Friday";
		XmlFileOut tvark = new XmlFileOut(folder+"/"+name+".xml", rootdir);
		for (int a = 0; a < 5; a++) {
			tvark.startRow();
			tvark.addTag("");
			tvark.endRow();
			tvark.startRow();
			tvark.addTag((a + 1) + "-dienis / (" +DayNames[a]+")" );
			tvark.endRow();
			//for (int b = 0; b < lpd; b++) {
			int b = 0;
			while (SubjectSchedule[a][b][0] != null){
				tvark.startRow();
				tvark.addTag("(Nr.: "+(b + 1) + ") ->");
				for (int c = 0; c < maxClasses; c++) {
					
					if (SubjectSchedule[a][b][c] != null) {									
						tvark.addTag(SubjectSchedule[a][b][c].getSubjCode());
						// c++;
					}
				}
				tvark.endRow();
				b++;
			}
		}
		tvark.close();
	}
//--------------------------------------------------
	public void printGroupsScreen() {
		for (int a = 0; a < 5; a++) {
			System.out.println("========================================= "
					+ (a + 1) + "-dienis ========");

			for (int b = 0; b < lpd; b++) {
				System.out.println("------------------------------ " + (b + 1)
						+ "-oji pamoka ----");

				for (int c = 0; GroupSchedule[a][b][c] != null; c++) {

					if (TeacherSchedule[a][b][c] != null) {
						System.out.print(GroupSchedule[a][b][c] + "; ");

					}
				}
				System.out.println();

			}
		}
	}
//--------------------------------
	public void printGroupsXML(String name) {
		String DayNames [] = new String [5];
		DayNames[0] = "Monday";
		DayNames[1] = "Tuesday";
		DayNames[2] = "Wednesday";
		DayNames[3] = "Thursday";
		DayNames[4] = "Friday";
		XmlFileOut tvark = new XmlFileOut(folder+"/"+name+".xml", rootdir);
		for (int a = 0; a < 5; a++) {
			tvark.startRow();
			tvark.addTag("");
			tvark.endRow();
			tvark.startRow();
			tvark.addTag((a + 1) + "-dienis / (" +DayNames[a]+")" );
			tvark.endRow();
			//for (int b = 0; b < lpd; b++) {
			int b = 0;
			while (SubjectSchedule[a][b][0] != null){
				tvark.startRow();
				tvark.addTag("(Nr.: "+(b + 1) + ") ->");

				for (int c = 0; c < maxClasses; c++) {
					if (TeacherSchedule[a][b][c] != null) {
						tvark.addTag(GroupSchedule[a][b][c]);
						// c++;
					}
				}
				tvark.endRow();
				b++;
			}
		}
		tvark.close();
	}

//----------------------------------------------------
	public void printSubjGroupsXML(String name) {
		String DayNames [] = new String [5];
		DayNames[0] = "Monday";
		DayNames[1] = "Tuesday";
		DayNames[2] = "Wednesday";
		DayNames[3] = "Thursday";
		DayNames[4] = "Friday";
		XmlFileOut tvark;
		if (name == null) {
			tvark = new XmlFileOut(folder+"/SubjGroups_initial.xml", rootdir);
		} else {
			String name1 = folder+"/"+name;
			tvark = new XmlFileOut(name1, rootdir);
		}
		for (int a = 0; a < 5; a++) {
			tvark.startRow();
			tvark.addTag("");
			tvark.endRow();
			tvark.startRow();
			tvark.addTag((a + 1) + "-dienis / (" +DayNames[a]+")" );
			tvark.endRow();
			//for (int b = 0; b < lpd; b++) {
			int b = 0;
			while (SubjectSchedule[a][b][0] != null){
				tvark.startRow();
				tvark.addTag("(Nr.: "+(b + 1) + ") ->");
				for (int c = 0; c < maxClasses; c++) {

					if (TeacherSchedule[a][b][c] != null) {

						tvark.addTag(SubjectSchedule[a][b][c].getSubjCode()
								+ " - " + GroupSchedule[a][b][c] + " - "
								+ StudentCount[a][b][c]);
						// c++;
					}
				}
				tvark.endRow();
				b++;
			}
		}
		tvark.close();
	}
//---------------------------------------------
	public void printTeachersScreen() {
		for (int a = 0; a < 5; a++) {
			System.out.println("========================================= "
					+ (a + 1) + "-dienis ========");

			for (int b = 0; b < lpd; b++) {
				System.out.println("------------------------------ " + (b + 1)
						+ "-oji pamoka ----");

				for (int c = 0; TeacherSchedule[a][b][c] != null; c++) {

					if (TeacherSchedule[a][b][c] != null) {
						System.out.print(TeacherSchedule[a][b][c] + "; ");

					}
				}
				System.out.println();

			}
		}
	}
//--------------------------------------------------------
	public void printRoomsScreen() {
		for (int a = 0; a < 5; a++) {
			System.out.println("========================================= "
					+ (a + 1) + "-dienis ========");

			for (int b = 0; b < lpd; b++) 
			{
				System.out.println("------------------------------ " + (b + 1)
						+ "-oji pamoka ----");
				for (int c = 0; RoomSchedule[a][b][c] != null; c++) 
				{
					if (RoomSchedule[a][b][c] != null) {
						System.out.print(RoomSchedule[a][b][c].getRoom() + "; ");
					}
				}
				System.out.println();
			}
		}
	}	
//---------------------------------------------------	
	
	public void printStudentXML(Student inStud,String folder1) {
		String DayNames [] = new String [5];
		DayNames[0] = "Monday";
		DayNames[1] = "Tuesday";
		DayNames[2] = "Wednesday";
		DayNames[3] = "Thursday";
		DayNames[4] = "Friday";
		XmlFileOut tvark = new XmlFileOut(folder+"/"+folder1+"/" + inStud.getName()
				+ ".xml", rootdir);
		for (int day = 0; day < 5; day++) {
			tvark.startRow();
			tvark.addTag("");
			tvark.endRow();
			tvark.startRow();
			tvark.addTag((day + 1) + "-dienis / (" +DayNames[day]+")" );
			tvark.endRow();
			//for (int lesson = 0; lesson < lpd; lesson++) {
			int lesson = 0;
			while (SubjectSchedule[day][lesson][0] != null){
				tvark.startRow();
				tvark.addTag("(Nr.: "+(lesson + 1) + ") ->");
				//tvark.startRow(); 
				//tvark.addTag((lesson + 1) + "-oji pamoka");
				for (int parallel = 0; parallel < maxClasses; parallel++) {
					for (int stud = 0; stud < StudentSchedule[day][lesson][parallel].length; stud++) {
						if (StudentSchedule[day][lesson][parallel][stud] != null
								&& StudentSchedule[day][lesson][parallel][stud].getName()
								.equals(inStud.getName())) { 

							tvark.addTag(SubjectSchedule[day][lesson][parallel]
									.getSubjCode());
							// c++;
						}
					}
				}
				tvark.endRow();
				lesson++;
			}
		}
		tvark.close();
	}
//-------------------------------------------------------	
	public void printTeacherXML(Teacher inStud, String folder1) {
		String DayNames [] = new String [5];
		DayNames[0] = "Monday";
		DayNames[1] = "Tuesday";
		DayNames[2] = "Wednesday";
		DayNames[3] = "Thursday";
		DayNames[4] = "Friday";
		XmlFileOut tvark = new XmlFileOut(folder+"/"+folder1+"/" + inStud.getName()
				+ ".xml", rootdir);
		for (int day = 0; day < 5; day++) {
			tvark.startRow();
			tvark.addTag("");
			tvark.endRow();
			tvark.startRow();
			tvark.addTag((day + 1) + "-dienis / (" +DayNames[day]+")" );
			tvark.endRow();
			//for (int lesson = 0; lesson < lpd; lesson++) {
			int lesson = 0;
			while (SubjectSchedule[day][lesson][0] != null){
				tvark.startRow();
				tvark.addTag("(Nr.: "+(lesson + 1) + ") ->");
				//tvark.startRow();
				//tvark.addTag((lesson + 1) + "-oji pamoka");

				for (int stud = 0; stud < StudentSchedule[day][lesson].length; stud++) {
					if (TeacherSchedule[day][lesson][stud] != null
							&& TeacherSchedule[day][lesson][stud].getName()
							.equals(inStud.getName())) {

						tvark.addTag(SubjectSchedule[day][lesson][stud]
								.getSubjCode());
					}
				}
				tvark.endRow();
				lesson++;
			}
		}
		tvark.close();
	}
//------------------------------------------------------------------
	public void printTeachersXML(String name) {
		String DayNames [] = new String [5];
		DayNames[0] = "Monday";
		DayNames[1] = "Tuesday";
		DayNames[2] = "Wednesday";
		DayNames[3] = "Thursday";
		DayNames[4] = "Friday";
		XmlFileOut tvark = new XmlFileOut(folder+"/"+name+".xml", rootdir);
		for (int a = 0; a < 5; a++) {
			tvark.startRow();
			tvark.addTag("");
			tvark.endRow();
			tvark.startRow();
			tvark.addTag((a + 1) + "-dienis / (" +DayNames[a]+")" );
			tvark.endRow();
			//for (int b = 0; b < lpd; b++) {
			int b = 0;
			while (SubjectSchedule[a][b][0] != null){
				tvark.startRow();
				tvark.addTag("(Nr.: "+(b + 1) + ") ->");
				//tvark.startRow();
				//tvark.addTag((b + 1) + "-oji pamoka");
				for (int c = 0; c < maxClasses; c++) {

					if (TeacherSchedule[a][b][c] != null) {

						tvark.addTag(TeacherSchedule[a][b][c].getName());
						// c++;
					}
				}
				tvark.endRow();
				b++;
			}
		}
		tvark.close();
	}
//---------------------------------------------------------------
	public void printRoomXML(Room in, String folder1) 
	{
		String DayNames [] = new String [5];
		DayNames[0] = "Monday";
		DayNames[1] = "Tuesday";
		DayNames[2] = "Wednesday";
		DayNames[3] = "Thursday";
		DayNames[4] = "Friday";
	//	String kab = Integer.toString(in.getRoom());
	
		XmlFileOut tvark = new XmlFileOut(folder+"/"+folder1+"/" +Integer.toString(in.getRoom()) + ".xml", rootdir);
		for (int day = 0; day < 5; day++) 
		{
			tvark.startRow();
			tvark.addTag("");
			tvark.endRow();
			tvark.startRow();
			tvark.addTag((day + 1) + "-dienis / (" +DayNames[day]+")" );
			tvark.endRow();

			int lesson = 0;
			while (SubjectSchedule[day][lesson][0] != null){
				tvark.startRow();
				tvark.addTag("(Nr.: "+(lesson + 1) + ") ->");

				for (int s = 0; s < RoomSchedule[day][lesson].length; s++) 
				{				
					if (RoomSchedule[day][lesson][s] != null
							&& RoomSchedule[day][lesson][s].getRoom()
							==(in.getRoom())) {

						tvark.addTag(SubjectSchedule[day][lesson][s]
								.getSubjCode());
					}
				}
				tvark.endRow();
				lesson++;
			}
		}
		tvark.close();
	}
//---------------------------------------------------	
	public void printRoomXML(String name) 
	{
		String DayNames [] = new String [5];
		DayNames[0] = "Monday";
		DayNames[1] = "Tuesday";
		DayNames[2] = "Wednesday";
		DayNames[3] = "Thursday";
		DayNames[4] = "Friday";
		XmlFileOut tvark = new XmlFileOut(folder+"/"+name+".xml", rootdir);
		for (int a = 0; a < 5; a++) 
		{
			tvark.startRow();
			tvark.addTag("");
			tvark.endRow();
			tvark.startRow();
			tvark.addTag((a + 1) + "-dienis / (" +DayNames[a]+")" );
			tvark.endRow();
			int b = 0;
			while (SubjectSchedule[a][b][0] != null)
			{
				tvark.startRow();
				tvark.addTag("(Nr.: "+(b + 1) + ") ->");

				for (int c = 0; c < maxClasses; c++) 
				{
					if (RoomSchedule[a][b][c] != null) 
					{
						tvark.addTag(Integer.toString(RoomSchedule[a][b][c].getRoom()));
						// c++;
					}
				}
				tvark.endRow();
				b++;
			}
		}
		tvark.close();
	}
//---------------------------------------------------------------
	public void getGroups() {
		String groups = null;
		String temp = null;
		for (int day = 0; day < 5; day++) {
			for (int less = 0; less < lpd; less++) {
				for (int clas = 0; clas < maxClasses; clas++) {
					temp = null;
					if (StudentSchedule[day][less][clas][0] != null)
						temp = StudentSchedule[day][less][clas][0].getKlass();
					groups = temp;

					for (int stud = 1; stud < 30; stud++) {
						if (temp != null
								&& StudentSchedule[day][less][clas][stud] != null
								&& !temp
										.equals(StudentSchedule[day][less][clas][stud]
												.getKlass())) {

							temp = StudentSchedule[day][less][clas][stud]
									.getKlass();
							groups = groups + ";" + temp;
						}
					}

					GroupSchedule[day][less][clas] = groups;

				}
			}
		}
	}
//--------------------------------------------------------------------
	public Schedule clone() {

		Schedule temp = new Schedule();
		temp.data = this.data;
		temp.teachers = this.teachers.clone();
		temp.students = this.students.clone();
		temp.GroupSchedule = this.GroupSchedule.clone();
		temp.lpd = this.lpd;
		temp.maxClasses = this.maxClasses;
		temp.StudentCount = this.StudentCount.clone();
		temp.StudentSchedule = this.StudentSchedule.clone();
		temp.SubjectSchedule = this.SubjectSchedule.clone();
		temp.TeacherSchedule = this.TeacherSchedule.clone();
		temp.RoomSchedule = this.RoomSchedule.clone();
		temp.TL = this.TL;
		temp.placeholder = this.placeholder.clone();
		temp.folder =this.folder;
		temp.rootdir =this.rootdir; 
		return temp;

	}
//---------------------------------------------------------------
	public void setPlaceholder(PointL point) 
	{
		placeholder[point.getX()][point.getY()][point.getZ()] = true;
	}

	public boolean isPlaceholder(PointL point) 
	{
		return placeholder[point.getX()][point.getY()][point.getZ()];
	}

	public void unsetPlaceholder(PointL point) 
	{
		placeholder[point.getX()][point.getY()][point.getZ()] = false;
	}

//-----------
	//public void setPlaceholderSt(PointSt point) 
//	{
//		placeholderSt[point.getX()][point.getY()][point.getZ()][point.getS()] = true;
//	}

//	public boolean isPlaceholderSt(PointSt point) 
//	{
//		return placeholderSt[point.getX()][point.getY()][point.getZ()][point.getS()];
//	}

//	public void unsetPlaceholderSt(PointSt point) 
//	{
//		placeholderSt[point.getX()][point.getY()][point.getZ()][point.getS()] = false;
//	}
	
//--------------------------------------------------------------------	
	
	
	public ScheduleData getData() {
		return data;
	}

	public int getLpd() {
		return lpd;
	}

	public int getMaxClasses() {
		return maxClasses;
	}

	public String getFolder() {
		return folder;
	}

	public void setData(ScheduleData data) {
		this.data = data;
	}

	public String getRootDir() {
		return rootdir;
	}

}
