#!/bin/bash
# Test script for par-term font features
# Run this inside par-term to verify font rendering

set -euo pipefail

# Colors for output
GREEN='\033[0;32m'
BLUE='\033[0;34m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color

echo ""
echo "╔══════════════════════════════════════════════════════════════╗"
echo "║  par-term Font Feature Test Suite                           ║"
echo "╚══════════════════════════════════════════════════════════════╝"
echo ""

# Test 1: Regular ASCII
echo -e "${BLUE}Test 1: Regular ASCII Characters${NC}"
echo "────────────────────────────────────────────────────────"
echo "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
echo "0123456789 !@#\$%^&*()_+-=[]{}|;:',.<>?/"
echo ""

# Test 2: Bold text (should use font_family_bold if configured)
echo -e "${BLUE}Test 2: Bold Text (Styled Font)${NC}"
echo "────────────────────────────────────────────────────────"
echo -e "Regular: The quick brown fox jumps over the lazy dog"
echo -e "\033[1mBold: The quick brown fox jumps over the lazy dog\033[0m"
echo -e "${YELLOW}✓ If configured, bold should use font_family_bold${NC}"
echo ""

# Test 3: Italic text (should use font_family_italic if configured)
echo -e "${BLUE}Test 3: Italic Text (Styled Font)${NC}"
echo "────────────────────────────────────────────────────────"
echo -e "Regular: The quick brown fox jumps over the lazy dog"
echo -e "\033[3mItalic: The quick brown fox jumps over the lazy dog\033[0m"
echo -e "${YELLOW}✓ If configured, italic should use font_family_italic${NC}"
echo ""

# Test 4: Bold+Italic (should use font_family_bold_italic if configured)
echo -e "${BLUE}Test 4: Bold+Italic Text (Styled Font)${NC}"
echo "────────────────────────────────────────────────────────"
echo -e "Regular: The quick brown fox jumps over the lazy dog"
echo -e "\033[1;3mBold+Italic: The quick brown fox jumps over the lazy dog\033[0m"
echo -e "${YELLOW}✓ If configured, should use font_family_bold_italic${NC}"
echo ""

# Test 5: CJK Characters (should use font_ranges if configured)
echo -e "${BLUE}Test 5: CJK Characters (Font Ranges)${NC}"
echo "────────────────────────────────────────────────────────"
echo "Chinese (Simplified): 你好世界 (Nǐ hǎo shìjiè)"
echo "Chinese (Traditional): 你好世界"
echo "Japanese Hiragana: こんにちは世界 (Konnichiwa sekai)"
echo "Japanese Katakana: コンニチハ セカイ"
echo "Korean: 안녕하세요 세계 (Annyeonghaseyo segye)"
echo -e "${YELLOW}✓ CJK should use font_ranges[0x4E00-0x9FFF] if configured${NC}"
echo ""

# Test 6: Emoji (should use font_ranges if configured)
echo -e "${BLUE}Test 6: Emoji (Font Ranges)${NC}"
echo "────────────────────────────────────────────────────────"
echo "Faces: 😀 😃 😄 😁 😊 🙂 🙃 😉 😍 🥰"
echo "Symbols: 🚀 ⭐ 🌟 🔥 💻 📱 ⚡ 🎉 ❤️ 👍"
echo "Animals: 🐶 🐱 🐭 🐹 🐰 🦊 🐻 🐼 🐨 🐯"
echo "Food: 🍎 🍊 🍋 🍌 🍉 🍇 🍓 🍈 🍒 🍑"
echo -e "${YELLOW}✓ Emoji should use font_ranges[0x1F600-0x1F64F] if configured${NC}"
echo ""

# Test 7: Flags (Regional Indicator Multi-Part Rendering)
echo -e "${BLUE}Test 7: Flags (Regional Indicators - Text Shaping)${NC}"
echo "────────────────────────────────────────────────────────"
echo "Country Flags: 🇺🇸 🇬🇧 🇯🇵 🇩🇪 🇫🇷 🇨🇦 🇦🇺 🇧🇷 🇮🇳 🇨🇳"
echo "More Flags: 🇰🇷 🇮🇹 🇪🇸 🇷🇺 🇲🇽 🇳🇱 🇸🇪 🇨🇭 🇵🇱 🇹🇷"
echo "Flag Examples:"
echo "  🇺🇸 United States (U+1F1FA + U+1F1F8)"
echo "  🇬🇧 United Kingdom (U+1F1EC + U+1F1E7)"
echo "  🇯🇵 Japan (U+1F1EF + U+1F1F5)"
echo -e "${YELLOW}✓ Flags use Regional Indicator pairs (U+1F1E6-U+1F1FF)${NC}"
echo -e "${YELLOW}✓ Tests multi-codepoint grapheme cluster rendering${NC}"
echo -e "${GREEN}✓ Text shaping enabled - flags should render correctly!${NC}"
echo ""

# Test 8: Mathematical Symbols (should use font_ranges if configured)
echo -e "${BLUE}Test 8: Mathematical Symbols (Font Ranges)${NC}"
echo "────────────────────────────────────────────────────────"
echo "Operators: + − × ÷ = ≠ ≈ ≤ ≥ ± ∓"
echo "Set Theory: ∈ ∉ ∋ ∌ ⊂ ⊃ ⊆ ⊇ ∩ ∪ ∅"
echo "Logic: ∀ ∃ ∄ ∧ ∨ ¬ ⊤ ⊥"
echo "Calculus: ∫ ∬ ∭ ∮ ∂ ∇ ∆ √ ∛ ∜"
echo "Examples:"
echo "  ∀x∈ℝ: x²≥0"
echo "  ∫₀^∞ e^(-x²)dx = √π/2"
echo "  ∑ᵢ₌₁^n i = n(n+1)/2"
echo -e "${YELLOW}✓ Math should use font_ranges[0x2200-0x22FF] if configured${NC}"
echo ""

# Test 9: Box Drawing (should use font_ranges if configured)
echo -e "${BLUE}Test 9: Box Drawing (Font Ranges)${NC}"
echo "────────────────────────────────────────────────────────"
echo "Simple box:"
echo "┌─────────┐"
echo "│  Hello  │"
echo "│  World  │"
echo "└─────────┘"
echo ""
echo "Complex grid:"
echo "┌─┬─┬─┐"
echo "│ │ │ │"
echo "├─┼─┼─┤"
echo "│ │ │ │"
echo "├─┼─┼─┤"
echo "│ │ │ │"
echo "└─┴─┴─┘"
echo ""
echo "Block elements: ▀ ▄ █ ▌ ▐ ░ ▒ ▓"
echo -e "${YELLOW}✓ Box drawing should use font_ranges[0x2500-0x257F]${NC}"
echo ""

# Test 10: Arrows and Symbols
echo -e "${BLUE}Test 10: Arrows and Symbols${NC}"
echo "────────────────────────────────────────────────────────"
echo "Arrows: ← → ↑ ↓ ↔ ↕ ⇐ ⇒ ⇑ ⇓ ⇔"
echo "Checkmarks: ✓ ✔ ✗ ✘ ☑ ☒"
echo "Stars: ★ ☆ ✦ ✧ ⭐ 🌟"
echo "Card suits: ♠ ♣ ♥ ♦"
echo "Music: ♩ ♪ ♫ ♬ ♭ ♮ ♯"
echo ""

# Test 11: OSC 8 Hyperlinks
echo -e "${BLUE}Test 11: OSC 8 Hyperlinks${NC}"
echo "────────────────────────────────────────────────────────"
echo "Regular URLs (regex detection):"
echo "  https://github.com/paulrobello/par-term"
echo "  http://example.com"
echo ""
echo "OSC 8 Hyperlinks (hover and Ctrl+Click):"
printf "  Visit "
printf "\e]8;;https://github.com/paulrobello/par-term\e\\GitHub Repository\e]8;;\e\\"
printf " for more info\n"
printf "  Check out "
printf "\e]8;;https://www.rust-lang.org\e\\Rust Lang\e]8;;\e\\"
printf " official site\n"
echo -e "${YELLOW}✓ Both URL types should be clickable with Ctrl+Click${NC}"
echo ""

# Test 12: Mixed Content
echo -e "${BLUE}Test 12: Mixed Content (All Features)${NC}"
echo "────────────────────────────────────────────────────────"
echo -e "\033[1mBold:\033[0m The number π ≈ 3.14159..."
echo -e "\033[3mItalic:\033[0m 世界 means 'world' 🌍"
echo -e "\033[1;3mBold+Italic:\033[0m ∫∫∫ Triple integral 📐"
echo ""
echo "Mixed line: Regular 你好 Bold 😀 Math ∑ᵢ₌₁^n Box ┌─┐"
echo ""

# Test 13: Emoji Skin Tones (Text Shaping - Multi-codepoint Graphemes)
echo -e "${BLUE}Test 13: Emoji Skin Tones (Fitzpatrick Modifiers)${NC}"
echo "────────────────────────────────────────────────────────"
echo "Waving Hand:"
echo "  👋 Default (no modifier)"
echo "  👋🏻 Light skin tone (U+1F3FB)"
echo "  👋🏼 Medium-light skin tone (U+1F3FC)"
echo "  👋🏽 Medium skin tone (U+1F3FD)"
echo "  👋🏾 Medium-dark skin tone (U+1F3FE)"
echo "  👋🏿 Dark skin tone (U+1F3FF)"
echo ""
echo "Thumbs Up:"
echo "  👍 👍🏻 👍🏼 👍🏽 👍🏾 👍🏿"
echo ""
echo "Raised Fist:"
echo "  ✊ ✊🏻 ✊🏼 ✊🏽 ✊🏾 ✊🏿"
echo ""
echo "Person Gestures:"
echo "  🙋 🙋🏻 🙋🏼 🙋🏽 🙋🏾 🙋🏿"
echo "  💁 💁🏻 💁🏼 💁🏽 💁🏾 💁🏿"
echo -e "${YELLOW}✓ Tests grapheme cluster caching with skin tone modifiers${NC}"
echo -e "${GREEN}✓ All skin tones should render with correct colors!${NC}"
echo ""

# Test 14: ZWJ Sequences (Zero-Width Joiner)
echo -e "${BLUE}Test 14: ZWJ Sequences (Complex Emoji)${NC}"
echo "────────────────────────────────────────────────────────"
echo "Family Emoji (ZWJ sequences):"
echo "  👨‍👩‍👧‍👦 Family: man, woman, girl, boy"
echo "  👨‍👩‍👧 Family: man, woman, girl"
echo "  👨‍👨‍👧‍👦 Family: man, man, girl, boy"
echo "  👩‍👩‍👧‍👧 Family: woman, woman, girl, girl"
echo ""
echo "Profession Emoji (ZWJ with modifiers):"
echo "  👨‍⚕️ Man health worker"
echo "  👩‍⚕️ Woman health worker"
echo "  👨‍💻 Man technologist"
echo "  👩‍💻 Woman technologist"
echo "  👨‍🚀 Man astronaut"
echo "  👩‍🚀 Woman astronaut"
echo ""
echo "Multi-component Emoji:"
echo "  👁️‍🗨️ Eye in speech bubble"
echo "  🏴‍☠️ Pirate flag"
echo "  🐻‍❄️ Polar bear"
echo "  👨🏽‍💻 Man technologist: medium skin tone"
echo "  👩🏾‍🚀 Woman astronaut: medium-dark skin tone"
echo -e "${YELLOW}✓ Tests complex ZWJ sequences with skin tones${NC}"
echo -e "${GREEN}✓ Text shaping should combine all components correctly!${NC}"
echo ""

# Test 15: Complex Scripts (Requires Text Shaping)
echo -e "${BLUE}Test 15: Complex Scripts (Arabic, Devanagari, Thai)${NC}"
echo "────────────────────────────────────────────────────────"
echo "Arabic (Right-to-Left, Contextual Shaping):"
echo "  مرحبا (marhaba - Hello)"
echo "  السلام عليكم (as-salāmu ʿalaykum - Peace be upon you)"
echo "  العربية (al-ʿarabiyyah - Arabic)"
echo "  مرحبا بك في العالم (Welcome to the world)"
echo -e "${YELLOW}✓ Arabic letters should connect based on position (initial/medial/final/isolated)${NC}"
echo ""
echo "Devanagari (Complex Ligatures):"
echo "  नमस्ते (namaste - Hello)"
echo "  धन्यवाद (dhanyavād - Thank you)"
echo "  स्वागत (svāgat - Welcome)"
echo "  देवनागरी (devanāgarī - Devanagari script)"
echo -e "${YELLOW}✓ Devanagari consonant clusters should form ligatures${NC}"
echo ""
echo "Thai (Non-spacing Marks):"
echo "  สวัสดี (sawatdii - Hello)"
echo "  ขอบคุณ (khop khun - Thank you)"
echo "  ภาษาไทย (phasa thai - Thai language)"
echo "  ยินดีต้อนรับ (yin dii ton rap - Welcome)"
echo -e "${YELLOW}✓ Thai vowels and tone marks should position correctly${NC}"
echo ""
echo "Bengali (Complex Shaping):"
echo "  হ্যালো (hyalo - Hello)"
echo "  ধন্যবাদ (dhônnôbad - Thank you)"
echo "  বাংলা (bangla - Bengali)"
echo ""
echo "Tamil (Complex Ligatures):"
echo "  வணக்கம் (vanakkam - Hello)"
echo "  நன்றி (nanri - Thank you)"
echo "  தமிழ் (tamil - Tamil)"
echo -e "${GREEN}✓ Text shaping should handle all script features correctly!${NC}"
echo ""

# Test 16: Bidirectional Text (BiDi)
echo -e "${BLUE}Test 16: Bidirectional Text (LTR + RTL Mix)${NC}"
echo "────────────────────────────────────────────────────────"
echo "English + Arabic:"
echo "  Hello مرحبا World"
echo "  The word مرحبا means hello"
echo "  Welcome to العالم العربي (Arab world)"
echo ""
echo "English + Hebrew:"
echo "  Hello שלום World"
echo "  Peace: שלום and سلام"
echo "  Mixed text: English שָׁלוֹם عَرَبِيّ 123 numbers"
echo ""
echo "LTR with embedded RTL:"
echo "  She said \"مرحبا\" to me"
echo "  The file is named \"שלום.txt\""
echo ""
echo "Complex BiDi (nested):"
echo "  English (Arabic العربية Hebrew עברית) English"
echo -e "${YELLOW}✓ BiDi algorithm should reorder text correctly${NC}"
echo -e "${GREEN}✓ Text shaping with unicode-bidi integration!${NC}"
echo ""

# Test 17: Combining Diacritics
echo -e "${BLUE}Test 17: Combining Diacritics${NC}"
echo "────────────────────────────────────────────────────────"
echo "Latin diacritics (precomposed):"
echo "  á é í ó ú à è ì ò ù â ê î ô û ã õ ñ"
echo "  ä ë ï ö ü ÿ ç Á É Í Ó Ú À È Ì Ò Ù"
echo ""
echo "Latin diacritics (combining characters):"
echo "  a\u0301 e\u0301 i\u0301 o\u0301 u\u0301 (acute: U+0301)"
echo "  a\u0300 e\u0300 i\u0300 o\u0300 u\u0300 (grave: U+0300)"
echo "  a\u0302 e\u0302 i\u0302 o\u0302 u\u0302 (circumflex: U+0302)"
echo "  a\u0303 e\u0303 i\u0303 o\u0303 u\u0303 (tilde: U+0303)"
echo "  a\u0308 e\u0308 i\u0308 o\u0308 u\u0308 (diaeresis: U+0308)"
echo ""
echo "Multiple combining marks:"
echo "  o\u0308\u0304 (o with diaeresis and macron)"
echo "  u\u0301\u0303 (u with acute and tilde)"
echo "  e\u0302\u0301 (e with circumflex and acute)"
echo ""
echo "Vietnamese (complex diacritics):"
echo "  Tiếng Việt (Vietnamese language)"
echo "  Xin chào (Hello)"
echo "  Cảm ơn (Thank you)"
echo ""
echo "IPA (International Phonetic Alphabet):"
echo "  [ˈhɛloʊ] (hello)"
echo "  [əˈbaʊt] (about)"
echo "  [ɪnˌtɚˈnæʃənəl] (international)"
echo -e "${YELLOW}✓ Combining marks should overlay correctly on base characters${NC}"
echo -e "${GREEN}✓ Text shaping handles multi-mark compositions!${NC}"
echo ""

# Test 18: Programming Ligatures
echo -e "${BLUE}Test 18: Programming Ligatures${NC}"
echo "────────────────────────────────────────────────────────"
echo "Arrow operators:"
echo "  -> => --> ==> >>= <<="
echo "  <- <-- <== <<- >>- ->> <->"
echo ""
echo "Comparison operators:"
echo "  == != === !== <= >= <=> <=>"
echo "  =/= =!= <> /= !=="
echo ""
echo "Logical operators:"
echo "  && || !! &&&"
echo ""
echo "Pipe operators:"
echo "  |> <| ||> <|| ||| |||>"
echo ""
echo "Other operators:"
echo "  ++ +++ -- :: ::: .= !~ ~= ~- ~@"
echo "  *> <* <*> <$ $> <$> <!-- -->"
echo ""
echo "Code examples:"
echo "  const add = (a, b) => a + b;"
echo "  fn process() -> Result<(), Error> {"
echo "  if x >= 0 && y <= 100 {"
echo "  data |> filter |> map |> reduce"
echo -e "${YELLOW}⚠ NOTE: Programming ligatures may not display in terminal context${NC}"
echo -e "${YELLOW}  Terminal cell width constraints prevent multi-char ligatures${NC}"
echo -e "${YELLOW}  This is a known limitation - future enhancement${NC}"
echo -e "${GREEN}✓ OpenType liga/clig features are activated in text shaping!${NC}"
echo ""

# Test 19: Wide Character Rendering
echo -e "${BLUE}Test 19: Wide Character Rendering${NC}"
echo "────────────────────────────────────────────────────────"
echo "CJK characters (2 cells wide):"
echo "  中文字体 (Chinese characters)"
echo "  日本語フォント (Japanese mixed)"
echo "  한국어 글꼴 (Korean characters)"
echo ""
echo "Emoji (2 cells wide):"
echo "  😀😃😄😁😊"
echo "  🚀⭐🌟🔥💻"
echo ""
echo "Mixed width test:"
echo "  ABC中文123日本語456한국어789"
echo "  a😀b😃c😄d😁e😊f"
echo ""
echo "Alignment test (should line up):"
echo "  ┌────┬────┬────┐"
echo "  │ A  │ 中 │ 😀 │"
echo "  │ BC │ 文 │ 🚀 │"
echo "  └────┴────┴────┘"
echo -e "${YELLOW}✓ Wide characters should occupy 2 cells${NC}"
echo -e "${YELLOW}✓ Tests glyph advances from text shaping${NC}"
echo -e "${GREEN}✓ Box alignment verifies correct cell width handling!${NC}"
echo ""

# Test 20: Variation Selectors
echo -e "${BLUE}Test 20: Variation Selectors (Text vs Emoji Style)${NC}"
echo "────────────────────────────────────────────────────────"
echo "Default rendering:"
echo "  ☺ ☹ ♠ ♣ ♥ ♦ ☀ ★ ☂"
echo ""
echo "Text style (U+FE0E):"
echo "  ☺︎ ☹︎ ♠︎ ♣︎ ♥︎ ♦︎ ☀︎ ★︎ ☂︎"
echo ""
echo "Emoji style (U+FE0F):"
echo "  ☺️ ☹️ ♠️ ♣️ ♥️ ♦️ ☀️ ★️ ☂️"
echo ""
echo "More examples:"
echo "  ©︎ (text) vs ©️ (emoji)"
echo "  ®︎ (text) vs ®️ (emoji)"
echo "  ✂︎ (text) vs ✂️ (emoji)"
echo -e "${YELLOW}✓ Variation selectors control text vs emoji rendering${NC}"
echo -e "${GREEN}✓ Text shaping respects variation selectors!${NC}"
echo ""

# Test 21: Performance Test
echo -e "${BLUE}Test 21: Performance Test${NC}"
echo "────────────────────────────────────────────────────────"
echo "Rendering many different glyphs..."
for i in {1..10}; do
    echo "Line $i: ABC 你好 😀👍🏽 ∀x∈ℝ ┌─┐ مرحبا नमस्ते -> => fi fl"
done
echo -e "${YELLOW}✓ Should render smoothly without lag${NC}"
echo -e "${YELLOW}✓ Tests shaped glyph caching performance${NC}"
echo ""

# Summary
echo ""
echo "╔══════════════════════════════════════════════════════════════╗"
echo "║  Test Suite Complete                                         ║"
echo "╚══════════════════════════════════════════════════════════════╝"
echo ""
echo -e "${GREEN}✓ All 21 comprehensive tests completed!${NC}"
echo ""
echo "Text Shaping Features Tested:"
echo "  ✓ Basic font features (regular, bold, italic, bold+italic)"
echo "  ✓ CJK characters and font ranges"
echo "  ✓ Emoji with skin tone modifiers (Fitzpatrick)"
echo "  ✓ Flag emoji (Regional Indicator pairs)"
echo "  ✓ ZWJ sequences (family, profession, complex emoji)"
echo "  ✓ Complex scripts (Arabic, Devanagari, Thai, Bengali, Tamil)"
echo "  ✓ Bidirectional text (LTR + RTL mix)"
echo "  ✓ Combining diacritics (precomposed and combining characters)"
echo "  ✓ Programming ligatures (liga, clig features)"
echo "  ✓ Wide character rendering (CJK, emoji cell width)"
echo "  ✓ Variation selectors (text vs emoji style)"
echo "  ✓ Performance with shaped glyph caching"
echo ""
echo "Verification Checklist:"
echo "  1. Styled fonts: Bold/italic should use configured font families"
echo "  2. Emoji skin tones: Should render with correct colors (👍🏻-👍🏿)"
echo "  3. Flag emoji: Should combine into flags (🇺🇸 🇬🇧 🇯🇵)"
echo "  4. Complex emoji: ZWJ sequences should form single glyphs (👨‍👩‍👧‍👦)"
echo "  5. Arabic/RTL: Letters should connect contextually"
echo "  6. Devanagari/Thai: Complex ligatures should form"
echo "  7. BiDi text: Mixed LTR/RTL should reorder correctly"
echo "  8. Diacritics: Combining marks should overlay properly"
echo "  9. Wide chars: CJK/emoji should occupy 2 cells"
echo "  10. Performance: Should render smoothly without lag"
echo ""
echo "Configuration:"
echo "  Config file: ~/.config/par-term/config.yaml"
echo "  Text shaping: enable_text_shaping: true (default)"
echo "  Ligatures: enable_ligatures: true (default)"
echo "  Kerning: enable_kerning: true (default)"
echo ""
echo "See examples/ directory for configuration templates"
echo ""
