import 'dart:math';

import 'package:flutter/material.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 ImageExt on Image {
  Widget circular() {
    return ClipRRect(
      clipBehavior: Clip.hardEdge,
      borderRadius: BorderRadius.circular(100),
      child: this,
    );
  }
}

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

  Widget sized({double? width, double? height}) {
    return SizedBox(
      width: width,
      height: height,
      child: this,
    );
  }
  Widget inkWell({required VoidCallback onTap,double radius=0}){
    return InkWell(
      child: Material(child: this,color: Colors.transparent,),
      onTap: onTap,
    );
  }
  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 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);
}
