package backend

import (
	"regexp"
	"strings"
)

var chineseSigilRankSuffixPattern = regexp.MustCompile(`^(.+?)(V|IV|III|II|I)(\+?)$`)

// normalizeChineseSigilItemName only fixes spacing before an item-rank suffix.
// A trailing plus without a Roman rank can be part of the game's actual name.
func normalizeChineseSigilItemName(name string) string {
	name = strings.TrimSpace(name)
	match := chineseSigilRankSuffixPattern.FindStringSubmatch(name)
	if len(match) == 0 {
		return name
	}
	return strings.TrimSpace(match[1]) + " " + match[2] + match[3]
}

// sigilCN 因子中文名映射
var sigilCN = map[string]string{
	"Attack Power V+":               "攻击力 V+",
	"Health V+":                     "体力 V+",
	"Critical Hit Rate V+":          "暴击率 V+",
	"Stun Power V+":                 "昏厥 V+",
	"Enmity V+":                     "背水 V+",
	"Stamina V+":                    "热血 V+",
	"Charged Attack V+":             "蓄力攻击 V+",
	"Linked Together V+":            "协同攻击 V+",
	"Throw V+":                      "投掷 V+",
	"Critical Damage V+":            "暴击伤害 V+",
	"Exploiter V+":                  "弱点攻击 V+",
	"Combo Finisher V+":             "连击收招 V+",
	"Concentrated Fire V+":          "集中炮火 V+",
	"Damage Cap V+":                 "伤害上限 V+",
	"Combo Booster V+":              "连击加成 V+",
	"Tyranny V+":                    "暴君 V+",
	"Lucky Charge V+":               "蓄力会心 V+",
	"Injury to Insult V+":           "弱化状态特效 V+",
	"Overdrive Assassin V+":         "Overdrive特效 V+",
	"Break Assassin V+":             "破防特效 V+",
	"Garrison V+":                   "坚守 V+",
	"Guts V+":                       "豪胆 V+",
	"Improved Guard V+":             "格挡性能 V+",
	"Guard Payback V+":              "格挡仇火 V+",
	"Improved Dodge+":               "躲避性能+",
	"Dodge Payback V+":              "躲避仇火 V+",
	"Improved Healing V+":           "回复性能 V+",
	"Regen V+":                      "自愈 V+",
	"Autorevive V+":                 "自动复活 V+",
	"Quick Cooldown V+":             "迅捷能力 V+",
	"Cascade V+":                    "怒涛 V+",
	"Uplift V+":                     "激昂 V+",
	"Potion Hoarder+":               "药水携带数+",
	"Skilled Assault V+":            "能力伤害 V+",
	"Aegis V+":                      "守护 V+",
	"Steel Nerves V+":               "坚持 V+",
	"Nimble Defense V+":             "凝神 V+",
	"Nimble Onslaught V+":           "明镜止水 V+",
	"Precise Resilience V+":         "抗争 V+",
	"Life on the Line V+":           "奋不顾身 V+",
	"Quick Charge V+":               "快速蓄力 V+",
	"Stronghold V+":                 "刚健 V+",
	"Power Hungry V+":               "修罗 V+",
	"War Elemental+":                "属性克制转换+",
	"Untouchable+":                  "躲避距离+",
	"Supplementary Damage V+":       "追击 V+",
	"Less Is More V+":               "身无长技 V+",
	"Head Start V+":                 "先发制人 V+",
	"Berserker V+":                  "穷寇心 V+",
	"Potent Greens+":                "万能药+",
	"Glass Cannon V+":               "刀上舞 V+",
	"Flight over Fight+":            "摇曳步 +",
	"Alpha+":                        "α秘纹+",
	"Beta+":                         "β秘纹+",
	"Gamma+":                        "γ秘纹+",
	"Greater Aegis V+":              "金刚 V+",
	"Auto Potion":                   "自动药水",
	"Seven-Star Boundary+":          "涯之七星+",
	"Two-Crown Boundary+":           "涯之二王+",
	"Ain+":                          "无态+",
	"Berserker Echo+":               "狂战士+",
	"Spartan Echo+":                 "斯巴达+",
	"Super Ultimate Perfect Dodge+": "超终极精准躲避+",

	// 角色专属
	//团长
	"Fearless Soul+":   "英勇之魂+",
	"Fearless Drive+":  "英勇神速+",
	"Fearless Spirit+": "英勇灵气+",
	"Fearless Heart+":  "英勇之心+",
	//卡塔利娜
	"Guardian's Awakening+":  "守护者之觉醒+",
	"Guardian's Conviction+": "守护者的决心+",
	"Guardian's Honor+":      "守护者的骄傲+",
	"Guardian's Warpath+":    "守护者的战气+",
	//牢卡姆
	"Helmsman's Awakening+":  "舵手之觉醒+",
	"Helmsman's Navigation+": "舵手的指引+",
	"Helmsman's Tenacity+":   "舵手的坚持+",
	"Helmsman's Warpath+":    "舵手的战气+",
	//io
	"Mage's Awakening+":  "魔法师之觉醒+",
	"Mage's Aspiration+": "魔法师的心愿+",
	"Mage's Savvy+":      "魔法师的伶俐+",
	"Mage's Warpath+":    "魔法师的战气+",
	//岁月老根
	"Veteran's Awakening+": "老兵之觉醒+",
	"Veteran's Insight+":   "老兵的智慧+",
	"Veteran's Vision+":    "老兵的天眼+",
	"Veteran's Warpath+":   "老兵的战气+",
	//植物人
	"Rose's Awakening+": "玫瑰之觉醒+",
	"Rose's Blooming+":  "玫瑰早绽+",
	"Rose's Profusion+": "玫瑰缤纷+",
	"Rose's Warpath+":   "玫瑰的战气+",
	//菲利
	"Phantasm's Awakening+": "幽幻之觉醒+",
	"Phantasm's Concord+":   "幽幻之应+",
	"Phantasm's Harmony+":   "幽幻之谊+",
	"Phantasm's Warpath+":   "幽幻的战气+",
	//玩双刀的
	"White Dragon's Awakening+": "白龙之觉醒+",
	"White Dragon's Oath+":      "白龙的誓约+",
	"White Dragon's Glory+":     "白龙的荣耀+",
	"White Dragon's Warpath+":   "白龙的战气+",

	//巴恩
	"Hero's Awakening+": "勇士之觉醒+",
	"Hero's Creed+":     "勇士的信念+",
	"Hero's Will+":      "勇士的毅力+",
	"Hero's Warpath+":   "勇士的战气+",
	//炎帝
	"Lord's Awakening+":  "王者之觉醒+",
	"Lord's Procession+": "王者行进+",
	"Lord's Ambition+":   "王者一心+",
	"Lord's Warpath+":    "王者的战气+",
	//齐格飞
	"Dragonslayer's Awakening+": "屠龙者之觉醒+",
	"Dragonslayer's Dominance+": "屠龙者的威猛+",
	"Dragonslayer's Ingenuity+": "屠龙者的才智+",
	"Dragonslayer's Warpath+":   "屠龙者的战气+",
	//豆丁
	"Holy Knight's Awakening+": "圣骑士之觉醒+",
	"Holy Knight's Luster+":    "圣骑士的剑辉+",
	"Holy Knight's Grandeur+":  "圣骑士的威光+",
	"Holy Knight's Warpath+":   "圣骑士的战气+",
	//水老头
	"Swordmaster's Awakening+": "变幻自如之觉醒+",
	"Swordmaster's Prowess+":   "变幻自如的迅刃+",
	"Swordmaster's Art+":       "变幻自如的妖剑士+",
	"Swordmaster's Warpath+":   "变幻自如的战气+",
	//乃刀
	"Butterfly's Awakening+": "斩姬之觉醒+",
	"Butterfly's Grace+":     "斩姬梦幻+",
	"Butterfly's Valor+":     "斩姬武艺+",
	"Butterfly's Warpath+":   "斩姬的战气+",
	//牛爷爷
	"Eternal Rage's Awakening+": "古今无双之觉醒+",
	"Eternal Rage's Mettle+":    "古今无双的气概+",
	"Eternal Rage's Ethos+":     "古今无双的强者+",
	"Eternal Rage's Warpath+":   "古今无双的战气+",
	//卡里奥斯特罗
	"Founder's Awakening+": "极致之觉醒+",
	"Founder's Strategy+":  "极致谋略+",
	"Founder's Truth+":     "极致真理+",
	"Founder's Warpath+":   "极致的战气+",
	//id
	"Versalis Soul+":       "异能之魂+",
	"Versalis Foundation+": "异能增长+",
	"Versalis Ignition+":   "异能战意+",
	"Versalis Heart+":      "异能之心+",
	//泽塔
	"Crimson's Awakening+": "真红之觉醒+",
	"Crimson's Clout+":     "真红烈焰+",
	"Crimson's Flight+":    "真红翔舞+",
	"Crimson's Warpath+":   "真红的战气+",
	//巴萨拉卡
	"Ebony's Awakening+": "冥暗之觉醒+",
	"Ebony's Presence+":  "冥暗刚刃+",
	"Ebony's Poise+":     "冥暗自若+",
	"Ebony's Warpath+":   "冥暗的战气+",
	//老七
	"Spirit Edge's Awakening+": "剑圣之觉醒+",
	"Spirit Edge's Rally+":     "剑圣的炼气+",
	"Spirit Edge's Fury+":      "剑圣的闪刃+",
	"Spirit Edge's Warpath+":   "剑圣的战气+",
	//二姐
	"Dark Huntress's Awakening+": "魔眼之觉醒+",
	"Dark Huntress's Volley+":    "魔眼的万箭+",
	"Dark Huntress's Surge+":     "魔眼的凛翔+",
	"Dark Huntress's Warpath+":   "魔眼的战气+",
	//sdf
	"Supreme Primarch's Awakening+": "天司长之觉醒+",
	"Supreme Primarch's Awe+":       "天司长的灵威+",
	"Supreme Primarch's Nimbus+":    "天司长的风雅+",
	"Supreme Primarch's Warpath+":   "天司长的战气+",

	// misc
	"Fortifying Vigor":                "强健甘露",
	"Instilling Vigor":                "修炼甘露",
	"Gilding Vigor":                   "幸运甘露",
	"Crabby Resonance":                "钳蟹的共鸣",
	"Crabvestment Returns":            "钳蟹的报恩",
	"Seven Net":                       "7net",
	"Crabmiration":                    "终极钳蟹因子",
	"SBA/Chain Burst Effect+ Common":  "奥义/连锁爆发效果+ 普通",
	"SBA/Chain Burst Effect+ Rare":    "奥义/连锁爆发效果+ 稀有",
	"SBA/Chain Burst Effect+ Epic":    "奥义/连锁爆发效果+ 史诗",
	"SBA/Chain Burst Effect+ Common+": "奥义/连锁爆发效果+ 普通+",
	"SBA/Chain Burst Effect+ Rare+":   "奥义/连锁爆发效果+ 稀有+",
	"SBA/Chain Burst Effect+ Epic+":   "奥义/连锁爆发效果+ 史诗+",
	"Stout Heart":                     "霸体",
	"Poison Resistance V":             "中毒抗性 V",
	"Burn Resistance V":               "灼热抗性 V",
	"Sandtomb Resistance V":           "泥沙抗性 V",
	"Glaciate Resistance V":           "冰冻抗性 V",
	"Dizzy Resistance V":              "昏迷抗性 V",
	"Paralysis Resistance V":          "麻痹抗性 V",
	"Skill Sealed Resistance V":       "能力封印抗性 V",
	"SBA Sealed Resistance V":         "奥义封印抗性 V",
	"Improved Dodge":                  "躲避性能",
	"Drain V":                         "HP吸收 V",
	"Drain+":                          "HP吸收+",
	"Potion Hoarder V":                "药水携带数 V",
	"Low Profile V":                   "匿踪 V",
	"Provoke V":                       "挑衅 V",
	"Fast Learner V":                  "获得经验值 V",
	"Rupie Tycoon V":                  "获得金币 V",
	"Slow Resistance V":               "缓速抗性 V",
	"Firm Stance V":                   "不动 V",
	"Blight Resistance V":             "灾祸抗性 V",
	"Steady Focus V":                  "蓄力强化 V",
	"Supplements V":                   "强化效果延长 V",
	"Natural Defenses":                "全弱化效果抗性",
	"Precise Wrath V":                 "怒发冲冠 V",
	"Window of Opportunity V":         "攻击机会 V",
	"Sigil Booster V":                 "因子强化 V",
	"Held Under Resistance V":         "水牢抗性 V",
	"Darkflame Resistance V":          "异能耐受 V",
	"Attack Down Resistance V":        "攻击DOWN抗性 V",
	"Defense Down Resistance V":       "防御DOWN抗性 V",
	"War Elemental":                   "属性克制转换",
	"Path to Mastery V":               "获得MSP V",
	"Untouchable":                     "躲避距离",
	"Potent Greens":                   "万能药",
	"Roll of the Die":                 "骰子剑",
	"Roll of the Die+":                "骰子剑+",
	"Flight over Fight":               "摇曳步",
	"Greater Aegis":                   "金刚",
}

// traitCN 特性中文名映射
var traitCN = map[string]string{
	"ATK":                          "攻击力",
	"HP":                           "体力",
	"Critical Hit Rate":            "暴击率",
	"Stun Power":                   "昏厥",
	"Enmity":                       "背水",
	"Stamina":                      "热血",
	"Charged Attack DMG":           "蓄力攻击",
	"Linked Together":              "协同攻击",
	"Throw DMG":                    "投掷",
	"Critical Hit DMG":             "暴击伤害",
	"Weak Point DMG":               "弱点攻击",
	"Combo Finisher DMG":           "连击收招",
	"Concentrated Fire":            "集中炮火",
	"DMG Cap":                      "伤害上限",
	"Combo Booster":                "连击加成",
	"Tyranny":                      "暴君",
	"Lucky Charge":                 "蓄力会心",
	"Injury to Insult":             "弱化状态特效",
	"Overdrive Assassin":           "Overdrive特效",
	"Break Assassin":               "破防特效",
	"Garrison":                     "坚守",
	"Guts":                         "豪胆",
	"Improved Guard":               "格挡性能",
	"Guard Payback":                "格挡仇火",
	"Improved Dodge":               "躲避性能",
	"Dodge Payback":                "躲避仇火",
	"Drain":                        "HP吸收",
	"Improved Healing":             "回复性能",
	"Regen":                        "自愈",
	"Autorevive":                   "自动复活",
	"Quick Cooldown":               "迅捷能力",
	"Cascade":                      "怒涛",
	"Uplift":                       "激昂",
	"Potion Hoarder":               "药水携带数",
	"Skilled Assault":              "能力伤害",
	"Aegis":                        "守护",
	"Steel Nerves":                 "坚持",
	"Nimble Defense":               "凝神",
	"Nimble Onslaught":             "明镜止水",
	"Precise Resilience":           "抗争",
	"Life on the Line":             "奋不顾身",
	"Quick Charge":                 "快速蓄力",
	"Stronghold":                   "刚健",
	"Power Hungry":                 "修罗",
	"War Elemental":                "属性克制转换",
	"Untouchable":                  "躲避距离",
	"Supplementary DMG":            "追击",
	"Catastrophe":                  "浩劫",
	"Less Is More":                 "身无长技",
	"Head Start":                   "先发制人",
	"Berserker":                    "穷寇心",
	"Potent Greens":                "万能药",
	"Glass Cannon":                 "刀上舞",
	"Flight over Fight":            "摇曳步",
	"Alpha":                        "α秘纹",
	"Beta":                         "β秘纹",
	"Gamma":                        "γ秘纹",
	"Greater Aegis":                "金刚",
	"Auto Potion":                  "自动药水",
	"Seven-Star Boundary":          "涯之七星",
	"Two-Crown Boundary":           "涯之二王",
	"Ain":                          "无态",
	"Berserker Echo":               "狂战士",
	"Spartan Echo":                 "斯巴达",
	"Super Ultimate Perfect Dodge": "超终极精准躲避",

	// 角色专属
	"Fearless Drive":             "英勇神速",
	"Fearless Spirit":            "英勇灵气",
	"Fearless Heart":             "英勇之心",
	"Guardian's Conviction":      "守护者的决心",
	"Guardian's Honor":           "守护者的骄傲",
	"Guardian's Warpath":         "守护者的战气",
	"Helmsman's Navigation":      "舵手的指引",
	"Helmsman's Tenacity":        "舵手的坚持",
	"Helmsman's Warpath":         "舵手的战气",
	"Mage's Aspiration":          "魔法师的心愿",
	"Mage's Savvy":               "魔法师的伶俐",
	"Mage's Warpath":             "魔法师的战气",
	"Veteran's Insight":          "老兵的智慧",
	"Veteran's Vision":           "老兵的天眼",
	"Veteran's Warpath":          "老兵的战气",
	"Rose's Blooming":            "玫瑰早绽",
	"Rose's Profusion":           "玫瑰缤纷",
	"Rose's Warpath":             "玫瑰的战气",
	"Phantasm's Concord":         "幽幻之应",
	"Phantasm's Harmony":         "幽幻之谊",
	"Phantasm's Warpath":         "幽幻的战气",
	"White Dragon's Oath":        "白龙的誓约",
	"White Dragon's Glory":       "白龙的荣耀",
	"White Dragon's Warpath":     "白龙的战气",
	"Hero's Creed":               "勇士的信念",
	"Hero's Will":                "勇士的毅力",
	"Hero's Warpath":             "勇士的战气",
	"Lord's Procession":          "王者行进",
	"Lord's Ambition":            "王者一心",
	"Lord's Warpath":             "王者的战气",
	"Dragonslayer's Dominance":   "屠龙者的威猛",
	"Dragonslayer's Ingenuity":   "屠龙者的才智",
	"Dragonslayer's Warpath":     "屠龙者的战气",
	"Holy Knight's Luster":       "圣骑士的剑辉",
	"Holy Knight's Grandeur":     "圣骑士的威光",
	"Holy Knight's Warpath":      "圣骑士的战气",
	"Swordmaster's Prowess":      "变幻自如的迅刃",
	"Swordmaster's Art":          "变幻自如的妖剑士",
	"Swordmaster's Warpath":      "变幻自如的战气",
	"Butterfly's Grace":          "斩姬梦幻",
	"Butterfly's Valor":          "斩姬武艺",
	"Butterfly's Warpath":        "斩姬的战气",
	"Eternal Rage's Mettle":      "古今无双的气概",
	"Eternal Rage's Ethos":       "古今无双的强者",
	"Eternal Rage's Warpath":     "古今无双的战气",
	"Founder's Strategy":         "极致谋略",
	"Founder's Truth":            "极致真理",
	"Founder's Warpath":          "极致的战气",
	"Versalis Foundation":        "异能增长",
	"Versalis Ignition":          "异能战意",
	"Versalis Heart":             "异能之心",
	"Crimson's Clout":            "真红烈焰",
	"Crimson's Flight":           "真红翔舞",
	"Crimson's Warpath":          "真红的战气",
	"Ebony's Presence":           "冥暗刚刃",
	"Ebony's Poise":              "冥暗自若",
	"Ebony's Warpath":            "冥暗的战气",
	"Spirit Edge's Rally":        "剑圣的炼气",
	"Spirit Edge's Fury":         "剑圣的闪刃",
	"Spirit Edge's Warpath":      "剑圣的战气",
	"Dark Huntress's Volley":     "魔眼的万箭",
	"Dark Huntress's Surge":      "魔眼的凛翔",
	"Dark Huntress's Warpath":    "魔眼的战气",
	"Supreme Primarch's Awe":     "天司长的灵威",
	"Supreme Primarch's Nimbus":  "天司长的风雅",
	"Supreme Primarch's Warpath": "天司长的战气",
	"Gladiator's Frenzy":         "狼王的激昂",
	"Gladiator's Top":            "狼王的大转轮",
	"Gladiator's Warpath":        "狼王的战气",
	"Bladequeen's Serenade":      "刃姬的小夜曲",
	"Bladequeen's Circuit":       "刃姬的轮舞曲",
	"Bladequeen's Warpath":       "刃姬的战气",
	"Ultramarine's Flash":        "群青的剑光",
	"Ultramarine's Adversity":    "群青的逆境",
	"Ultramarine's Warpath":      "群青的战气",
	"Thunderwolf's Recharge":     "雷狼的弹匣",
	"Thunderwolf's Acuity":       "雷狼的慧眼",
	"Thunderwolf's Warpath":      "雷狼的战气",
	"Enchantress's Blessing":     "转世的恩宠",
	"Enchantress's Rhythm":       "转世的跃动",
	"Enchantress's Warpath":      "转世的战气",
	"The Black's Mark":           "黑龙的咒印",
	"The Black's Impulse":        "黑龙的折跃",
	"The Black's Warpath":        "黑龙的战气",
	"Celestial Ventus":           "天星之止息",
	"Celestial Terra":            "天星之界",
	"Celestial Nyx":              "天星之炼",
	"Celestial Aqua":             "天星之雪",
	"Celestial Incendo":          "天星之焰",
	"Celestial Lumen":            "天星之煌",
	"Divergence":                 "分歧",
	"Immortal Shell":             "可怕的漆黑钳蟹因子",
	"In a Pinch":                 "漆黑之谊",
	"Sumo Force":                 "相扑斗力",
	"Fatebreaker":                "浪迹天涯",
	"Fortifying Vigor":           "强健甘露",
	"Instilling Vigor":           "修炼甘露",
	"Gilding Vigor":              "幸运甘露",
	"Crabby Resonance":           "钳蟹的共鸣",
	"Crabvestment Returns":       "钳蟹的报恩",
	"Seven Net":                  "7net",
	"Crabmiration":               "终极钳蟹因子",
	"SBA/Chain Burst Effect+":    "奥义/连锁爆发效果+",
	"Stout Heart":                "霸体",
	"Poison Resistance":          "中毒抗性",
	"Burn Resistance":            "灼热抗性",
	"Sandtomb Resistance":        "泥沙抗性",
	"Glaciate Resistance":        "冰冻抗性",
	"Dizzy Resistance":           "昏迷抗性",
	"Paralysis Resistance":       "麻痹抗性",
	"Skill Sealed Resistance":    "能力封印抗性",
	"SBA Sealed Resistance":      "奥义封印抗性",
	"Low Profile":                "匿踪",
	"Provoke":                    "挑衅",
	"Fast Learner":               "获得经验值",
	"Rupie Tycoon":               "获得金币",
	"Slow Resistance":            "缓速抗性",
	"Firm Stance":                "不动",
	"Blight Resistance":          "灾祸抗性",
	"Steady Focus":               "蓄力强化",
	"Supplements":                "强化效果延长",
	"Natural Defenses":           "全弱化效果抗性",
	"Precise Wrath":              "怒发冲冠",
	"Window of Opportunity":      "攻击机会",
	"Sigil Booster":              "因子强化",
	"Held Under Resistance":      "水牢抗性",
	"Darkflame Resistance":       "异能耐受",
	"Attack Down Resistance":     "攻击DOWN抗性",
	"Defense Down Resistance":    "防御DOWN抗性",
	"Path to Mastery":            "获得MSP",
	"Roll of the Die":            "骰子剑",
}

func cnName(en string) string {
	if !useChinese() {
		return en
	}
	if s, ok := sigilCN[en]; ok {
		return s
	}
	return en
}

func cnTrait(en string) string {
	if !useChinese() {
		return en
	}
	if s, ok := traitCN[en]; ok {
		return s
	}
	return en
}
