import 'dart:convert';
import 'dart:io';
import 'dart:typed_data';
import 'dart:ui';

import 'package:car_provider_app/config/constants.dart';
import 'package:car_provider_app/theme/colors.dart';
import 'package:car_provider_app/widgets/extensions/int_extensions.dart';
import 'package:car_provider_app/widgets/extensions/string_extensions.dart';
import 'package:flutter/material.dart';
import 'package:flutter_svg/svg.dart';
import 'package:path/path.dart' as p;
import 'package:sizer/sizer.dart';

Color getColorFromHex(String hexColor) {
  hexColor = hexColor.toUpperCase().replaceAll('#', '');

  if (hexColor.length == 6) {
    hexColor = 'FF' + hexColor;
  }

  return Color(int.parse(hexColor, radix: 16));
}

Widget spacerVertical(double v) {
  return Container(
    height: v,
    width: double.infinity,
  );
}

String convertFileToBase64(File file) {
  List<int> imageBytes = file.readAsBytesSync();
  return base64Encode(imageBytes);
}

String checkFileExt(String path) {
  String ex=p.extension(path);
  var parts = ex.split('.');
  return parts[1]??"png"; // '.dart'
}

Future delay(int seconds) async {
  return await Future.delayed(Duration(seconds: seconds));
}

inkWellCorner({double? r}) {
  return RoundedRectangleBorder(
    borderRadius: BorderRadius.circular(r ?? 4),
  );
}

Widget spacerHorizontal(double v) {
  return Container(
    height: v,
    width: v,
  );
}

Widget mDivider(Color color, {double? h}) {
  return Container(
    width: double.infinity,
    height: h ?? 1,
    color: color,
  );
}

Widget mDivider3({double? h}) {
  return Container(
    width: double.infinity,
    height: h ?? 1,
    color: borderLightColor!.withOpacity(0.7) ?? Colors.transparent,
  );
}

Widget mDivider2(Color color, double w) {
  return Container(
    width: w,
    height: 1,
    color: color,
  );
}

InputDecoration txtField(String label) {
  return new InputDecoration(
    border: InputBorder.none,
    focusedBorder: InputBorder.none,
    enabledBorder: InputBorder.none,
    errorBorder: InputBorder.none,
    hintText: label,
    hintStyle: TextStyle(color: Colors.grey),
    disabledBorder: InputBorder.none,
    isDense: false,
    contentPadding: EdgeInsets.only(left: 15, right: 15),
  );
}

Widget mFlex(int f) {
  return Flexible(
    flex: f,
    child: Container(
      width: double.infinity,
      height: double.infinity,
    ),
  );
}

Widget mExp(int f) {
  return Expanded(
    flex: f,
    child: Container(
      width: double.infinity,
    ),
  );
}

Widget mExpHorizontal(int f) {
  return Expanded(
    flex: f,
    child: Container(
      width: double.infinity,
    ),
  );
}

spacer() {
  return SizedBox(
    height: 8,
  );
}

Widget floatButton(String icon, {Color? color, required Function onClick, String? title}) {
  return Padding(
    padding: const EdgeInsets.only(top: 12, bottom: 12),
    child: Column(
      crossAxisAlignment: CrossAxisAlignment.center,
      mainAxisAlignment: MainAxisAlignment.center,
      children: [
        FloatingActionButton(
          onPressed: () {
            onClick();
          },
          heroTag: icon,
          backgroundColor: accentColor,
          elevation: 4,
          child: Container(
            child: SvgPicture.asset(
              categorySvgIcons + icon,
              color: color,
            ),
            width: double.infinity,
            height: double.infinity,
            decoration: containerRadius(Colors.white, 200),
            clipBehavior: Clip.antiAlias,
            padding: EdgeInsets.all(15),
            margin: EdgeInsets.all(1),
          ),
        ),
        if (title != null) 2.w.toInt().height,
        if (title != null) title.toText12(color: headingColor, isBold: true),
      ],
    ),
  );
}

navigateTo(context, page) {
  Navigator.push(context, MaterialPageRoute(builder: (context) => page));
}

circularImage(String im, double width, double height) {
  return new Container(width: 190.0, height: 190.0, decoration: new BoxDecoration(shape: BoxShape.circle, image: new DecorationImage(fit: BoxFit.fill, image: new AssetImage(im))));
}

circularImage2(String im, double width, double height) {
  return new Container(width: width, height: height, decoration: new BoxDecoration(shape: BoxShape.circle, image: new DecorationImage(fit: BoxFit.fill, image: new AssetImage(im))));
}

cardRadius(double radius) {
  return RoundedRectangleBorder(
    side: BorderSide(color: Colors.transparent, width: 1),
    borderRadius: BorderRadius.circular(radius),
  );
}

cardRadiusWithoutBorder(double radius) {
  return RoundedRectangleBorder(
    side: BorderSide(color: Colors.transparent, width: 1),
    borderRadius: BorderRadius.circular(radius),
  );
}

Image imageFromBase64String(String base64String) {
  return Image.memory(base64Decode(base64String));
}

Uint8List dataFromBase64String(String base64String) {
  return base64Decode(base64String);
}

String base64String(Uint8List data) {
  return base64Encode(data);
}

Widget overLayWidget({double? width, double? height, List<Color>? color}) {
  return Container(
    width: width ?? double.infinity,
    height: height ?? 60,
    decoration: BoxDecoration(
      gradient: LinearGradient(
        colors: color != null
            ? color
            : [
                Colors.black.withOpacity(0.2),
                Colors.black.withOpacity(0.1),
                Colors.black.withOpacity(0.004),
              ],
        begin: Alignment.topCenter,
        end: Alignment.bottomCenter,
        tileMode: TileMode.clamp,
      ),
    ),
  );
}

Decoration containerRadius(Color color, double r) {
  return BoxDecoration(
    color: color,
    borderRadius: BorderRadius.all(Radius.circular(r)),
  );
}

Decoration containerRadiusTop({Color? color, double? r}) {
  return BoxDecoration(
    color: color ?? Colors.white,
    borderRadius: BorderRadius.only(topRight: Radius.circular(r ?? 12), topLeft: Radius.circular(r ?? 12)),
  );
}

Decoration containerRadiusBorder(Color color, double r) {
  return BoxDecoration(
    color: Colors.transparent,
    border: Border.all(color: color, width: 1),
    borderRadius: BorderRadius.all(Radius.circular(r)),
  );
}

Decoration containerRadiusBottom(Color color, double r) {
  return BoxDecoration(
    color: color,
    borderRadius: BorderRadius.only(bottomLeft: Radius.circular(r), bottomRight: Radius.circular(r)),
  );
}

ShapeBorder cardRadiusTop(double radius) {
  return RoundedRectangleBorder(
    side: BorderSide(color: Colors.transparent, width: 0),
    borderRadius: BorderRadius.only(topLeft: Radius.circular(radius), topRight: Radius.circular(radius)),
  );
}

Decoration containerColorRadiusBorderWidth(Color background, double radius, Color color, double w) {
  return BoxDecoration(
    color: background,
    border: Border.all(
        width: w, //
        color: color //                  <--- border width here
        ),
    borderRadius: BorderRadius.circular(radius),
  );
}

ShapeBorder cardRadiusTop2(double radius) {
  return RoundedRectangleBorder(
    borderRadius: BorderRadius.only(topLeft: Radius.circular(radius), topRight: Radius.circular(radius)),
  );
}

ShapeBorder cardRadiusBottom(double radius) {
  return RoundedRectangleBorder(
    borderRadius: BorderRadius.only(bottomLeft: Radius.circular(radius), bottomRight: Radius.circular(radius)),
  );
}

Decoration containerColorRadiusBorder(Color background, double radius, Color color) {
  return BoxDecoration(
    color: background,
    border: Border.all(
        width: 1, //
        color: color //                  <--- border width here
        ),
    borderRadius: BorderRadius.circular(radius),
  );
}

//Decoration appGradient = BoxDecoration(
//  gradient: LinearGradient(
//    colors: [
//      Colors.green[200],
//      Colors.green,
//    ],
//    begin: Alignment.topCenter,
//    end: Alignment.bottomCenter,
//  ),
//);
// launchURL(String url) async {
//   if (await canLaunch(url)) {
//     await launch(url);
//   } else {
//     throw 'Could not launch $url';
//   }
// }
