using System.Text;
// using GreatFriends.ThaiBahtText;

namespace BMA.EHR.Recurit.Exam.Service.Extensions
{
    public static class IntegerExtension
    {
        // public static string ToThaiBahtText(this int value, bool appendBahtOnly)
        // {
        //     var decValue = (decimal)value;

        //     return decValue.ThaiBahtText(appendBahtOnly: appendBahtOnly);
        // }

        public static string ToThaiMonth(this int value)
        {
            switch (value)
            {
                case 1: return "มกราคม";
                case 2: return "กุมภาพันธ์";
                case 3: return "มีนาคม";
                case 4: return "เมษายน";
                case 5: return "พฤษภาคม";
                case 6: return "มิถุนายน";
                case 7: return "กรกฎาคม";
                case 8: return "สิงหาคม";
                case 9: return "กันยายน";
                case 10: return "ตุลาคม";
                case 11: return "พฤศจิกายน";
                case 12: return "ธันวาคม";
                default: return "";
            }
        }

        public static int ToThaiYear(this int value)
        {
            if (value < 2400)
                return value + 543;
            else return value;
        }

        public static int ToCeYear(this int value)
        {
            if (value >= 2400)
                return value - 543;
            else return value;
        }

        public static string ToNumericText(this int number)
        {
            return number.ToString("#,##0");
        }

        public static string ToThaiNumber(this string value)
        {
            string arabicNumbers = "0123456789";
            string thaiNumbers = "๐๑๒๓๔๕๖๗๘๙";
            StringBuilder result = new StringBuilder();
            foreach (char digit in value)
            {
                int index = arabicNumbers.IndexOf(digit);
                if (index >= 0)
                {
                    result.Append(thaiNumbers[index]);
                }
                else
                {
                    result.Append(digit);
                }
            }
            return result.ToString();
        }

        public static string NumberToThaiText(this float number)
        {
            int baht = (int)number;  // แยกส่วนบาท
            int satang = (int)Math.Round((number - baht) * 100); // คำนวณสตางค์ (ปัดเป็นจำนวนเต็ม)

            string bahtText = ConvertIntToThai(baht) + "บาท";
            string satangText = satang > 0 ? ConvertIntToThai(satang) + "สตางค์" : "ถ้วน";

            return bahtText + satangText;
        }

        public static string ConvertIntToThai(int number)
        {
            string[] unit = { "", "สิบ", "ร้อย", "พัน", "หมื่น", "แสน", "ล้าน" };
            string[] digit = { "ศูนย์", "หนึ่ง", "สอง", "สาม", "สี่", "ห้า", "หก", "เจ็ด", "แปด", "เก้า" };

            string result = "";
            string numStr = number.ToString();
            int len = numStr.Length;

            for (int i = 0; i < len; i++)
            {
                int num = numStr[i] - '0';
                int pos = len - i - 1;

                if (num != 0)
                {
                    if (pos == 1 && num == 1)
                        result += "สิบ";
                    else if (pos == 1 && num == 2)
                        result += "ยี่สิบ";
                    else if (pos == 0 && num == 1 && len > 1)
                        result += "เอ็ด";
                    else
                        result += digit[num] + unit[pos];
                }
            }

            return result;
        }
    }
}