import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:tp5/fltinfo/widget/w_cadre.dart';
import 'package:tp5/lido/lido_api.dart';
import 'package:tp5/lido/model/Lidoapi_ofp.dart';

class LidoofpPage extends ConsumerStatefulWidget {
  const LidoofpPage({super.key, required this.params});
  final LidoofpPageParams params;

  @override
  LidoofpPageState createState() => LidoofpPageState();
}

class LidoofpPageState extends ConsumerState<LidoofpPage> {
  @override
  void initState() {
    super.initState();
  }

  Map<String, dynamic> ofpcat = {};
  Map<String, String> ofpcattype = {};

  String? _getCatName(String? cat) {
    var json = widget.params.lidoofp?.categoriesNames?.toJson() ?? {};
    json.addAll({
      "WXSIGWX": "Significant WX",
      "CREWINFO": "Crew Information",
      "IWFR": "Weather",
      "NOTAM": "NOTAM",
      "ATS": "ATC Flight Plan",
      "DISP": "Dispatch Information",
      "VERTPROF": "Vertical Profile",
      "DMS": "Additional Docs",
      "ABD": "Additional Briefing Documents",
      "UAD": "UAD Maps",
      "DISMAP": "Dispatcher Maps",
      "OFP": "OFP",
      "WXNOTAM": "WX/NOTAM",
      "ADDWXNOTAM": "WX/NOTAM",
      "WXSATWX": "Satellite WX",
      "RAIM": "RAIM"
    });
    if (json.containsKey(cat ?? "")) {
      return json[cat];
    }
    return null;
  }

  @override
  Widget build(BuildContext context) {
    final params = widget.params;

    // log(params.lidoofp?.briefingPackages.first.categories.map((e)=>e.name).toString() ?? "null",
    //     name: "LidoofpPage");
    return Scaffold(
      appBar: AppBar(
          title: Text(params.title ??
              params.lidoofp?.briefingPackages.firstOrNull?.leg.first
                  .legidentifier ??
              "---")),
      body: params.lidoofp == null
          ? const Text("No OFP found")
          : ListView(
              children: params.lidoofp?.categories
                      .map((cat) => Padding(
                          padding: const EdgeInsets.symmetric(
                              vertical: 10, horizontal: 5),
                          child: WCadre(
                              title: "${_getCatName(cat.name) ?? {cat.name}}",
                              child: InkWell(
                                  onTap: () async {
                                    final link =
                                        cat.documents.first.links?.self?.href ??
                                            cat.links?.self?.href ??
                                            "nolink";
                                    print(
                                        "lidoofppage: build: onTap: link: $link");
                                    final out = await ref
                                        .read(lidoapiProvider)
                                        .getDoc(link,
                                            mediaType:
                                                cat.documents.first.mediaType);
                                    setState(() {
                                      ofpcat[cat.name ?? "xxxx"] = out;
                                      ofpcattype[cat.name ?? "xxxx"] =
                                          cat.documents.first.mediaType ?? "";
                                    }); //print(out);
                                  },
                                  child: ofpcat.containsKey(cat.name ?? "xxx")
                                      ? Text(ofpcat[cat.name ?? "xxxx"]["data"]
                                          .toString())
                                      : const Text("Tap to load content...")
                                  // Text(
                                  //     "${cat.name}: ${_getCatName(cat.name)}\n    docs: ${cat.documents.first.mediaType} ${cat.documents.first.links?.self}\n    link: ${cat.links?.self}")

                                  ))))
                      .toList() ??
                  [const Text("No categories found on OFP")]),
    );
  }
}

class LidoofpPageParams {
  const LidoofpPageParams({this.lidoofp, this.title});
  final String? title;
  final LidoapiOfp? lidoofp;
}
