import 'package:ambito/src/domain/entity/baumarten/baumarten.dart';
import 'package:ambito/src/domain/entity/massnahme/massnahme.dart';
import 'package:ambito/src/packages/ambito_api/base.dart';
import 'package:ambito/src/pages/actions/actions_page.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_i18n/flutter_i18n.dart';
import 'package:get/get_navigation/src/root/get_material_app.dart';
import 'package:isar/isar.dart';
import 'package:logger/logger.dart';

final String AmbitoToken = 'TFxO7vzBLVRCu9I3VMoHmTuCvSu8aCDi';

final Logger logger = Logger(
  printer: PrettyPrinter(),
);

late Isar isar;

void main() async {
  WidgetsFlutterBinding.ensureInitialized();

  /*Isar.open(
    schemas: [BaumartenSchema, IdValueColorSchema, IdValueSchema],
    directory: '',
  );*/

  await Isar.initialize();
  const dir = Isar.sqliteInMemory;
  const engine = kIsWeb ? IsarEngine.sqlite : IsarEngine.isar;
  isar = Isar.open(
    schemas: [BaumartenSchema, MassnahmeSchema],
    directory: dir,
    engine: engine,
    inspector: true,
  );

  Future.wait([
    BaseApi().getContent('baumarten'),
    BaseApi().getContent('massnahmen'),
  ]);

  //await BaseApi().getContent('baumarten');
  //await BaseApi().getContent('massnahmen');

  runApp(const Ambito());
}

class Ambito extends StatelessWidget {
  const Ambito({super.key});
  @override
  Widget build(BuildContext context) {
    return GetMaterialApp(
      debugShowCheckedModeBanner: false,
      localizationsDelegates: [
        FlutterI18nDelegate(
          translationLoader: FileTranslationLoader(
            fallbackFile: 'de',
            basePath: 'i18n',
          ),
          missingTranslationHandler: (key, locale) {
            print("--- Missing Key: $key, languageCode: $locale");
          },
        ),
      ],
      title: 'Flutter Demo',
      locale: const Locale('de'),
      builder: FlutterI18n.rootAppBuilder(),
      theme: ThemeData(
        scaffoldBackgroundColor: Colors.white,
        useMaterial3: true,
      ),
      home: const MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key});

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  String activeLink = '';

  @override
  void initState() {
    activeLink = 'start';
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    double fontSize = 16;
    return Scaffold(
        body: Column(
      children: [
        SizedBox(height: 50),
        Row(
          mainAxisAlignment: MainAxisAlignment.spaceEvenly,
          children: [
            LinkButton('start'),
            LinkButton('database'),
            LinkButton('designer'),
            Image.asset(
              'images/logo.png',
              scale: 1.3,
            ),
            LinkButton('service'),
            LinkButton('network'),
            LinkButton('contact'),
          ],
        ),
        SizedBox(height: 50),
        Expanded(
          child: getContent(),
        ),
      ],
    ));
  }

  Widget getContent() {
    if (activeLink == 'database') {
      return ActionsPage();
    }
    return Text(activeLink);
  }

  Widget LinkButton(String link) {
    double fontSize = 16;
    return TextButton(
      style: ButtonStyle(
        overlayColor: MaterialStateProperty.resolveWith<Color>(
            (Set<MaterialState> states) {
          return Colors.white;
        }),
      ),
      onPressed: () {
        setState(() {
          activeLink = link;
        });
      },
      child: Text(
        context.translate('page.start.links.$link.title'),
        style: TextStyle(
          fontSize: fontSize,
          fontWeight: FontWeight.bold,
          color: (activeLink == link)
              ? Colors.grey.shade400
              : Colors.grey.shade800,
        ),
      ),
    );
  }
}

extension ContextI18n on BuildContext {
  translate(String key) {
    return FlutterI18n.translate(this, key);
  }
}
