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

namespace FPGACoder.ViewModel
{
    public class RegisterViewModel : FPGAPropertyViewModel
    {

        private NIFPGA.lvbitx.Register register;
        private RegisterViewModel(NIFPGA.lvbitx.Register register)
        {
            this.register = register;
            PropertyName = register.Name;
        }
        static RegisterViewModel()
        {

        }

        public override string TargetName => register.Name;
        public static RegisterViewModel GetRegisterViewModel(NIFPGA.lvbitx.Register register,string propertyname="")
        {
            RegisterViewModel vm = new RegisterViewModel(register);
            if(!string.IsNullOrEmpty(propertyname))
            {
                vm.PropertyName = propertyname;
            }
            return vm;
        }
        public override string PropertyString => GetPropertyString() ;
        private string GetPropertyString()
        {
            string protype = GetProType();
            string proname = $"{(string.IsNullOrEmpty(PropertyName) ? TargetName : PropertyName)}";
            return $"private {protype} _{proname};\r\n /// <summary> \r\n///{TargetName}\r\n/// </summary>\r\n[AllowNull]\r\npublic {protype} {proname} \r\n" + "{\r\nget{\r\nif(_"+proname+"==null)\r\n{\r\n_"+proname+$"=({protype})fpga.Properties[\"{TargetName}\"];"+"\r\n}\r\nreturn _"+proname+";\r\n}\r\n}";
        }
    private string GetProType()
        {
            
            string value = "";
            switch (register.Datatype)
            {
                case NIFPGA.lvbitx.Datatype.Boolean:
                    value = "FPGAProperty<bool>";
                    break;
                case NIFPGA.lvbitx.Datatype.Int8:
                    value = "FPGAProperty<sbyte>";
                    break;
                case NIFPGA.lvbitx.Datatype.Uint8:
                    value = "FPGAProperty<byte>";
                    break;
                case NIFPGA.lvbitx.Datatype.Int16:
                    value = "FPGAProperty<short>";
                    break;
                case NIFPGA.lvbitx.Datatype.Uint16:
                    value = "FPGAProperty<ushort>";
                    break;
                case NIFPGA.lvbitx.Datatype.Int32:
                    value = "FPGAProperty<int>";
                    break;
                case NIFPGA.lvbitx.Datatype.Uint32:
                    value = "FPGAProperty<uint>";
                    break;
                case NIFPGA.lvbitx.Datatype.Int64:
                    value = "FPGAProperty<long>";
                    break;
                case NIFPGA.lvbitx.Datatype.Uint64:
                    value = "FPGAProperty<ulong>";
                    break;
                case NIFPGA.lvbitx.Datatype.Float:
                    value = "FPGAProperty<float>";
                    break;
                case NIFPGA.lvbitx.Datatype.Double:
                    value = "FPGAProperty<double>";
                    break;
                case NIFPGA.lvbitx.Datatype.FXP:
                    value = "FPGAFXPProperty";
                    break;
                case NIFPGA.lvbitx.Datatype.Array:
                    switch(register.ArrayValueType)
                    {
                        case NIFPGA.lvbitx.Datatype.Boolean:
                            value = "FPGAArrayProperty<bool>";
                            break;
                        case NIFPGA.lvbitx.Datatype.Int8:
                            value = "FPGAArrayProperty<sbyte>";
                            break;
                        case NIFPGA.lvbitx.Datatype.Uint8:
                            value = "FPGAArrayProperty<byte>";
                            break;
                        case NIFPGA.lvbitx.Datatype.Int16:
                            value = "FPGAArrayProperty<short>";
                            break;
                        case NIFPGA.lvbitx.Datatype.Uint16:
                            value = "FPGAArrayProperty<ushort>";
                            break;
                        case NIFPGA.lvbitx.Datatype.Int32:
                            value = "FPGAArrayProperty<int>";
                            break;
                        case NIFPGA.lvbitx.Datatype.Uint32:
                            value = "FPGAArrayProperty<uint>";
                            break;
                        case NIFPGA.lvbitx.Datatype.Int64:
                            value = "FPGAArrayProperty<long>";
                            break;
                        case NIFPGA.lvbitx.Datatype.Uint64:
                            value = "FPGAArrayProperty<ulong>";
                            break;
                        case NIFPGA.lvbitx.Datatype.Float:
                            value = "FPGAArrayProperty<float>";
                            break;
                        case NIFPGA.lvbitx.Datatype.Double:
                            value = "FPGAArrayProperty<double>";
                            break;
                        case NIFPGA.lvbitx.Datatype.FXP:
                            value = "FPGAArrayFXPProperty";
                            break;
                    }
                    break;
            }
            return value;
        }
    }
}
