#pragma once

using namespace System;
using namespace System::Windows::Forms;
using namespace System::Drawing;

namespace TicTacToe
{
    public enum class TTTStatus { Empty, Cross, Circle, Undefined };

    public ref class TTTField : Control
    {
    private:
       TTTStatus status;
       bool locked;
   
    public:
       TTTField(void) 
       {
          this->status = TTTStatus::Empty;
       }

       TTTField(TTTStatus status)
       {
          this->status = status;
          this->locked = false;
          this->Size = System::Drawing::Size(50, 50);

          SetStyle(ControlStyles::UserPaint, true);
          SetStyle(ControlStyles::ResizeRedraw, true);
       }


       /***********************************
        * Eigenschaften
        */
       property TTTStatus Status
       {
           TTTStatus get() 
           { 
              return this->status; 
           }
           void set(TTTStatus status) 
           { 
              if (!locked) 
                 this->status = status; 
           }
       }

       property bool Locked
       {
           bool get() 
           { 
              return this->locked; 
           }
           void set(bool locked) 
           { 
              this->locked = locked; 
           }
       }


       /***********************************
        * Größen-Management
        */

       virtual System::Drawing::Size GetPreferredSize(System::Drawing::Size proposedSize) override
       {
           return System::Drawing::Size(50, 50);
       }

       virtual property System::Drawing::Size MinimumSize 
       {
           System::Drawing::Size get() override
           {
               return System::Drawing::Size(50, 50);
           }
       }

       /***********************************
        * Zeichnen
        */
    protected:
       virtual void OnPaint(PaintEventArgs ^e) override
       {
          Control::OnPaint(e);
           
           Graphics ^gc = e->Graphics;
           Rectangle rect = ClientRectangle;
               
           gc->FillRectangle(Brushes::AntiqueWhite, rect);
           gc->DrawRectangle(Pens::Black, rect.X, rect.Y,
                                          rect.Width-1,rect.Height-1);
           switch (status)
           {
           case TTTStatus::Cross:
              gc->DrawLine(Pens::Black,
                           Point(rect.X + 5, rect.Y + 5),
                           Point(rect.X + rect.Width - 5, rect.Y + rect.Height - 5));
              gc->DrawLine(Pens::Black,
                           Point(rect.X + rect.Width - 5, rect.Y + 5),
                           Point(rect.X + 5, rect.Y + rect.Height - 5));
              break;
           case TTTStatus::Circle:
              gc->DrawEllipse(Pens::Black, 
                              rect.X + 5, rect.Y + 5,
                              rect.Width - 10, rect.Height - 10);
              break;
           }
           
       }

       /***********************************
        * Ereignisbehandlung
        */
       virtual void OnClick(EventArgs ^e) override
       {
           if (!this->locked)
           {
               this->status++;
               if (this->status == TTTStatus::Undefined)
                  this->status = TTTStatus::Empty;
                this->Refresh();
           }
           Control::OnClick(e);
       }
   };
}
