// Created with MonoDevelop
//
//    Copyright (C) 2023Hiperscan GmbH Dresden, Germany
//
//    This program is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program.  If not, see <http://www.gnu.org/licenses/>.


using System;

namespace Hiperscan.Common
{
    public class DebugWriter
    {
        public DebugWriter(bool debug = false)
        {
            this.Debug = debug;
        }

        public void WriteLine(string format = "", params object[] values)
        {
            if (this.Debug)
            {
                if (this.WriteShortException)
                    values = this.ConvertExToShortEx(values);

                Console.WriteLine(format, values);
            }
        }

        public void Write(string format = "", params object[] values)
        {
            if (this.Debug)
            {
                if (this.WriteShortException)
                    values = this.ConvertExToShortEx(values);

                format = format ?? String.Empty;
                Console.Write(format, values);
            }
        }

        private object[] ConvertExToShortEx(params object[] values)
        {
            if (values == null || values.Length == 0)
                return values;

            for (int i = 0; i < values.Length; i++)
            {
                if (values[i] is Exception)
                    values[i] = ((Exception)values[i]).Message;
            }

            return values;
        }

        public bool Debug { get; set; } = false;
        public bool WriteShortException { get; set; } = false;
    }
}
