using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace MasterProject
{
    class CallerClass
    {
        public static bool Call(Node root, int D, int JTR, int UBW, int DBW, int LR)
        {
            Node.Values = new int[5];
            Node.Values[0] = D;
            Node.Values[1] = JTR;
            Node.Values[2] = UBW;
            Node.Values[3] = DBW;
            Node.Values[4] = LR;
            return Calculate(root, true);
        }

        public static Node Load(string path)
        {
            try
            {
                XmlDocument doc = new XmlDocument();
                doc.Load(path);

                Node root;
                XmlElement element = doc.DocumentElement;
                recall(out root, element);
                return root;
            }
            catch
            {
                return null;
            }
        }

        public static bool Calculate(Node root, bool r)
        {
            if (root == null)
                return r;
            if (root.IsRight(r))
                return Calculate(root.Right, true);
            else
                return Calculate(root.Left, false);
        }

        public static void recall(out Node tem, XmlNode x)
        {
            if (x.ChildNodes.Count == 5)
            {
                tem = new Node(x.ChildNodes[0].InnerText, x.ChildNodes[1].InnerText, x.ChildNodes[2].InnerText);
                recall(out tem.Right, x.ChildNodes[3]);
                recall(out tem.Left, x.ChildNodes[4]);
            }
            else
                tem = null;
        }
    }
}