import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:location/helper/internet_connection_helper.dart';
import 'package:location/resource/assets.gen.dart';
import 'package:location/resource/colors.gen.dart';
import 'package:location/resource/string.gen.dart';
import 'package:location/utils/common_expand.dart';
import 'package:location/utils/toast_util.dart';

class NetErrorDialog {
  static const String _tag = 'NetErrorDialog';

  static void show() {
    if (SmartDialog.checkExist(tag: _tag)) {
      return;
    }
    SmartDialog.show(builder: (_) => _NetErrorView(), tag: _tag);
  }

  static void dismiss() {
    SmartDialog.dismiss(tag: _tag);
  }
}

class _NetErrorView extends StatelessWidget {
  const _NetErrorView();

  @override
  Widget build(BuildContext context) {
    return Container(
      width: 274.w,
      decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(16.r),
          border: Border.all(
            color: Colors.white,
            width: 2.w,
          ),
          gradient: LinearGradient(
              begin: Alignment.topCenter,
              end: Alignment.bottomCenter,
              colors: [
                '#E4E4FF'.color,
                '#FFFFFF'.color,
              ])),
      child: Stack(
        alignment: Alignment.center,
        children: [
          Positioned(
              top: 16.w,
              right: 16.w,
              child: GestureDetector(
                onTap: onNetErrorCloseClick,
                child: Assets.images.iconDialogClose2
                    .image(width: 20.w, height: 20.w),
              )),
          buildNetErrorContent(),
        ],
      ),
    );
  }

  Widget buildNetErrorContent() {
    return IntrinsicHeight(
      child: Column(
        children: [
          SizedBox(height: 30.w),
          Assets.images.iconDialogNetError.image(height: 98.w),
          SizedBox(height: 10.w),
          Text(
            StringName.dialogNetErrorTitle,
            style: TextStyle(
                fontSize: 17.sp,
                color: '#333333'.color,
                fontWeight: FontWeight.bold),
          ),
          SizedBox(height: 8.w),
          Text(StringName.dialogNetErrorDesc,
              style: TextStyle(fontSize: 15.sp, color: '#999999'.color)),
          SizedBox(height: 22.w),
          GestureDetector(
            onTap: onNetErrorAgainClick,
            child: Container(
              width: 229.w,
              height: 43.w,
              decoration: BoxDecoration(
                color: ColorName.colorPrimary,
                borderRadius: BorderRadius.circular(100.r),
              ),
              child: Center(
                child: Text(StringName.dialogNetErrorAgain,
                    style: TextStyle(
                        fontSize: 14.sp,
                        color: Colors.white,
                        fontWeight: FontWeight.w500)),
              ),
            ),
          ),
          SizedBox(height: 20.w),
        ],
      ),
    );
  }

  void onNetErrorAgainClick() async {
    if (await InternetConnectionHelper.getInstance().isConnected()) {
      NetErrorDialog.dismiss();
    } else {
      ToastUtil.show(StringName.networkError);
    }
  }

  void onNetErrorCloseClick() {
    NetErrorDialog.dismiss();
  }
}
