using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;

namespace ___C_File_Editor
{
  public class HeaderData
  {
    public HeaderData(uint Start, byte[] bin)
    {
      this.Start = Start;
      this.Offsets = new List<uint>(0);
      this.Sizes = new List<uint>(0);

      char[] charsToTrim = { '\0' };
      this.Name = Encoding.ASCII.GetString(bin, (int)Start, 8).TrimEnd(charsToTrim);
      this.Flg1 = BitConverter.ToUInt32(bin, (int)Start + 0x08);
      this.HSize = BitConverter.ToUInt32(bin, (int)Start + 0x0C);
      this.Size = BitConverter.ToUInt32(bin, (int)Start + 0x10);
      this.Count1 = BitConverter.ToUInt32(bin, (int)Start + 0x14);
      this.Count2 = BitConverter.ToUInt32(bin, (int)Start + 0x18);
      this.Count3 = BitConverter.ToUInt32(bin, (int)Start + 0x1C);
      this.Offset1 = BitConverter.ToUInt32(bin, (int)Start + 0x20);
      this.Offset2 = BitConverter.ToUInt32(bin, (int)Start + 0x24);
      this.Offset3 = BitConverter.ToUInt32(bin, (int)Start + 0x28);
      this.Flg2 = BitConverter.ToUInt32(bin, (int)Start + 0x2C);

      for (int i = 0; i < this.Count1; i++)
      {
        this.Offsets.Add(BitConverter.ToUInt32(bin, (int)(Start + this.Offset1 + (i * 0x04))));
      }

      if (this.Offset2 > 0)
      {
        for (int i = 0; i < this.Count1; i++)
        {
          this.Sizes.Add(BitConverter.ToUInt32(bin, (int)(Start + this.Offset2 + (i * 0x04))));
        }
      }
    }

    public uint Start { get; set; }
    public string Name { get; set; }
    public uint Flg1 { get; set; }
    public uint HSize { get; set; }
    public uint Size { get; set; }
    public uint Count1 { get; set; }
    public uint Count2 { get; set; }
    public uint Count3 { get; set; }
    public uint Offset1 { get; set; }
    public uint Offset2 { get; set; }
    public uint Offset3 { get; set; }
    public uint Flg2 { get; set; }

    public List<uint> Offsets { get; private set; }
    public List<uint> Sizes { get; private set; }
  }

  public class CData
  {
    public CData(HeaderData h, byte[] bin, CheckBox[] cb)
    {
      this.HType = new bool[8];

      this.Alpha152 = new float[0];
      this.Unknown = false;
      if (bin[h.Offsets[0]] == 0)
      {

      }
      else if (bin[h.Offsets[0]] == 1)
      {
        int count = 0;
        while (count < 8 && h.Offsets[0] + (8 * count) < bin.Length && bin[h.Offsets[0] + (8 * count)] == 1)
        {
          cb[bin[h.Offsets[0] + (count * 8) + 4]].IsChecked = true;
          count++;
        }
        if (h.Offsets[0] + (8 * count) < h.Size && bin[h.Offsets[0] + (8 * count)] == 10)
        {
          this.hideGlasses = true;
        }
      }
      else if (bin[h.Offsets[0]] == 4)
      {
        this.Alpha152 = new float[9];
        int count = 0;
        while (count < 8 && bin[h.Offsets[0] + 4 + (count * 8)] == 1)
        {
          cb[bin[h.Offsets[0] + (count * 8) + 8]].IsChecked = true;
          count++;
        }
        if (bin[h.Offsets[0] + 4 + (count * 8)] == 5)
        {
          for (int i = 0; i < Alpha152.Length; i++)
          {
            Alpha152[i] = BitConverter.ToSingle(bin, (int)h.Offsets[0] + 8 + (count * 8) + (4 * i));
          }
        }
      }
      else if (bin[h.Offsets[0]] == 10)
      {
        this.hideGlasses = true;
      }
      else
      {
        Unknown = true;
      }

    }

    public bool[] HType { get; set; }
    public float[] Alpha152 { get; set; }
    public bool hideGlasses { get; set; }
    public bool Unknown { get; set; }
  }

}
