import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:mc_common_app/classes/consts.dart';
import 'package:mc_common_app/theme/colors.dart';
import 'package:provider/provider.dart';

extension ExtendedText on Widget {
  showOverlay({double? width, double? height}) {
    return Container(
      width: width ?? double.infinity,
      height: height ?? 60,
      decoration: BoxDecoration(
        gradient: LinearGradient(colors: [
          Colors.black.withOpacity(0.2),
          Colors.black.withOpacity(0.1),
          Colors.black.withOpacity(0.004),
        ], begin: Alignment.topCenter, end: Alignment.bottomCenter, tileMode: TileMode.clamp),
      ),
      child: this,
    );
  }
}

extension ContainerExt on Widget {
  Widget toWhiteContainer({required double width, required double allPading, EdgeInsets? margin, VoidCallback? onTap}) {
    return InkWell(
      onTap: onTap,
      child: Container(
        decoration: const BoxDecoration(
          color: MyColors.white,
          boxShadow: [
            BoxShadow(
              blurRadius: 8,
              spreadRadius: 3,
              offset: Offset(0, 3),
              color: MyColors.greyShadowColor,
            ),
          ],
        ),
        padding: EdgeInsets.all(allPading),
        margin: margin,
        child: this,
      ),
    );
  }

  // Widget toContainer({required double borderRadius, required Color backgroundColor, required EdgeInsetsGeometry padding}) {
  //   return Container(
  //     decoration: BoxDecoration(color: backgroundColor, borderRadius: BorderRadius.circular(borderRadius)),
  //     padding: padding,
  //     child: this,
  //   );
  // }

  Widget toContainer({
    double borderRadius = 8,
    double paddingAll = 10,
    double marginAll = 0,
    EdgeInsetsGeometry? margin,
    EdgeInsetsGeometry? padding,
    Color backgroundColor = Colors.white,
    bool isShadowEnabled = false,
    bool isEnabledBorder = false,
    Color borderColor = Colors.black,
    double borderWidget = 1,
    double? width,
    double? height,
  }) =>
      Container(
        decoration: BoxDecoration(
          color: backgroundColor,
          borderRadius: BorderRadius.circular(borderRadius),
          border: !isEnabledBorder ? null : Border.all(color: borderColor, width: borderWidget),
          boxShadow: !isShadowEnabled
              ? null
              : [
                  BoxShadow(
                    color: const Color(0xff000000).withOpacity(.05),
                    blurRadius: 26,
                    offset: const Offset(0, -3),
                  ),
                ],
        ),
        padding: padding ?? EdgeInsets.all(paddingAll),
        margin: margin ?? EdgeInsets.all(marginAll),
        width: width,
        height: height,
        child: this,
      );
}

extension ImageExt on Image {
  Widget toCircle({required double borderRadius}) {
    return ClipRRect(
      clipBehavior: Clip.hardEdge,
      borderRadius: BorderRadius.circular(borderRadius),
      child: this,
    );
  }
}

extension ActionIcon on Widget {
  Widget toCircleContainer() {
    return Container(
      height: 44,
      width: 44,
      decoration: BoxDecoration(
        shape: BoxShape.circle,
        border: Border.all(color: MyColors.lightTextColor),
      ),
      child: this,
    );
  }
}

extension WidgetExt on Widget {
  Widget toWidget() {
    return this;
  }

  Widget sized({double? width, double? height}) {
    return SizedBox(
      width: width,
      height: height,
      child: this,
    );
  }

  Widget inkWell({required VoidCallback onTap, double radius = 0}) {
    return InkWell(
      onTap: onTap,
      child: Material(
        color: Colors.transparent,
        child: this,
      ),
    );
  }

  Widget sizeSq(double size) {
    return SizedBox(
      width: size,
      height: size,
      child: this,
    );
  }

  Widget fillParent({double hFactor = 1, double? vFactor}) {
    return FractionallySizedBox(
      heightFactor: null,
      widthFactor: hFactor,
      child: this,
    );
  }

  Widget margin({double top = 0, double bottom = 0, double left = 0, double right = 0}) {
    var pad = EdgeInsets.only(top: top, left: left, bottom: bottom, right: right);
    try {
      (this as dynamic).margin = pad;
    } catch (err) {
      return Padding(
        padding: pad,
        child: this,
      );
    }
    return this;
  }

  Widget toClip({double r = 40}) {
    return ClipRRect(
      borderRadius: BorderRadius.all(
        Radius.circular(r),
      ),
      clipBehavior: Clip.antiAlias,
      child: this,
    );
  }

  // Widget scrollable({Axis? direction, bool fadingEdge = true}) {
  //   var scrollview = SingleChildScrollView(
  //     child: this,
  //     controller: ScrollController(),
  //     scrollDirection: direction ?? Axis.vertical,
  //   );
  //   return fadingEdge ? scrollview.fadingEdge() : scrollview;
  // }

  Widget expand() {
    return Expanded(
      child: this,
    );
  }

  Widget align(Alignment alignment) {
    return Align(
      alignment: alignment,
      child: this,
    );
  }

  Widget center() {
    return Center(
      child: this,
    );
  }

  Widget padding(EdgeInsets padding) {
    return Padding(
      padding: padding,
      child: this,
    );
  }

  Widget paddingAll(double padding) {
    return Padding(
      padding: EdgeInsets.all(padding),
      child: this,
    );
  }

  Widget horPaddingMain() {
    return Padding(
      padding: const EdgeInsets.symmetric(horizontal: 21),
      child: this,
    );
  }

  Widget paddingOnly({double left = 0.0, double right = 0.0, double top = 0.0, double bottom = 0.0}) =>
      Padding(padding: EdgeInsets.only(left: left, right: right, top: top, bottom: bottom), child: this);

  // Widget onTap(VoidCallback onTap, {double corners = 0}) {
  //   return Clickable.widget(child: this, corners: corners, onTap: onTap);
  // }

  Widget ignoreInteraction() {
    return IgnorePointer(
      child: this,
    );
  }
}

// extension xScrollView on ScrollView {
//   Widget fadingEdge() => FadingEdgeScrollView.fromScrollView(
//     child: this,
//     gradientFractionOnEnd: 0.08,
//     gradientFractionOnStart: 0.08,
//     shouldDisposeScrollController: true,
//   );
// }
//
// extension x2ScrollView on SingleChildScrollView {
//   Widget fadingEdge() => FadingEdgeScrollView.fromSingleChildScrollView(
//     child: this,
//     gradientFractionOnEnd: 0.08,
//     gradientFractionOnStart: 0.08,
//     shouldDisposeScrollController: true,
//   );
// }

class Position {
  final double x, y, w, h;

  Position(this.x, this.y, this.w, this.h);
}

extension KeyExt on GlobalKey {
  Position globalPosition() {
    RenderBox box = currentContext!.findRenderObject() as RenderBox;
    Offset xy = box.localToGlobal(Offset.zero);
    Size wh = box.size;
    return Position(xy.dx, xy.dy, wh.width, wh.height);
  }
}

extension StateExt on State {
  reload({VoidCallback? b}) {
    setState(b ?? () {});
  }
}

// extension LocatiionExt on Location {
//   LatLng toLatLng() {
//     return LatLng(lat!, lng!);
//   }
// }

// extension xList<T> on List<T> {
//   T randomItem() {
//     final random = new Random();
//     var i = random.nextInt(this.length);
//     return this[i];
//   }
//
//   List<T> append(List<T> items) {
//     this.addAll(items);
//     return this;
//   }
//
//   bool isFirst(T item) => first == item;
//
//   bool isLast(T item) => last == item;
//
//   getOneByOne(
//       {required int delayMillis,
//       required Function(T) callback, VoidCallback? complete}) async {
//     for (var i in this) {
//       await delay(delayMillis);
//       callback(i);
//     }
//     complete!();
//   }
// }

extension xFuture<T> on Future<T?> {
  thenWithDelay(int millis, Function(T) thenBlock) {
    then((value) {
      Future.delayed(Duration(milliseconds: millis)).then((value) => thenBlock(value));
    });
  }
}

extension xDouble on int {
  Duration durationMillis() => Duration(milliseconds: this);

  Duration durationSec() => Duration(seconds: this);

  Duration durationHour() => Duration(hours: this);
}

extension BuildSVG on String {
  Widget buildSvg({double? height, double? width, BoxFit fit = BoxFit.contain}) {
    return SvgPicture.asset(
      this,
      fit: fit,
      height: height,
      width: width,
    );
  }
}

extension LocaleSetup on MultiProvider {
  Widget setupLocale() {
    return EasyLocalization(
        supportedLocales: MyLocales.supportedLocales,
        fallbackLocale: MyLocales.fallBackLocale,
        startLocale: MyLocales.startLocale,
        assetLoader: MyLocales.assetLoader,
        path: MyLocales.localesAssetPath,
        child: this);
  }
}

extension WidgetExtensions on Widget {
  Widget onPress(VoidCallback onTap) => InkWell(onTap: onTap, child: this);
}
