﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CSML;
using CSML.Graphics2D;
using CSML.UI;

namespace CSMLTest
{
    public class Checkbox : Element
    {
        string text;
        public string Text
        {
            get { return text; }
            set { text = value; Resize(); }
        }

        FontSize fontSize;
        public FontSize FontSize
        {
            get { return fontSize; }
            set { fontSize = value; Resize(); }
        }

        int padding = 8;
        public int Padding
        {
            get { return padding; }
            set { padding = value; Resize(); }
        }

        int boxSize = 0;
        public int BoxSize
        {
            get { return boxSize; }
            set { boxSize = value; Resize(); }
        }

        public bool Toggled;
        public int BoxCornerRadius = 4;
        public int BoxBorder = 2;
        
        public Checkbox(bool toggled, FontSize fontSize, string text) 
            : base(fontSize.LineHeight, fontSize.LineHeight, false, false)
        {
            Toggled = toggled;
            this.text = text;
            this.fontSize = fontSize;
            boxSize = Math.Max(24, fontSize.LineHeight);
            Resize();
            OnRender += Render;
        }

        private void Resize()
        {
            SizeX = boxSize + fontSize.GetWidth(ref text) + padding * 2 + 8;
            SizeY = boxSize + padding * 2;
            UpdateLayout();
        }
        
        private void Render()
        {
            Draw2D.Box(new RectangleI(Rect.X + padding, Rect.Y + padding, boxSize, boxSize), BoxCornerRadius, BoxBorder, 4, Color.White, Color.Black, 0xeeeeeeff);

            if (Toggled)
            {
                Draw2D.Line(new Vector2(Rect.X + padding + 6, Rect.Y + padding + boxSize / 2), new Vector2(Rect.X + padding + boxSize / 2, Rect.Y + padding + boxSize - 6), 4, Color.Green);
                Draw2D.Line(new Vector2(Rect.X + padding + boxSize / 2, Rect.Y + padding + boxSize - 6), new Vector2(Rect.X + padding + boxSize - 6, Rect.Y + padding + 6), 4, Color.Green);
            }
            
            Draw2D.TextLine(fontSize, ref text, new Vector2(Rect.X + padding + boxSize + 8, Rect.Y + padding + (boxSize - fontSize.LineHeight) / 2 + fontSize.Ascent), Color.Black);
        }


    }
}
