//! Main map editor module.
//!
//! The entry point is responsible for maintaining the lifetime of the editor
//! state and human interactions with it, but is otherwise not permitted to
//! directly edit it.

use maraiah::{durandal::image::*,
              marathon::map,
              rozinante::{color::*, draw::*}};

impl MapEditor
{
   /// Creates a closed map editor.
   #[inline]
   pub const fn new_closed() -> Self
   {
      MapEditor::Closed
   }

   /// Opens the editor with a new empty map.
   #[inline]
   pub fn open_new(&mut self)
   {
      *self = MapEditor::Opened(MapEditorState::default());
   }

   /// Draws the screen for this editor state.
   pub fn draw<D, I>(&self, d: &D, im: &I)
      where D: DrawArea<NativeImage = I>,
            I: CacheImage
   {
      match self {
         MapEditor::Closed => {
            let tx_top = "Map Required To Proceed";
            let tx_bot = "CAS.qterm//CyberAcme Systems Inc.";

            let dw = d.w();
            let dh = d.h();
            let iw = im.w();
            let ih = im.h();

            d.clear(Color16::new(0, 0, 0));

            d.image((dw / 2 - iw / 2, dh / 2 - ih / 2), im);

            d.rect(Rect{x: 0, y: 0, w: dw, h: 18}, CR_DARK_RED);
            d.text((4, 14), tx_top, CR_RED);

            d.rect(Rect{x: 0, y: dh - 18, w: dw, h: 18}, CR_DARK_RED);
            d.text((4, dh - 4), tx_bot, CR_RED);
         }
         MapEditor::Opened(_st) => {
            d.clear(Color16::new(0, 0, 0));
         }
      }
   }

   /// Returns `true` if `self` is closed.
   #[allow(dead_code)]
   #[inline]
   pub fn is_closed(&self) -> bool
   {
      match self {
         MapEditor::Closed    => true,
         MapEditor::Opened(_) => false,
      }
   }

   /// Returns `true` if `self` is opened.
   #[allow(dead_code)]
   #[inline]
   pub fn is_opened(&self) -> bool
   {
      match self {
         MapEditor::Closed    => false,
         MapEditor::Opened(_) => true,
      }
   }
}

impl Default for Tool
{
   fn default() -> Tool {Tool::Points}
}

/// Copyable map state.
#[derive(Clone, Default)]
pub struct MapEditorStateBlock
{
   info: map::Minf,
}

/// The state of an opened map editor.
#[derive(Default)]
pub struct MapEditorState
{
   edit_stack: Vec<MapEditorStateBlock>,
   tool:       Tool,
}

/// An entire map editor, which may be opened or closed.
pub enum MapEditor
{
   Closed,
   Opened(MapEditorState),
}

/// A tool in the map editor.
pub enum Tool
{
   Points,
   Lines,
   Polygons,
}

// EOF
