import 'package:flutter/material.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:logging/logging.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';
import 'package:scanbot_sdk/scanbot_sdk_ui_v2.dart';

import '../../services/storage/expenses.dart';

part 'scan_screen.g.dart';

@riverpod
Future<String?> _scanDocument(Ref ref) async {
  var configuration = DocumentScanningFlow(
    appearance: DocumentFlowAppearanceConfiguration(
      statusBarMode: StatusBarMode.DARK,
    ),
    cleanScanningSession: true,
    outputSettings: DocumentScannerOutputSettings(pagesScanLimit: 1),
    screens: DocumentScannerScreens(
      review: ReviewScreenConfiguration(enabled: false),
    ),
  );
  var documentResult = await ScanbotSdkUiV2.startDocumentScanner(configuration);

  return documentResult.data?.pdfURI;
}

class ScanScreen extends HookConsumerWidget {
  @override
  Widget build(BuildContext context, WidgetRef ref) {
    final boredSuggestion = ref.watch(_scanDocumentProvider);

    restartScan() async {
      try {
        final val = ref.refresh(_scanDocumentProvider);
        Logger.root.info("Load again startup result: $val");
      } catch (e, s) {
        Logger.root.shout("Failed to try again startup loading! $e $s");
      }
    }

    // Perform a switch-case on the result to handle loading/error states
    return switch (boredSuggestion) {
      AsyncData(:final value) when value != null => ExpenseEditor(
        filePath: value,
        onFinished: (e) {},
      ),

      // No data
      AsyncData(:final value) when value == null => ScanErrorScreen(
        message: "No document scanned!",
        onTryAgain: restartScan,
      ),

      // Error
      AsyncError(:final error) => ScanErrorScreen(
        message: error.toString(),
        onTryAgain: restartScan,
      ),
      _ => const Center(child: CircularProgressIndicator()),
    };
  }
}

class ScanErrorScreen extends StatelessWidget {
  final String message;
  final Function() onTryAgain;

  const ScanErrorScreen({
    super.key,
    required this.message,
    required this.onTryAgain,
  });

  @override
  Widget build(BuildContext context) {
    return Center(
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          Spacer(flex: 5),
          Text("An error occurred while scanning"),
          Spacer(flex: 1),
          Text(message),
          Spacer(flex: 1),
          MaterialButton(
            onPressed: onTryAgain,
            child: Text("Try again".toUpperCase()),
          ),
          Spacer(flex: 5),
        ],
      ),
    );
  }
}

class ExpenseEditor extends HookWidget {
  final String filePath;
  final Function(Expense) onFinished;

  const ExpenseEditor({
    super.key,
    required this.filePath,
    required this.onFinished,
  });

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    throw UnimplementedError();
  }
}
