import React from 'react';

export type ServiceAnimationType = 'design' | 'development' | 'ecommerce' | 'mobile' | 'uiux' | 'team' | 'default';

export function getServiceAnimationType(title: string): ServiceAnimationType {
    const t = title.toLowerCase();
    if (t.includes('web design') || t.includes('website design')) return 'design';
    if (t.includes('software') || t.includes('web application') || t.includes('development')) return 'development';
    if (t.includes('e-commerce') || t.includes('ecommerce')) return 'ecommerce';
    if (t.includes('mobile') || t.includes('app')) return 'mobile';
    if (t.includes('ui') || t.includes('ux') || t.includes('graphic')) return 'uiux';
    if (t.includes('team') || t.includes('augmentation')) return 'team';
    return 'default';
}

// ─── DESIGN: Browser wireframe আঁকা হচ্ছে + cursor মুভ করছে ─────────────────
const DesignAnimation: React.FC = () => (
    <svg viewBox="0 0 64 64" style={{ width: '100%', height: '100%' }} xmlns="http://www.w3.org/2000/svg">
        {/* Browser frame */}
        <rect x="4" y="7" width="56" height="50" rx="3"
            fill="rgba(255,255,255,0.1)" stroke="rgba(255,255,255,0.7)" strokeWidth="1.5"/>
        <rect x="4" y="7" width="56" height="10" rx="3" fill="rgba(255,255,255,0.2)"/>
        {/* Traffic lights */}
        <circle cx="10" cy="12" r="1.8" fill="#ff5f57"/>
        <circle cx="16.5" cy="12" r="1.8" fill="#ffbd2e"/>
        <circle cx="23" cy="12" r="1.8" fill="#28c840"/>

        {/* Hero block – stroke-dashoffset draw */}
        <rect x="8" y="21" width="48" height="9" rx="1.5"
            fill="none" stroke="rgba(255,255,255,0.9)" strokeWidth="1.5"
            strokeDasharray="114" strokeDashoffset="114">
            <animate attributeName="strokeDashoffset"
                values="114;0;0;114" dur="4s" repeatCount="indefinite"
                keyTimes="0;0.25;0.82;1"/>
        </rect>

        {/* Left column */}
        <rect x="8" y="34" width="16" height="16" rx="1.5"
            fill="none" stroke="rgba(255,255,255,0.75)" strokeWidth="1.5"
            strokeDasharray="64" strokeDashoffset="64">
            <animate attributeName="strokeDashoffset"
                values="64;64;0;0;64" dur="4s" repeatCount="indefinite"
                keyTimes="0;0.28;0.52;0.82;1"/>
        </rect>

        {/* Right column */}
        <rect x="28" y="34" width="28" height="16" rx="1.5"
            fill="none" stroke="rgba(255,255,255,0.75)" strokeWidth="1.5"
            strokeDasharray="88" strokeDashoffset="88">
            <animate attributeName="strokeDashoffset"
                values="88;88;0;0;88" dur="4s" repeatCount="indefinite"
                keyTimes="0;0.55;0.73;0.82;1"/>
        </rect>

        {/* Design cursor (arrow pointer) */}
        <g>
            <animateTransform attributeName="transform" type="translate"
                values="8,21; 56,21; 56,30; 8,34; 24,50; 56,50; 8,21"
                dur="4s" repeatCount="indefinite"
                keyTimes="0;0.08;0.25;0.28;0.52;0.73;1"/>
            <polygon points="0,0 0,9 2.5,6 4.5,10 6,9.5 4,5.5 7,5.5"
                fill="white" opacity="0.95"/>
        </g>
    </svg>
);

// ─── DEVELOPMENT: Code টাইপ হচ্ছে + cursor blinking ─────────────────────────
const DevelopmentAnimation: React.FC = () => (
    <svg viewBox="0 0 64 64" style={{ width: '100%', height: '100%' }} xmlns="http://www.w3.org/2000/svg">
        {/* Terminal frame */}
        <rect x="4" y="7" width="56" height="50" rx="3"
            fill="rgba(0,0,0,0.4)" stroke="rgba(255,255,255,0.35)" strokeWidth="1.2"/>
        <rect x="4" y="7" width="56" height="9" rx="3" fill="rgba(0,0,0,0.5)"/>
        <circle cx="10" cy="11.5" r="1.5" fill="#ff5f57"/>
        <circle cx="16" cy="11.5" r="1.5" fill="#ffbd2e"/>
        <circle cx="22" cy="11.5" r="1.5" fill="#28c840"/>

        {/* $ prompt */}
        <text x="7" y="27" fontSize="5.5" fill="rgba(100,255,100,0.9)" fontFamily="monospace">$</text>

        {/* Line 1 – purple keyword */}
        <rect x="14" y="22.5" height="5" rx="1.2" fill="rgba(195,150,255,0.92)" width="0">
            <animate attributeName="width"
                values="0;36;36;0" dur="5s" repeatCount="indefinite"
                keyTimes="0;0.18;0.82;1"/>
        </rect>

        {/* Line 2 – cyan function */}
        <rect x="7" y="30.5" height="5" rx="1.2" fill="rgba(100,215,255,0.92)" width="0">
            <animate attributeName="width"
                values="0;0;30;30;0" dur="5s" repeatCount="indefinite"
                keyTimes="0;0.18;0.4;0.82;1"/>
        </rect>

        {/* Line 3 – orange string (indented) */}
        <rect x="14" y="38.5" height="5" rx="1.2" fill="rgba(255,175,100,0.92)" width="0">
            <animate attributeName="width"
                values="0;0;24;24;0" dur="5s" repeatCount="indefinite"
                keyTimes="0;0.4;0.57;0.82;1"/>
        </rect>

        {/* Line 4 – green closing */}
        <rect x="7" y="46.5" height="5" rx="1.2" fill="rgba(100,255,140,0.92)" width="0">
            <animate attributeName="width"
                values="0;0;16;16;0" dur="5s" repeatCount="indefinite"
                keyTimes="0;0.57;0.68;0.82;1"/>
        </rect>

        {/* Blinking text cursor */}
        <rect y="0" width="3" height="5" rx="0.8" fill="rgba(255,255,255,0.95)">
            <animate attributeName="opacity" values="1;0;1" dur="0.7s" repeatCount="indefinite"/>
            <animateTransform attributeName="transform" type="translate"
                values="14,22.5; 50,22.5; 37,30.5; 38,38.5; 23,46.5; 14,22.5"
                dur="5s" repeatCount="indefinite"
                keyTimes="0;0.18;0.4;0.57;0.68;1"/>
        </rect>
    </svg>
);

// ─── ECOMMERCE: Products cart-এ ঢুকছে + badge counter বাড়ছে ─────────────────
const EcommerceAnimation: React.FC = () => (
    <svg viewBox="0 0 64 64" style={{ width: '100%', height: '100%' }} xmlns="http://www.w3.org/2000/svg">
        {/* Shopping cart */}
        <path d="M5 12 L13 12 L20 42 L54 42 L58 22 L13 22"
            fill="none" stroke="rgba(255,255,255,0.9)" strokeWidth="2.2"
            strokeLinejoin="round" strokeLinecap="round"/>
        <circle cx="5" cy="10" r="2" fill="rgba(255,255,255,0.7)"/>
        {/* Wheels */}
        <circle cx="25" cy="49" r="4.5" fill="none" stroke="rgba(255,255,255,0.85)" strokeWidth="2"/>
        <circle cx="47" cy="49" r="4.5" fill="none" stroke="rgba(255,255,255,0.85)" strokeWidth="2"/>
        <circle cx="25" cy="49" r="1.5" fill="rgba(255,255,255,0.6)"/>
        <circle cx="47" cy="49" r="1.5" fill="rgba(255,255,255,0.6)"/>

        {/* Product 1 – yellow box flying in */}
        <rect x="-3.5" y="-3.5" width="7" height="7" rx="1.5" fill="rgba(255,210,60,0.95)">
            <animateTransform attributeName="transform" type="translate"
                values="12,8; 36,32" dur="0.9s" begin="0s" repeatCount="indefinite"
                calcMode="spline" keySplines="0.4 0 0.2 1"/>
            <animate attributeName="opacity" values="1;1;0" dur="0.9s" begin="0s"
                repeatCount="indefinite" keyTimes="0;0.75;1"/>
        </rect>

        {/* Product 2 – cyan circle flying in */}
        <circle r="3.5" fill="rgba(80,220,255,0.95)">
            <animateTransform attributeName="transform" type="translate"
                values="55,7; 38,32" dur="0.9s" begin="1.1s" repeatCount="indefinite"
                calcMode="spline" keySplines="0.4 0 0.2 1"/>
            <animate attributeName="opacity" values="1;1;0" dur="0.9s" begin="1.1s"
                repeatCount="indefinite" keyTimes="0;0.75;1"/>
        </circle>

        {/* Product 3 – pink pill flying in */}
        <rect x="-3" y="-3" width="6" height="6" rx="3" fill="rgba(255,120,180,0.95)">
            <animateTransform attributeName="transform" type="translate"
                values="34,5; 42,32" dur="0.9s" begin="2.2s" repeatCount="indefinite"
                calcMode="spline" keySplines="0.4 0 0.2 1"/>
            <animate attributeName="opacity" values="1;1;0" dur="0.9s" begin="2.2s"
                repeatCount="indefinite" keyTimes="0;0.75;1"/>
        </rect>

        {/* Counter badge */}
        <circle cx="54" cy="12" r="8" fill="rgba(255,60,60,0.95)"/>
        <text x="54" y="16" textAnchor="middle" fontSize="8" fill="white"
            fontFamily="sans-serif" fontWeight="bold" opacity="0">
            1
            <animate attributeName="opacity" values="0;1;1;0"
                dur="3.3s" repeatCount="indefinite" keyTimes="0;0.05;0.33;0.38"/>
        </text>
        <text x="54" y="16" textAnchor="middle" fontSize="8" fill="white"
            fontFamily="sans-serif" fontWeight="bold" opacity="0">
            2
            <animate attributeName="opacity" values="0;0;1;1;0"
                dur="3.3s" repeatCount="indefinite" keyTimes="0;0.38;0.43;0.66;0.71"/>
        </text>
        <text x="54" y="16" textAnchor="middle" fontSize="8" fill="white"
            fontFamily="sans-serif" fontWeight="bold" opacity="0">
            3
            <animate attributeName="opacity" values="0;0;1;1;0"
                dur="3.3s" repeatCount="indefinite" keyTimes="0;0.71;0.76;0.95;1"/>
        </text>
    </svg>
);

// ─── MOBILE: Phone-এ app লোড হচ্ছে ──────────────────────────────────────────
const MobileAnimation: React.FC = () => (
    <svg viewBox="0 0 64 64" style={{ width: '100%', height: '100%' }} xmlns="http://www.w3.org/2000/svg">
        <rect x="18" y="4" width="28" height="56" rx="5"
            fill="rgba(255,255,255,0.1)" stroke="rgba(255,255,255,0.8)" strokeWidth="1.8"/>
        <rect x="21" y="10" width="22" height="38" rx="2" fill="rgba(255,255,255,0.12)"/>
        <circle cx="32" cy="55" r="3" fill="none" stroke="rgba(255,255,255,0.6)" strokeWidth="1.2"/>
        <rect x="26" y="6.5" width="12" height="2" rx="1" fill="rgba(255,255,255,0.4)"/>

        {/* Progress bar */}
        <rect x="23" y="13" width="18" height="3" rx="1.5" fill="rgba(255,255,255,0.2)"/>
        <rect x="23" y="13" width="0" height="3" rx="1.5" fill="rgba(255,255,255,0.9)">
            <animate attributeName="width" values="0;18;18;0"
                dur="3s" repeatCount="indefinite" keyTimes="0;0.4;0.85;1"/>
        </rect>

        {/* Content lines appearing */}
        <rect x="23" y="20" width="18" height="3" rx="1.5" fill="rgba(255,255,255,0.7)" opacity="0">
            <animate attributeName="opacity" values="0;0;1;1;0"
                dur="3s" repeatCount="indefinite" keyTimes="0;0.4;0.5;0.85;1"/>
        </rect>
        <rect x="23" y="25.5" width="13" height="3" rx="1.5" fill="rgba(255,255,255,0.6)" opacity="0">
            <animate attributeName="opacity" values="0;0;1;1;0"
                dur="3s" repeatCount="indefinite" keyTimes="0;0.5;0.58;0.85;1"/>
        </rect>
        <rect x="23" y="31" width="16" height="3" rx="1.5" fill="rgba(255,255,255,0.6)" opacity="0">
            <animate attributeName="opacity" values="0;0;1;1;0"
                dur="3s" repeatCount="indefinite" keyTimes="0;0.58;0.66;0.85;1"/>
        </rect>

        {/* Tap ripple */}
        <circle cx="32" cy="42" r="0" fill="none" stroke="rgba(255,255,255,0.7)" strokeWidth="1.5">
            <animate attributeName="r" values="0;8;8" dur="3s" repeatCount="indefinite" keyTimes="0;0.7;1"/>
            <animate attributeName="opacity" values="0;1;0" dur="3s" repeatCount="indefinite" keyTimes="0;0.7;1"/>
        </circle>
    </svg>
);

// ─── UI/UX: Color palette সিলেকশন + brush stroke ─────────────────────────────
const UIUXAnimation: React.FC = () => (
    <svg viewBox="0 0 64 64" style={{ width: '100%', height: '100%' }} xmlns="http://www.w3.org/2000/svg">
        {/* Palette */}
        <ellipse cx="33" cy="36" rx="22" ry="18"
            fill="rgba(255,255,255,0.12)" stroke="rgba(255,255,255,0.6)" strokeWidth="1.5"/>
        <circle cx="23" cy="50" r="5"
            fill="rgba(255,255,255,0.15)" stroke="rgba(255,255,255,0.5)" strokeWidth="1.2"/>

        {/* Color dots */}
        <circle cx="25" cy="28" r="5" fill="rgba(255,80,80,0.9)"/>
        <circle cx="36" cy="23" r="5" fill="rgba(255,165,0,0.9)"/>
        <circle cx="47" cy="30" r="5" fill="rgba(100,220,100,0.9)"/>
        <circle cx="47" cy="42" r="5" fill="rgba(80,140,255,0.9)"/>
        <circle cx="37" cy="49" r="5" fill="rgba(200,100,255,0.9)"/>

        {/* Selection ring cycling */}
        <circle r="8" fill="none" stroke="white" strokeWidth="2.5">
            <animateTransform attributeName="transform" type="translate"
                values="25,28; 36,23; 47,30; 47,42; 37,49; 25,28"
                dur="5s" repeatCount="indefinite" calcMode="discrete"
                keyTimes="0;0.2;0.4;0.6;0.8;1"/>
        </circle>

        {/* Brush stroke drawing */}
        <path d="M8 14 Q 18 8, 28 16" fill="none"
            stroke="rgba(255,255,255,0.85)" strokeWidth="2.5" strokeLinecap="round"
            strokeDasharray="30" strokeDashoffset="30">
            <animate attributeName="strokeDashoffset" values="30;0;0;30"
                dur="5s" repeatCount="indefinite" keyTimes="0;0.18;0.88;1"/>
        </path>
        <circle cx="8" cy="14" r="2.5" fill="rgba(255,255,255,0.9)">
            <animateTransform attributeName="transform" type="translate"
                values="0,0; 20,2; 20,2; 0,0" dur="5s" repeatCount="indefinite"
                keyTimes="0;0.18;0.88;1"/>
        </circle>
    </svg>
);

// ─── TEAM: Members connected + activity dot ────────────────────────────────
const TeamAnimation: React.FC = () => (
    <svg viewBox="0 0 64 64" style={{ width: '100%', height: '100%' }} xmlns="http://www.w3.org/2000/svg">
        {/* Left person */}
        <circle cx="13" cy="22" r="6.5" fill="rgba(255,255,255,0.75)"/>
        <ellipse cx="13" cy="44" rx="9" ry="7" fill="rgba(255,255,255,0.45)"/>
        {/* Center person (lead) */}
        <circle cx="32" cy="18" r="8" fill="rgba(255,255,255,0.95)"/>
        <ellipse cx="32" cy="46" rx="10" ry="8" fill="rgba(255,255,255,0.65)"/>
        {/* Right person */}
        <circle cx="51" cy="22" r="6.5" fill="rgba(255,255,255,0.75)"/>
        <ellipse cx="51" cy="44" rx="9" ry="7" fill="rgba(255,255,255,0.45)"/>

        {/* Pulsing connection lines */}
        <line x1="13" y1="22" x2="32" y2="18"
            stroke="rgba(255,255,255,0.65)" strokeWidth="1.5" strokeDasharray="4,3">
            <animate attributeName="opacity" values="0.3;1;0.3" dur="2s" repeatCount="indefinite"/>
        </line>
        <line x1="32" y1="18" x2="51" y2="22"
            stroke="rgba(255,255,255,0.65)" strokeWidth="1.5" strokeDasharray="4,3">
            <animate attributeName="opacity" values="0.3;1;0.3" dur="2s" begin="0.6s" repeatCount="indefinite"/>
        </line>

        {/* Activity dot travelling between members */}
        <circle r="2.5" fill="rgba(255,220,80,0.95)">
            <animateTransform attributeName="transform" type="translate"
                values="13,22; 32,18; 51,22; 32,18; 13,22"
                dur="4s" repeatCount="indefinite"
                calcMode="spline"
                keySplines="0.4 0 0.2 1;0.4 0 0.2 1;0.4 0 0.2 1;0.4 0 0.2 1"/>
        </circle>

        {/* Checkmark badge appearing */}
        <circle cx="32" cy="7" r="5" fill="rgba(100,220,120,0.9)" opacity="0">
            <animate attributeName="opacity" values="0;0;1;1;0"
                dur="4s" repeatCount="indefinite" keyTimes="0;0.55;0.65;0.9;1"/>
        </circle>
        <text x="32" y="10.5" textAnchor="middle" fontSize="6"
            fill="white" fontWeight="bold" opacity="0">
            ✓
            <animate attributeName="opacity" values="0;0;1;1;0"
                dur="4s" repeatCount="indefinite" keyTimes="0;0.55;0.65;0.9;1"/>
        </text>
    </svg>
);

// ─── DEFAULT: Generic pulse ────────────────────────────────────────────────
const DefaultAnimation: React.FC = () => (
    <svg viewBox="0 0 64 64" style={{ width: '100%', height: '100%' }} xmlns="http://www.w3.org/2000/svg">
        <circle cx="32" cy="32" r="14" fill="none" stroke="rgba(255,255,255,0.6)" strokeWidth="2">
            <animate attributeName="r" values="14;22;14" dur="2.5s" repeatCount="indefinite"/>
            <animate attributeName="opacity" values="1;0.4;1" dur="2.5s" repeatCount="indefinite"/>
        </circle>
        <circle cx="32" cy="32" r="8" fill="rgba(255,255,255,0.85)">
            <animate attributeName="r" values="7;10;7" dur="2.5s" repeatCount="indefinite"/>
        </circle>
    </svg>
);

// ─── Map type → component ─────────────────────────────────────────────────
const animations: Record<ServiceAnimationType, React.FC> = {
    design: DesignAnimation,
    development: DevelopmentAnimation,
    ecommerce: EcommerceAnimation,
    mobile: MobileAnimation,
    uiux: UIUXAnimation,
    team: TeamAnimation,
    default: DefaultAnimation,
};

interface ServiceCardAnimationProps {
    title: string;
}

const ServiceCardAnimation: React.FC<ServiceCardAnimationProps> = ({ title }) => {
    const type = getServiceAnimationType(title);
    const AnimationComponent = animations[type];
    return <AnimationComponent />;
};

export default ServiceCardAnimation;
