using System;
using Gtk;

/// Line icons drawn with Cairo, so they never depend on the icon theme or
/// an SVG loader being installed.
///
///   new ModeIcon(kind, 96)                     mode buttons: white icon on a
///                                              translucent rounded plate
///   new ModeIcon(kind, 32, false, 10, r, g, b) plain icon in any colour
///                                              (設定 / 終了 / 戻る / 再生)
public class ModeIcon : DrawingArea
{
    public enum Kind { Stethoscope, BodyTarget, Chart, Cog, Power, Back, Play }

    private readonly Kind _kind;
    private readonly bool _plate;
    private readonly int _gap;          // space reserved between icon and label
    private readonly double _r, _g, _b; // stroke colour

    /// Mode-button style: plate + white glyph + 28px gap.
    public ModeIcon(Kind kind, int size) : this(kind, size, true, 28, 1, 1, 1) { }

    public ModeIcon(Kind kind, int size, bool plate, int gap, double r, double g, double b)
    {
        _kind = kind; _plate = plate; _gap = gap;
        _r = r; _g = g; _b = b;
        // GTK ignores CSS margins on a button's image, so the gap is part of the icon.
        SetSizeRequest(size + gap, size);
        Drawn += OnDrawn;
    }

    private void OnDrawn(object o, DrawnArgs args)
    {
        var cr = args.Cr;
        double w = AllocatedWidth - _gap, h = AllocatedHeight, s = Math.Min(w, h);
        double x0 = 0, y0 = (h - s) / 2;

        double k;
        if (_plate)
        {
            double r = s * 0.27;
            cr.NewPath();
            cr.Arc(x0 + s - r, y0 + r,     r, -Math.PI / 2, 0);
            cr.Arc(x0 + s - r, y0 + s - r, r, 0,            Math.PI / 2);
            cr.Arc(x0 + r,     y0 + s - r, r, Math.PI / 2,  Math.PI);
            cr.Arc(x0 + r,     y0 + r,     r, Math.PI,      3 * Math.PI / 2);
            cr.ClosePath();
            cr.SetSourceRGBA(1, 1, 1, 0.18);
            cr.Fill();
            k = s * 0.58 / 24.0;
        }
        else
        {
            k = s / 24.0;
        }

        cr.Save();
        cr.Translate(x0 + (s - 24 * k) / 2, y0 + (s - 24 * k) / 2);
        cr.Scale(k, k);
        cr.LineWidth = _plate ? 1.8 : 2.2;
        cr.LineCap = Cairo.LineCap.Round;
        cr.LineJoin = Cairo.LineJoin.Round;
        cr.SetSourceRGB(_r, _g, _b);

        switch (_kind)
        {
            case Kind.Stethoscope: Stethoscope(cr); break;
            case Kind.BodyTarget:  BodyTarget(cr);  break;
            case Kind.Chart:       Chart(cr);       break;
            case Kind.Cog:         Cog(cr);         break;
            case Kind.Power:       Power(cr);       break;
            case Kind.Back:        Back(cr);        break;
            case Kind.Play:        Play(cr);        break;
        }
        cr.Stroke();
        cr.Restore();

        args.RetVal = true;
    }

    // earpieces, binaurals, tubing, chest piece
    private static void Stethoscope(Cairo.Context cr)
    {
        cr.MoveTo(4, 3);  cr.LineTo(6, 3);
        cr.MoveTo(14, 3); cr.LineTo(16, 3);
        cr.MoveTo(5, 3);  cr.LineTo(5, 8);
        cr.ArcNegative(10, 8, 5, Math.PI, 0);          // U-bend to (15,8)
        cr.LineTo(15, 3);
        cr.MoveTo(10, 13); cr.LineTo(10, 15.5);
        cr.ArcNegative(15, 15.5, 5, Math.PI, 0);       // tubing to (20,15.5)
        cr.LineTo(20, 12.5);
        cr.NewSubPath();
        cr.Arc(20, 10.5, 2, 0, 2 * Math.PI);           // chest piece
    }

    // torso with an auscultation target on the chest
    private static void BodyTarget(Cairo.Context cr)
    {
        cr.MoveTo(9.5, 3.5);                            // neckline
        cr.CurveTo(9.9, 4.7, 10.8, 5.4, 12, 5.4);
        cr.CurveTo(13.2, 5.4, 14.1, 4.7, 14.5, 3.5);
        cr.MoveTo(9.5, 3.5);                            // body outline
        cr.LineTo(5.5, 5.3); cr.LineTo(3.5, 11); cr.LineTo(6.1, 12);
        cr.LineTo(7, 9.5);   cr.LineTo(7, 21);   cr.LineTo(17, 21);
        cr.LineTo(17, 9.5);  cr.LineTo(17.9, 12); cr.LineTo(20.5, 11);
        cr.LineTo(18.5, 5.3); cr.LineTo(14.5, 3.5);
        cr.NewSubPath();
        cr.Arc(12, 13, 2, 0, 2 * Math.PI);              // target
        cr.MoveTo(12, 9.8);   cr.LineTo(12, 10.8);
        cr.MoveTo(12, 15.2);  cr.LineTo(12, 16.2);
        cr.MoveTo(8.8, 13);   cr.LineTo(9.8, 13);
        cr.MoveTo(14.2, 13);  cr.LineTo(15.2, 13);
    }

    // clipboard chart with a check and answer lines
    private static void Chart(Cairo.Context cr)
    {
        cr.Rectangle(9, 3, 6, 3);                       // clip
        cr.MoveTo(7, 4.5);  cr.LineTo(6, 4.5);          // board
        cr.ArcNegative(6, 5.5, 1, -Math.PI / 2, -Math.PI);
        cr.LineTo(5, 20);
        cr.ArcNegative(6, 20, 1, Math.PI, Math.PI / 2);
        cr.LineTo(18, 21);
        cr.ArcNegative(18, 20, 1, Math.PI / 2, 0);
        cr.LineTo(19, 5.5);
        cr.ArcNegative(18, 5.5, 1, 0, -Math.PI / 2);
        cr.LineTo(17, 4.5);
        cr.MoveTo(8.5, 11); cr.LineTo(10.3, 12.8); cr.LineTo(14, 9);   // check
        cr.MoveTo(8.5, 16.5); cr.LineTo(15.5, 16.5);
        cr.MoveTo(8.5, 19);   cr.LineTo(13, 19);
    }

    // cog wheel: 8 trapezoid teeth around a ring, hole in the middle
    private static void Cog(Cairo.Context cr)
    {
        const int teeth = 8;
        const double cx = 12, cy = 12, rOut = 10.5, rIn = 8;
        double step = 2 * Math.PI / teeth;
        double halfOut = step * 0.20, halfIn = step * 0.30;

        for (int i = 0; i < teeth; i++)
        {
            double a = i * step - Math.PI / 2;
            double a1 = a - halfIn, a2 = a - halfOut, a3 = a + halfOut, a4 = a + halfIn;
            if (i == 0) cr.MoveTo(cx + rIn * Math.Cos(a1), cy + rIn * Math.Sin(a1));
            else        cr.LineTo(cx + rIn * Math.Cos(a1), cy + rIn * Math.Sin(a1));
            cr.LineTo(cx + rOut * Math.Cos(a2), cy + rOut * Math.Sin(a2));
            cr.LineTo(cx + rOut * Math.Cos(a3), cy + rOut * Math.Sin(a3));
            cr.LineTo(cx + rIn * Math.Cos(a4),  cy + rIn * Math.Sin(a4));
            cr.Arc(cx, cy, rIn, a4, a + step - halfIn);   // ring to the next tooth
        }
        cr.ClosePath();
        cr.NewSubPath();
        cr.Arc(cx, cy, 3.2, 0, 2 * Math.PI);
    }

    // power symbol
    private static void Power(Cairo.Context cr)
    {
        cr.Arc(12, 13, 8.5, -Math.PI / 2 + 0.75, 3 * Math.PI / 2 - 0.75);
        cr.MoveTo(12, 2.5); cr.LineTo(12, 11.5);
    }

    // ‹ chevron
    private static void Back(Cairo.Context cr)
    {
        cr.MoveTo(15, 5); cr.LineTo(8, 12); cr.LineTo(15, 19);
    }

    // ▶ outline
    private static void Play(Cairo.Context cr)
    {
        cr.MoveTo(7, 4.5); cr.LineTo(19.5, 12); cr.LineTo(7, 19.5);
        cr.ClosePath();
    }
}
