using System;
using System.IO;
using System.Collections.Generic;
using System.Xml;

using OpenTK;
using OpenTK.Graphics;
using OpenTK.Graphics.OpenGL;

using PlanetExpress.Tools;

namespace PlanetExpress {	
	
	public class NewMissionScreenClass : CustomGameScreenClass {
		private string LvlFileName;
		private XmlDocument LvlInfo;		
		private string Info(string s) {			
			return LvlInfo.SelectSingleNode("mission").SelectSingleNode(s).InnerText;
		}
		
		private int ActiveDlgIndex = 0;
		private XmlNodeList Dlgs;
		private  void LoadDlg(XmlNode n) {
			//загружаем персонажа
			PorfessorTex = TexMan.LoadTexture(
			                               GameApp.GameFileName("data", "background",
		                                                       n.SelectSingleNode("person").InnerText));
			//Загружаем текст
			List<string> lst = new List<string>();
			foreach (XmlNode l in n.SelectNodes("line")) {
				lst.Add(l.InnerText);
			}
			LvlDesc = lst.ToArray();			
			//музыка
			foreach (XmlNode snd in n.SelectNodes("loop")) {
				if (snd.InnerText=="")
					GameApp.SndStopLoop();
				else
					GameApp.SndPlayLoop(snd.InnerText);
			}
			//звуки
			foreach (XmlNode snd in n.SelectNodes("snd")) {				
				GameApp.SndPlay(snd.InnerText);
			}
						
		}
		
		private bool DlgNext() {
			if (ActiveDlgIndex < Dlgs.Count-1) {
				ActiveDlgIndex++;
				LoadDlg(Dlgs[ActiveDlgIndex]);
				return true;
			} else {
				return false;	
			}
		}
		
		private PlanetRenderClass Planet;
		private TexClass PorfessorTex;
		private TexClass Item;
		
		private string LvlTitle;
		private string[] LvlDesc;
		
		public NewMissionScreenClass(string lvl) {
			LvlInfo = new XmlDocument();
			LvlInfo.Load(GameApp.GameFileName("data", "levels", lvl));
			
			TexClass PlanetTex = TexMan.LoadTexture(
			                              GameApp.GameFileName("data", "background", Info("planet"))
			                               );
			Planet = new PlanetRenderClass(PlanetTex, 256);
			
			Dlgs = LvlInfo.SelectSingleNode("mission").SelectNodes("dlg");
						
			                               
			Item = TexMan.LoadTexture(
			                               GameApp.GameFileName("data", "items", Info("item"))
			                               );
			LvlTitle = Info("title");
			LoadDlg(Dlgs[0]);			
		}
		
		public override void Action (double time) {
		}

		public override void Draw () {
			
			GL.ClearColor(0f, 0f, 0f, 1f);
			GL.Clear(ClearBufferMask.ColorBufferBit|
			         ClearBufferMask.DepthBufferBit);
						
			GameApp.Stars.Render();
			int t = Environment.TickCount;
					
			
			GL.Color3(new float[]{1f, 1f, 1f});
			Planet.Render(   GameApp.ScreenWidth - Planet.Size/3*2 + (float)Math.Sin(t/1000f)*10,
			                 GameApp.ScreenHeight - Planet.Size/3*2 + (float)Math.Cos(t/1000f)*10);			
			GL.Color3(new float[]{1f, 1f, 1f});			
			
			if (PorfessorTex != null)
				PorfessorTex.Render(8, GameApp.ScreenHeight - PorfessorTex.Height);
			
			//Item
			if (Item != null)
			Item.Render(
			            GameApp.ScreenWidth-4-Item.Width,
			            40);
			
			//Text
			GL.Color3(new float[]{1f, 1f, 1f});			
			GameApp.FontRenderBig.Render(LvlTitle, 8, 0, 0);
			
			int top = 0;
			if (Item != null)
			foreach (string l in LvlDesc) {
				float w = 
					top > Item.Height/2 ?
						GameApp.ScreenWidth-4 :
						GameApp.ScreenWidth-16-Item.Width;
				top += (int)GameApp.FontRender.Render( l, 8, 40 + top, w);
			}
		}
		
		public override void MouseDown (object sender, OpenTK.Input.MouseButtonEventArgs e) {
			if (!DlgNext()) DoSwithToScreen(new MissionScreenClass(
			                                                       LvlInfo.SelectSingleNode("mission").
			                                                       SelectSingleNode("lvl"),
			                                						LvlInfo.SelectSingleNode("mission").
			                                                       SelectSingleNode("next").InnerText)
			                                );
		}


	}
}
