// ignore_for_file: must_be_immutable

import 'dart:convert';

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:general_package/apiservices/apiservices.dart';
import 'package:general_package/authservices/auth_service.dart';
import 'package:general_package/responsive/responsive.dart';
import 'package:general_package/themes/themes.dart';
import 'package:general_package/dynamic_form/dynamic_form.dart';
import 'package:get/get.dart';
import 'package:get_storage/get_storage.dart';
import 'package:simple_barcode_scanner/simple_barcode_scanner.dart';

class DynamicFormButton extends StatefulWidget {
  DynamicFormButton({super.key, required this.dynamicFormButtonModel});
  DynamicFormButtonModel dynamicFormButtonModel;
  static bgButton(BuildContext context, String text,
      {EdgeInsetsGeometry? padding,
      double? width,
      Color? backgroundColor,
      Gradient? gradient,
      Color? color,
      double? borderRadius,
      double? fontSize,
      FontWeight? fontWeight,
      double? elevation,
      bool? isDisabled,
      Function()? onPressed}) {
    return Container(
      width: (width != null) ? width : ScreenTypeData.fullWidth(context) * .7,
      decoration: BoxDecoration(
          borderRadius:
              BorderRadius.circular((borderRadius != null) ? borderRadius : 29),
          color: backgroundColor,
          gradient: (backgroundColor != null)
              ? null
              : (gradient != null)
                  ? gradient
                  : (isDisabled!=null&&isDisabled)
                      ? AppTheme.disableGradientColor
                      : AppTheme.primaryGradientColor),
      child: TextButton(
          onPressed:(isDisabled!=null&&isDisabled)? null:onPressed,
          style: TextButton.styleFrom(
              elevation: (elevation != null) ? elevation : 0,
              padding: (padding != null)
                  ? padding
                  : const EdgeInsets.symmetric(vertical: 20, horizontal: 20),
              textStyle: TextStyle(
                  fontSize: (fontSize != null) ? fontSize : 18,
                  fontWeight:
                      (fontWeight != null) ? fontWeight : FontWeight.bold)),
          child: Text(
            text,
            textAlign: TextAlign.center,
            style:
                TextStyle(color: (color != null) ? color : AppTheme.whiteColor),
          )),
    );
  }

  static boarderButton(BuildContext context, String text,
      {EdgeInsetsGeometry? padding,
      double? width,
      Color? backgroundColor,
      Color? foregroundColor,
      Color? color,
      double? borderRadius,
      double? fontSize,
      FontWeight? fontWeight,
      double? elevation,
      Function()? onPressed}) {
    return SizedBox(
      width: (width != null) ? width : ScreenTypeData.fullWidth(context) * .7,
      child: TextButton(
          onPressed: onPressed,
          style: TextButton.styleFrom(
              elevation: (elevation != null) ? elevation : 0,
              backgroundColor: (backgroundColor != null)
                  ? backgroundColor
                  : AppTheme.whiteColor,
              foregroundColor: (foregroundColor != null)
                  ? foregroundColor
                  : AppTheme.primaryColor,
              padding: (padding != null)
                  ? padding
                  : const EdgeInsets.symmetric(vertical: 20, horizontal: 20),
              shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.circular(
                    (borderRadius != null) ? borderRadius : 29),
              ),
              side: BorderSide(
                  color: (foregroundColor != null)
                      ? foregroundColor
                      : AppTheme.primaryColor,
                  width: 2),
              textStyle: TextStyle(
                  fontSize: (fontSize != null) ? fontSize : 18,
                  fontWeight:
                      (fontWeight != null) ? fontWeight : FontWeight.bold)),
          child: Text(
            text,
            textAlign: TextAlign.center,
            style:
                TextStyle(color: (color != null) ? color : AppTheme.blackColor),
          )),
    );
  }

  static textButton(BuildContext context, String text,
      {EdgeInsetsGeometry? padding,
      Color? color,
      double? fontSize,
      FontWeight? fontWeight,
      Function()? onPressed}) {
    return Container(
      padding: padding,
      child: InkWell(
          onTap: onPressed,
          // onPressed: onPressed,
          // style: TextButton.styleFrom(
          //     padding: (padding != null)
          //         ? padding
          //         : const EdgeInsets.symmetric(vertical: 20, horizontal: 20),
          //     textStyle: TextStyle(
          //         fontSize: (fontSize != null) ? fontSize : 18,
          //         fontWeight:
          //             (fontWeight != null) ? fontWeight : FontWeight.bold)),
          child: Text(
            text,
            textAlign: TextAlign.center,
            style: TextStyle(
                color: (color != null) ? color : AppTheme.primaryColor,
                fontSize: (fontSize != null) ? fontSize : 16,
                fontWeight: (fontWeight != null) ? fontWeight : FontWeight.bold),
          )),
    );
  }

  @override
  State<DynamicFormButton> createState() => _DynamicFormButton();
}

class _DynamicFormButton extends State<DynamicFormButton> {
  @override
  Widget build(BuildContext context) {
    Function() onPressFunction(DynamicFormButtonOnPressEnum onPressedEnum) {
      switch (onPressedEnum) {
        case DynamicFormButtonOnPressEnum.postData:
          return () {
            if (widget.dynamicFormButtonModel.onSubmit != null) {
              APIService(context)
                  .postData(widget.dynamicFormButtonModel.onSubmit!,
                      widget.dynamicFormButtonModel.postData)
                  .then((value) {
                if (value != null) {
                  final box = GetStorage();
                  if (value["token"] != null) {
                    // box.write('token', value["token"]);
                    authService.login(value["token"]);
                  }
                  if (value["redirectUrl"] != null) {
                    if (value["token"] != null) {
                      Get.offAllNamed(value["redirectUrl"]);
                    } else {
                      Get.toNamed(value["redirectUrl"]);
                    }
                  }
                }
              });
            } else {
              if (kDebugMode) {
                print("Please provide URL");
              }
            }
            return "Done";
          };
        case DynamicFormButtonOnPressEnum.scanAndPostData:
          return () async {
            if (widget.dynamicFormButtonModel.onSubmit != null) {
              var res = await Navigator.push(
                  context,
                  MaterialPageRoute(
                    builder: (context) => const SimpleBarcodeScannerPage(),
                  ));
              widget.dynamicFormButtonModel.postData
                  .addAll({'scanData': res.toString()});
              APIService(context)
                  .postData(widget.dynamicFormButtonModel.onSubmit!,
                      widget.dynamicFormButtonModel.postData)
                  .then((value) {
                if (value != null) {
                  final box = GetStorage();
                  if (value["token"] != null) {
                    // box.write('token', value["token"]);
                    authService.login(value["token"]);
                  }
                  if (value["redirectUrl"] != null) {
                    if (value["token"] != null) {
                      Get.offAllNamed(value["redirectUrl"]);
                    } else {
                      Get.toNamed(value["redirectUrl"]);
                    }
                  }
                }
              });
            } else {
              if (kDebugMode) {
                print("Please provide URL");
              }
            }
            return "Done";
          };

        case DynamicFormButtonOnPressEnum.backPage:
          return () {
            Get.back();
            return "Done";
          };
        case DynamicFormButtonOnPressEnum.movePage:
          return () {
            Get.toNamed(widget.dynamicFormButtonModel.onSubmit!);
            return "Done";
          };
        case DynamicFormButtonOnPressEnum.disable:
          print(widget.dynamicFormButtonModel.title);
          return () => null;
        // Navigator.pushNamed(
        // context, widget.dynamicFormButtonModel.onSubmit!);
        default:
          return () {
            APIService(context).postData(
                widget.dynamicFormButtonModel.onSubmit!,
                widget.dynamicFormButtonModel.postData);
            return "Done";
          };
      }
    }

    Widget formTypes(onPressedEnum) {
      switch (onPressedEnum) {
        case DynamicFormButtonTypeEnum.bg:
          return DynamicFormButton.bgButton(
              context, widget.dynamicFormButtonModel.title,
              isDisabled: widget.dynamicFormButtonModel.isDisabled,
              padding: widget.dynamicFormButtonModel.padding,
              width: widget.dynamicFormButtonModel.width,
              backgroundColor: widget.dynamicFormButtonModel.backgroundColor,
              color: widget.dynamicFormButtonModel.color,
              borderRadius: widget.dynamicFormButtonModel.borderRadius,
              fontSize: widget.dynamicFormButtonModel.fontSize,
              fontWeight: widget.dynamicFormButtonModel.fontWeight,
              elevation: widget.dynamicFormButtonModel.elevation,
              onPressed:
                  onPressFunction(widget.dynamicFormButtonModel.onPressed));
        case DynamicFormButtonTypeEnum.boarder:
          return DynamicFormButton.boarderButton(
              context, widget.dynamicFormButtonModel.title,
              padding: widget.dynamicFormButtonModel.padding,
              width: widget.dynamicFormButtonModel.width,
              backgroundColor: widget.dynamicFormButtonModel.backgroundColor,
              foregroundColor: widget.dynamicFormButtonModel.foregroundColor,
              color: widget.dynamicFormButtonModel.color,
              borderRadius: widget.dynamicFormButtonModel.borderRadius,
              fontSize: widget.dynamicFormButtonModel.fontSize,
              fontWeight: widget.dynamicFormButtonModel.fontWeight,
              elevation: widget.dynamicFormButtonModel.elevation,
              onPressed:
                  onPressFunction(widget.dynamicFormButtonModel.onPressed));
        case DynamicFormButtonTypeEnum.text:
          return DynamicFormButton.textButton(
              context, widget.dynamicFormButtonModel.title,
              color: widget.dynamicFormButtonModel.color,
              fontSize: widget.dynamicFormButtonModel.fontSize,
              fontWeight: widget.dynamicFormButtonModel.fontWeight,
              padding:  widget.dynamicFormButtonModel.padding,
              onPressed:
                  onPressFunction(widget.dynamicFormButtonModel.onPressed));

        default:
          return DynamicFormButton.bgButton(
              context, widget.dynamicFormButtonModel.title,
              isDisabled: widget.dynamicFormButtonModel.isDisabled,
              padding: widget.dynamicFormButtonModel.padding,
              width: widget.dynamicFormButtonModel.width,
              backgroundColor: widget.dynamicFormButtonModel.backgroundColor,
              color: widget.dynamicFormButtonModel.color,
              borderRadius: widget.dynamicFormButtonModel.borderRadius,
              fontSize: widget.dynamicFormButtonModel.fontSize,
              fontWeight: widget.dynamicFormButtonModel.fontWeight,
              elevation: widget.dynamicFormButtonModel.elevation,
              onPressed:
                  onPressFunction(widget.dynamicFormButtonModel.onPressed));
      }
    }

    return formTypes(widget.dynamicFormButtonModel.type);
  }
}
