import 'package:flutter/material.dart';

MaterialColor createMaterialColor(Color color) {
  List strengths = <double>[.05];
  final swatch = <int, Color>{};
  final int r = (color.r * 255.0).round() & 0xff,
      g = (color.g * 255.0).round() & 0xff,
      b = (color.b * 255.0).round() & 0xff;

  for (int i = 1; i < 10; i++) {
    strengths.add(0.1 * i);
  }
  for (var strength in strengths) {
    final double ds = 0.5 - strength;
    swatch[(strength * 1000).round()] = Color.fromRGBO(
      r + ((ds < 0 ? r : (255 - r)) * ds).round(),
      g + ((ds < 0 ? g : (255 - g)) * ds).round(),
      b + ((ds < 0 ? b : (255 - b)) * ds).round(),
      1,
    );
  }
  return MaterialColor(color.toARGB32(), swatch);
}

// ── Main scheme ───────────────────────────────────────────────────────────────
// Grounded reads as an institution, not a toy: near-black ink, one hard
// accent, and a warning palette that carries the enforcement tiers.
var colorWhite = const Color(0xFFFFFFFF);
var colorPrimary = const Color(0xFF2F6F4E);
var colorPrimaryDark = const Color(0xFF141414);
var colorPrimaryDark2 = const Color(0xFF232323);
var colorPrimaryLight = const Color(0xFFEDEEEA);
var colorPrimaryLight2 = const Color(0xFFF4F5F2);

var colorAccent = const Color(0xFFB5442F);
var colorMilkWhite = const Color(0xFFD9D8CE);
var colorMuted = const Color(0xFFE6EAE6);
var colorSecondary = const Color(0xFFF1F4F1);

var colorPositive = const Color(0xFF2F6F4E);
var colorNegative = const Color(0xFFB3261E);
var colorGrey = const Color(0xFFA8A8A8);
var colorGrey2 = const Color(0xFF6E6E6E);
var colorGrey3 = const Color(0xFF8A8A8A);
var colorTinted = const Color(0xFFE0A88F);
var colorWarmYellow = const Color(0xFFD9A404);
var colorReddish = const Color(0xFFC0392B);
var colorDarkBlue = const Color(0xFF10161F);

// ── Semantic ──────────────────────────────────────────────────────────────────
const Color colorSuccess = Color(0xFF2E7D4F);
const Color colorWarning = Color(0xFFC98A04);
const Color colorDestructive = Color(0xFFA32C1C);
const Color colorBlack = Color(0xFF000000);
const Color colorBorder = Color(0x14000000);
const Color colorDivider = Color(0x12000000);

// ── Standing tiers ────────────────────────────────────────────────────────────
// Each standing has one colour used consistently everywhere it appears, so the
// tier is legible at a glance without reading the label.
const Color colorStandingGood = Color(0xFF2F6F4E);
const Color colorStandingGoodBg = Color(0xFFE4EFE8);
const Color colorStandingWarned = Color(0xFFC98A04);
const Color colorStandingWarnedBg = Color(0xFFFAF0D8);
const Color colorStandingGrounded = Color(0xFFB5442F);
const Color colorStandingGroundedBg = Color(0xFFF7E3DE);
const Color colorStandingLockdown = Color(0xFF7A1F14);
const Color colorStandingLockdownBg = Color(0xFFEFD6D2);

// ── Commitment classes ────────────────────────────────────────────────────────
const Color colorClassNonNegotiable = Color(0xFF7A1F14);
const Color colorClassNonNegotiableBg = Color(0xFFF3DFDB);
const Color colorClassStandard = Color(0xFF2C4A63);
const Color colorClassStandardBg = Color(0xFFE0E8EF);
const Color colorClassElective = Color(0xFF6E6E6E);
const Color colorClassElectiveBg = Color(0xFFEDEDED);

// ── Debt / charting ───────────────────────────────────────────────────────────
const Color colorDebtLine = Color(0xFFB5442F);
const Color colorDebtFill = Color(0x1AB5442F);
const Color colorChartGrid = Color(0x0F000000);
const Color colorChartAxis = Color(0xFF8A8A8A);

// ── Surfaces ──────────────────────────────────────────────────────────────────
const Color colorCard = Color(0xFFFFFFFF);
const Color colorSheetBackground = Color(0xFFF6F5F2);
const Color colorInset = Color(0xFFFAFAF8);

// ── Dark surfaces ─────────────────────────────────────────────────────────────
const Color colorDarkBg = Color(0xFF080808);
const Color colorDarkCard = Color(0xFF141414);
const Color colorDarkSurface = Color(0xFF1C1C1C);
const Color colorDarkBorder = Color(0xFF262626);

Color getColorFromHex(String hexColor) {
  if (hexColor.isEmpty || hexColor.length <= 6) {
    hexColor = "#ffffff";
  }

  hexColor = hexColor.toUpperCase().replaceAll("#", "");

  if (hexColor.length == 6) {
    hexColor = "FF$hexColor";
  }

  try {
    return Color(int.parse(hexColor, radix: 16));
  } catch (e) {
    return colorWhite;
  }
}

String colorToHex(Color color) {
  return '#${color.toARGB32().toRadixString(16).padLeft(8, '0').toUpperCase()}';
}
