using Sandbox.ModAPI.Ingame;
using System.Collections.Generic;

namespace IngameScript
{
    public class Door
    {
        private MyGridProgram _p;
        private List<DoorHinge> _hinges;

        public bool Locked
        {
            get
            {
                foreach (DoorHinge hinge in _hinges)
                {
                    if (!hinge.Locked) return false;
                }
                return true;
            }
        }

        public Door(MyGridProgram p)
        {
            _p = p;
            _hinges = new List<DoorHinge>();
        }

        // Add a hinge to the door
        public void AddHinge(IMyMotorStator hinge)
        {
            _hinges.Add(new DoorHinge(_p, hinge));
        }

        public void OpenDoor()
        {
            foreach (DoorHinge hinge in _hinges)
            {
                hinge.OpenDoorHinge();
            }
        }

        public void CloseDoor()
        {
            foreach (DoorHinge hinge in _hinges)
            {
                hinge.CloseDoorHinge();
            }
        }

        // Process the door's movement. This will return true when the door is done moving.
        public bool Actuate()
        {
            bool done = true;
            foreach (DoorHinge hinge in _hinges)
            {
                if (!hinge.Actuate()) done = false;
            }
            return done;
        }
    }
}