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

namespace PTMedicalInsurance.Common
{
    public class FastReportFunction
    {
        public static object moneyToChinese(object money)
        {
            try
            {
                double currency = Double.Parse(string.Format("{0}",money));
                return numberToChinese(currency);
            }
            catch (Exception e)
            {
                return money;
            }
        }

        private static string numberToChinese(double money)
        {
            try
            {
                string s = money.ToString("#L#E#D#C#K#E#D#C#J#E#D#C#I#E#D#C#H#E#D#C#G#E#D#C#F#E#D#C#.0B0A");
                string d = Regex.Replace(s, @"((?<=-|^)[^1-9]*)|((?'z'0)[0A-E]*((?=[1-9])|(?'-z'(?=[F-L\.]|$))))|((?'b'[F-L])(?'z'0)[0A-L]*((?=[1-9])|(?'-z'(?=[\.]|$))))", "${b}${z}");
                return Regex.Replace(d, ".", delegate (Match m) { return "负元空零壹贰叁肆伍陆柒捌玖空空空空空空空分角拾佰仟万億兆京垓秭穰"[m.Value[0] - '-'].ToString(); });
            }
            catch (Exception ex)
            {
                return money.ToString();
            }
        }

        public static void Register()
        {
            
            Type funType = typeof(FastReportFunction);

            MethodInfo method = funType.GetMethod("moneyToChinese");
            if(!FastReport.Utils.RegisteredObjects.IsTypeRegistered(funType) && method != null)
            {
                FastReport.Utils.RegisteredObjects.Add(funType, "Functions,CustomFunctions",0x34);
                FastReport.Utils.RegisteredObjects.AddFunction(method, "CustomFunctions");
            }
            
        }
    }
}