// Warning: Some assembly references could not be resolved automatically. This might lead to incorrect decompilation of some parts,
// for ex. property getter/setter access. To get optimal decompilation results, please manually add the missing references to the list of loaded assemblies.
// EdenAutoMorpherScript, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null
// Eden.AutoMorpher.BoneCorrespondenceUtil
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;

public class BoneCorrespondenceUtil
{
    private char[] nameDelims = new char[8] { ' ', '-', '_', ':', '.', '|', '\\', '/' };

    public float ComputeDistanceScore(float d, float near, float mid, float far)
    {
        if (d < near)
        {
            return 120f;
        }
        if (d < mid)
        {
            return 90f;
        }
        if (d < far)
        {
            return 50f;
        }
        return 0f;
    }

    public float ComputeRotationScore(float ang, float RotTolDeg)
    {
        return Mathf.Clamp01(1f - ang / RotTolDeg);
    }

    public float ComputeNameScore(string bodyName, string bodyPath, string clothesName, string clothesPath)
    {
        string text = this.StripDigits(this.NormalizeName(bodyName));
        string text2 = this.StripDigits(this.NormalizeName(clothesName));
        string text3 = this.StripDigits(this.NormalizePath(bodyPath));
        string text4 = this.StripDigits(this.NormalizePath(clothesPath));
        if (!string.IsNullOrEmpty(text3) && text3 == text4)
        {
            return 80f;
        }
        if (!string.IsNullOrEmpty(text) && text == text2)
        {
            return 70f;
        }
        if (!string.IsNullOrEmpty(text) && !string.IsNullOrEmpty(text2) && (text.Contains(text2) || text2.Contains(text)))
        {
            return 40f;
        }
        return this.SubsequenceCoverage(bodyName, clothesName) * 10f;
    }

    private float SubsequenceCoverage(string bodyRaw, string clothesRaw)
    {
        string text = this.StripDigits(this.NormalizeName(bodyRaw));
        string text2 = this.StripDigits(this.NormalizeName(clothesRaw));
        if (string.IsNullOrEmpty(text) || string.IsNullOrEmpty(text2))
        {
            return 0f;
        }
        return (float)this.SubNameMatchBest(text, text2) / (float)text.Length;
    }

    private int SubNameMatchBest(string a, string b, int minMatchCount = 3)
    {
        if (string.IsNullOrEmpty(a) || string.IsNullOrEmpty(b))
        {
            return 0;
        }
        if (a.Length < minMatchCount || b.Length < minMatchCount)
        {
            return 0;
        }
        int num = 0;
        for (int i = 0; i <= a.Length - minMatchCount; i++)
        {
            int num2 = this.SubNameMatch(a, i, b, minMatchCount);
            if (num2 > num)
            {
                num = num2;
            }
        }
        return num;
    }

    private int SubNameMatch(string a, int startA, string b, int minMatchCount = 3)
    {
        int length = a.Length;
        int length2 = b.Length;
        int num = 0;
        int num2 = 0;
        int num3 = 0;
        int num4 = 0;
        for (int i = startA; i < length; i++)
        {
            for (int j = num3; j < length2; j++)
            {
                num3 = j + 1;
                if (a[i] == b[j])
                {
                    num4 = i + 1;
                    num2++;
                    if (num2 > num)
                    {
                        num = num2;
                    }
                    break;
                }
                if (num2 > num)
                {
                    num = num2;
                }
                num2 = 0;
            }
            if (num3 >= length2)
            {
                break;
            }
        }
        if (num2 > num)
        {
            num = num2;
        }
        if (num < minMatchCount)
        {
            return 0;
        }
        return num4 - startA;
    }

    public string GetHierarchyPath(Transform root, Transform t)
    {
        if (t == null)
        {
            return "";
        }
        Stack<string> stack = new Stack<string>();
        Transform transform = t;
        while (transform != null && transform != root)
        {
            stack.Push(transform.name);
            transform = transform.parent;
        }
        return string.Join("/", stack);
    }

    public string NormalizeName(string s)
    {
        if (string.IsNullOrEmpty(s))
        {
            return "";
        }
        s = s.ToLowerInvariant();
        s = s.Replace("left", "l").Replace("right", "r");
        char[] array = this.nameDelims;
        foreach (char c in array)
        {
            s = s.Replace(c.ToString(), "");
        }
        s = this.StripDigits(s);
        return s;
    }

    public string NormalizePath(string p)
    {
        if (string.IsNullOrEmpty(p))
        {
            return "";
        }
        return string.Join("/", (from x in p.Split('/')
                                 where !string.IsNullOrEmpty(x)
                                 select x).Select(this.NormalizeName));
    }

    public string StripDigits(string s)
    {
        if (string.IsNullOrEmpty(s))
        {
            return "";
        }
        StringBuilder stringBuilder = new StringBuilder(s.Length);
        foreach (char c in s)
        {
            if (!char.IsDigit(c))
            {
                stringBuilder.Append(c);
            }
        }
        return stringBuilder.ToString();
    }
}
