using System;
using System.Collections.Generic;
using System.Linq;

namespace AxCopilot.Services;

public static class IntentDetector
{
	public static class Categories
	{
		public const string Coding = "coding";

		public const string Translation = "translation";

		public const string Analysis = "analysis";

		public const string Creative = "creative";

		public const string Document = "document";

		public const string Math = "math";

		public const string General = "general";

		public static readonly string[] All = new string[7] { "coding", "translation", "analysis", "creative", "document", "math", "general" };
	}

	private static readonly Dictionary<string, (string Keyword, double Weight)[]> _keywords = new Dictionary<string, (string, double)[]>
	{
		["coding"] = new(string, double)[72]
		{
			("코드", 1.0),
			("함수", 1.0),
			("클래스", 1.0),
			("버그", 1.2),
			("디버그", 1.2),
			("리팩토링", 1.5),
			("컴파일", 1.2),
			("에러", 0.8),
			("구현", 0.9),
			("개발", 0.7),
			("테스트", 0.8),
			("빌드", 1.0),
			("배포", 0.8),
			("커밋", 1.2),
			("브랜치", 1.2),
			("머지", 1.0),
			("풀리퀘", 1.2),
			("변수", 1.0),
			("메서드", 1.2),
			("인터\ufffd\ufffd이스", 1.0),
			("타입", 0.6),
			("파라미터", 1.0),
			("반환", 0.8),
			("예외", 1.0),
			("스택", 0.9),
			("알고리즘", 1.2),
			("자료구조", 1.2),
			("정렬", 0.8),
			("재귀", 1.0),
			("루프", 0.9),
			("API", 1.0),
			("SDK", 1.0),
			("라이브러리", 0.8),
			("패키지", 0.8),
			("모듈", 0.8),
			("깃", 1.0),
			("레포", 1.0),
			("소스", 0.7),
			("프로그래밍", 1.0),
			("코딩", 1.2),
			("code", 1.0),
			("function", 1.0),
			("class", 0.8),
			("bug", 1.2),
			("debug", 1.2),
			("refactor", 1.5),
			("compile", 1.2),
			("error", 0.6),
			("implement", 1.0),
			("develop", 0.7),
			("test", 0.7),
			("build", 0.8),
			("deploy", 0.8),
			("commit", 1.2),
			("branch", 1.2),
			("merge", 1.0),
			("variable", 1.0),
			("method", 1.0),
			("interface", 0.8),
			("parameter", 1.0),
			("return", 0.6),
			("exception", 1.0),
			("stack", 0.7),
			("algorithm", 1.2),
			("syntax", 1.2),
			("runtime", 1.0),
			("compile", 1.0),
			("git", 1.2),
			("npm", 1.0),
			("pip", 1.0),
			("nuget", 1.0),
			("docker", 1.0)
		},
		["translation"] = new(string, double)[19]
		{
			("번역", 2.0),
			("영어로", 2.0),
			("한국어로", 2.0),
			("일본어로", 2.0),
			("중국어로", 2.0),
			("영문", 1.5),
			("국문", 1.5),
			("통역", 1.5),
			("원문", 1.2),
			("의역", 1.5),
			("직역", 1.5),
			("translate", 2.0),
			("English", 1.0),
			("Korean", 1.0),
			("Japanese", 1.0),
			("Chinese", 1.0),
			("translation", 2.0),
			("localize", 1.5),
			("localization", 1.5)
		},
		["analysis"] = new(string, double)[27]
		{
			("분석", 1.5),
			("요약", 1.5),
			("비교", 1.2),
			("장\ufffd\ufffd점", 1.5),
			("평가", 1.2),
			("검토", 1.0),
			("리뷰", 0.8),
			("통계", 1.2),
			("데이터", 0.8),
			("트렌드", 1.0),
			("인사이트", 1.2),
			("근거", 1.0),
			("원인", 0.8),
			("결론", 0.8),
			("핵심", 0.7),
			("analyze", 1.5),
			("summarize", 1.5),
			("compare", 1.2),
			("evaluate", 1.2),
			("review", 0.8),
			("statistics", 1.2),
			("data", 0.6),
			("trend", 1.0),
			("insight", 1.2),
			("pros", 1.0),
			("cons", 1.0),
			("conclusion", 0.8)
		},
		["creative"] = new(string, double)[25]
		{
			("작성", 0.8),
			("글쓰기", 1.5),
			("스토리", 1.5),
			("시", 1.2),
			("소설", 1.5),
			("에\ufffd\ufffd이", 1.5),
			("블로그", 1.2),
			("카피", 1.2),
			("슬로건", 1.5),
			("제목", 0.8),
			("아이디어", 1.0),
			("창작", 1.5),
			("묘사", 1.2),
			("대본", 1.5),
			("가사", 1.5),
			("story", 1.5),
			("poem", 1.5),
			("essay", 1.5),
			("blog", 1.2),
			("creative", 1.5),
			("slogan", 1.5),
			("copy", 0.8),
			("fiction", 1.5),
			("narrative", 1.2),
			("lyrics", 1.5)
		},
		["document"] = new(string, double)[27]
		{
			("보고서", 2.0),
			("문서", 1.2),
			("제안서", 2.0),
			("기획서", 2.0),
			("계획서", 1.8),
			("발표자료", 2.0),
			("프레젠테이션", 2.0),
			("양식", 1.5),
			("서식", 1.5),
			("템플릿", 1.2),
			("\ufffd\ufffd셀", 1.5),
			("워드", 1.2),
			("파워포인트", 1.5),
			("PDF", 0.8),
			("CSV", 1.0),
			("회의록", 2.0),
			("업무일지", 2.0),
			("주간보고", 2.0),
			("월간보고", 2.0),
			("report", 1.8),
			("document", 1.0),
			("proposal", 2.0),
			("presentation", 2.0),
			("template", 1.2),
			("spreadsheet", 1.5),
			("excel", 1.5),
			("memo", 1.2)
		},
		["math"] = new(string, double)[33]
		{
			("수학", 1.5),
			("계산", 1.2),
			("방정식", 2.0),
			("증명", 2.0),
			("미적분", 2.0),
			("통계", 1.0),
			("확률", 1.5),
			("행렬", 2.0),
			("벡터", 1.5),
			("미분", 2.0),
			("적분", 2.0),
			("함수", 0.5),
			("그래프", 0.8),
			("좌표", 1.5),
			("기하", 1.5),
			("삼각함수", 2.0),
			("로그", 1.0),
			("지수", 1.0),
			("급수", 2.0),
			("극한", 2.0),
			("math", 1.5),
			("calculate", 1.2),
			("equation", 2.0),
			("proof", 2.0),
			("calculus", 2.0),
			("probability", 1.5),
			("matrix", 2.0),
			("vector", 1.5),
			("derivative", 2.0),
			("integral", 2.0),
			("theorem", 2.0),
			("formula", 1.5),
			("algebra", 1.5)
		}
	};

	public static (string Category, double Confidence) Detect(string message)
	{
		if (string.IsNullOrWhiteSpace(message))
		{
			return (Category: "general", Confidence: 0.0);
		}
		string text = message.ToLowerInvariant();
		string[] collection = text.Split(new char[16]
		{
			' ', '\t', '\n', '\r', ',', '.', '!', '?', ';', ':',
			'(', ')', '[', ']', '{', '}'
		}, StringSplitOptions.RemoveEmptyEntries);
		HashSet<string> hashSet = new HashSet<string>(collection);
		Dictionary<string, double> dictionary = new Dictionary<string, double>();
		double num = 0.0;
		string key;
		foreach (KeyValuePair<string, (string, double)[]> keyword in _keywords)
		{
			keyword.Deconstruct(out key, out var value);
			string key2 = key;
			(string, double)[] array = value;
			double num2 = 0.0;
			int num3 = 0;
			(string, double)[] array2 = array;
			for (int i = 0; i < array2.Length; i++)
			{
				(string, double) tuple = array2[i];
				string item = tuple.Item1;
				double item2 = tuple.Item2;
				string text2 = item.ToLowerInvariant();
				if (IsKorean(item) ? text.Contains(text2) : hashSet.Contains(text2))
				{
					num2 += item2;
					num3++;
				}
			}
			dictionary[key2] = num2;
			if (num2 > num)
			{
				num = num2;
			}
		}
		if (num < 1.0)
		{
			return (Category: "general", Confidence: 0.0);
		}
		string item3 = "general";
		double num4 = 0.0;
		foreach (KeyValuePair<string, double> item4 in dictionary)
		{
			item4.Deconstruct(out key, out var value2);
			string text3 = key;
			double num5 = value2;
			if (num5 > num4)
			{
				num4 = num5;
				item3 = text3;
			}
		}
		double num6 = Math.Min(1.0, num4 / 4.0 + 0.3);
		double[] array3 = dictionary.Values.OrderByDescending((double s) => s).ToArray();
		if (array3.Length >= 2 && array3[1] > 0.0)
		{
			double num7 = array3[1] / array3[0];
			if (num7 > 0.7)
			{
				num6 *= 0.8;
			}
		}
		return (Category: item3, Confidence: Math.Round(num6, 2));
	}

	private static bool IsKorean(string text)
	{
		foreach (char c in text)
		{
			if (c >= '가' && c <= '힣')
			{
				return true;
			}
			if (c >= 'ㄱ' && c <= 'ㆎ')
			{
				return true;
			}
		}
		return false;
	}
}
