import 'package:flutter/material.dart';
import 'package:satu/core/models/dictionary/category/category_response.dart';
import 'package:satu/core/models/dictionary/contragent/contragent_response_entity.dart';
import 'package:satu/core/models/dictionary/good/good_response_entity.dart';
import 'package:satu/views/dictionaries/category/category_edit.dart';
import 'package:satu/views/dictionaries/category/category_select_view.dart';
import 'package:satu/views/dictionaries/contragents/contragents_edit.dart';
import 'package:satu/views/dictionaries/goods/goods_edit.dart';
import 'package:satu/views/login/login_view.dart';
import 'package:satu/views/main/main_view.dart';
import 'package:satu/views/settings/printer_bluetooth/printer_encoding_select.dart';
import 'package:satu/views/settings/printer_bluetooth/printer_paper_size_select.dart';
import 'package:satu/views/settings/printer_bluetooth/printer_select.dart';
import 'package:satu/views/settings/printer_bluetooth/printer_view.dart';
import 'package:satu/views/work/views/add_by_barcode/add_by_barcode_view.dart';
import 'package:satu/views/work/views/add_product/add_product_view.dart';
import 'package:satu/views/work/views/contragent/select_contragent_view.dart';
import 'package:satu/views/work/views/payment/payment_view.dart';
import 'package:satu/views/work/views/receipt/receipt_view.dart';
import 'package:satu/views/work/work_view.dart';

import './route_names.dart';

Route<dynamic> generateRoute(RouteSettings settings) {
  switch (settings.name) {
    case loginViewRoute:
      //LoginModel model = settings.arguments as LoginModel;
      return _getPageRoute(
        routeName: settings.name,
        viewToShow: LoginView(),
      );
    case workViewRoute:
      return _getPageRoute(
        routeName: settings.name,
        viewToShow: const WorkView(),
      );
    case mainViewRoute:
      return _getPageRoute(
        routeName: settings.name,
        viewToShow: MainView(),
      );
    case addProductViewRoute:
      return _getPageRoute(
        routeName: settings.name,
        viewToShow: AddProductView(),
      );
    case addByBarcodeViewRoute:
      return _getPageRoute(
        routeName: settings.name,
        viewToShow: const AddByBarcodeView(),
      );
    case settingPrinterBluetoothViewRoute:
      return _getPageRoute(
        routeName: settings.name,
        viewToShow: const PrinterView(),
      );
    case settingPrinterBluetoothSelectViewRoute:
      return _getPageRoute(
        routeName: settings.name,
        viewToShow: const PrinterSelect(),
      );
    case settingPrinterPaperSizeViewRoute:
      return _getPageRoute(
        routeName: settings.name,
        viewToShow: const PrinterPaperSizeView(),
      );
    case settingPrinterEncodingViewRoute:
      return _getPageRoute(
        routeName: settings.name,
        viewToShow: const PrinterEncodingView(),
      );
    case contragentSelectViewRoute:
      return _getPageRoute(
        routeName: settings.name,
        viewToShow: SelectContragentView(),
      );
    case contragentEditRoute:
      final ContragentResponseEntity contragent =
          settings.arguments! as ContragentResponseEntity;
      return _getPageRoute(
        routeName: settings.name,
        viewToShow: ContragentEdit(contragent: contragent),
      );
    case paymentViewRoute:
      return _getPageRoute(
        routeName: settings.name,
        viewToShow: const PaymentView(),
      );
    case categoryEditRoute:
      final CategoryResponse category = settings.arguments! as CategoryResponse;
      return _getPageRoute(
        routeName: settings.name,
        viewToShow: CategoryEdit(
          category: category,
        ),
      );
    case categorySelectViewRoute:
      return _getPageRoute(
        routeName: settings.name,
        viewToShow: CategorySelectView(),
      );
    case goodsEditRoute:
      final GoodResponseEntity good = settings.arguments! as GoodResponseEntity;
      return _getPageRoute(
        routeName: settings.name,
        viewToShow: GoodEdit(
          good: good,
        ),
      );

    case receiptViewRoute:
      final int data = settings.arguments! as int;
      //return SlideRightRoute(widget: ImageShowContainer(data));
      return _getPageRoute(
        routeName: settings.name,
        viewToShow: ReceiptView(
          transactionId: data,
        ),
      );
    default:
      return MaterialPageRoute(
          builder: (_) => Scaffold(
                body: Center(
                    child: Text('No route defined for ${settings.name}')),
              ));
  }
}

PageRoute _getPageRoute({String? routeName, Widget? viewToShow}) {
  return MaterialPageRoute(
      settings: RouteSettings(
        name: routeName,
      ),
      builder: (_) => viewToShow!);
}

class SlideRightRoute extends PageRouteBuilder {
  SlideRightRoute({this.widget})
      : super(
          pageBuilder: (BuildContext context, Animation<double> animation,
              Animation<double> secondaryAnimation) {
            return widget!;
          },
          transitionsBuilder: (BuildContext context,
              Animation<double> animation,
              Animation<double> secondaryAnimation,
              Widget child) {
            return SlideTransition(
              position: Tween<Offset>(
                begin: const Offset(1.0, 0.0),
                end: Offset.zero,
              ).animate(animation),
              child: child,
            );
          },
        );
  final Widget? widget;
}
