using System;

namespace arDev
{
    public partial class Narumi
    {
        public class ErrorFlag
        {
            private COMM.Flag _value { get; set; } = new COMM.Flag(16);
            public void SetValue(Int16 value) { this._value.writeValue(value); }
            public UInt16 Value
            {
                get
                {
                    return (UInt16)_value.Value;
                }
            }
            

            public bool GetValue(eflag idx)
            {
                return _value.Get((int)idx);
            }
            public bool GetChanged(eflag idx)
            {
                return _value.GetChanged((int)idx);
            }

            public bool Emergency { get { return GetValue(eflag.Emergency); } }
            public bool Overcurrent { get { return GetValue(eflag.Overcurrent); } }
            public bool Charger_run_error { get { return GetValue(eflag.Charger_run_error); } }
            public bool Charger_pos_error { get { return GetValue(eflag.Charger_pos_error); } }
            public bool line_out_error { get { return GetValue(eflag.line_out_error); } }
            public bool runerror_by_no_magent_line { get { return GetValue(eflag.runerror_by_no_magent_line); } }
            public bool controller_comm_error { get { return GetValue(eflag.controller_comm_error); } }
            public bool arrive_ctl_comm_error { get { return GetValue(eflag.arrive_ctl_comm_error); } }
            public bool door_ctl_comm_error { get { return GetValue(eflag.door_ctl_comm_error); } }
            public bool charger_comm_error { get { return GetValue(eflag.charger_comm_error); } }
            public bool cross_ctrl_comm_error { get { return GetValue(eflag.cross_ctrl_comm_error); } }
            public override string ToString()
            {
                //모든사태값을 탭으로 구분하여 문자를 생성한다
                var sb = new System.Text.StringBuilder();
                for (int i = 0; i < 16; i++)
                {
                    var def = Enum.IsDefined(typeof(eflag), i);
                    if (def)
                    {
                        var flag = (eflag)i;
                        var value = _value.Get(i);
                        sb.AppendLine($"[{i:00}][{flag}] : {value}");
                    }


                }
                return sb.ToString();
            }

            public string ToRtfString()
            {
                var sb = new System.Text.StringBuilder();
                sb.AppendLine(@"{\rtf1\ansi\deff0");
                sb.AppendLine(@"{\colortbl ;\red0\green0\blue255;}"); // Color 1 = Blue

                for (int i = 0; i < 16; i++)
                {
                    var def = Enum.IsDefined(typeof(eflag), i);
                    if (def)
                    {
                        var flag = (eflag)i;
                        var value = _value.Get(i);
                        string line = $"[{i:00}][{flag}] : {value}";

                        // : true가 포함된 줄은 파란색
                        if (value == true)
                        {
                            sb.AppendLine(@"\cf1 " + line + @"\cf0\line");
                        }
                        else
                        {
                            sb.AppendLine(line + @"\line");
                        }
                    }
                }

                sb.AppendLine("}");
                return sb.ToString();
            }
        }

    }


}
