import 'dart:io';
import 'dart:ui';

import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:location/resource/colors.gen.dart';
import 'package:location/utils/common_expand.dart';

import '../resource/assets.gen.dart';
import '../resource/string.gen.dart';
import '../utils/common_style.dart';

class CheckLocationPermissionDialog {
  static final String _tag = 'CheckLocationPermissionDialog';

  static void show({required VoidCallback onRequestPermissionClick}) {
    SmartDialog.show(
        tag: _tag,
        builder: (_) {
          return CheckLocationPermissionView(
              onRequestPermissionClick: onRequestPermissionClick);
        });
  }

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

class CheckLocationPermissionView extends Dialog {
  final VoidCallback onRequestPermissionClick;

  const CheckLocationPermissionView(
      {super.key, required this.onRequestPermissionClick});

  @override
  Widget build(BuildContext context) {
    //图片当做背景
    return Container(
      width: 290.w,
      height: 230.w,
      decoration: BoxDecoration(
        image: DecorationImage(
          image: Assets.images.bgCheckLocationPermission.provider(),
          fit: BoxFit.fill,
        ),
      ),
      child: Column(
        children: [
          SizedBox(height: 92.w),
          Text(StringName.dialogRecordLocation,
              style: TextStyle(
                  fontSize: 18.sp,
                  color: '#333333'.color,
                  fontWeight: FontWeight.bold)),
          SizedBox(height: 8.w),
          Text(StringName.dialogRecordLocationHasPermission,
              style: TextStyle(fontSize: 15.sp, color: '#404040'.color)),
          SizedBox(height: 20.6.w),
          if (Platform.isIOS) buildIosBtnView() else buildAndroidBtnView(),
        ],
      ),
    );
  }

  Widget buildIosBtnView() {
    return GestureDetector(
      onTap: () {
        onRequestPermissionClick.call();
        CheckLocationPermissionDialog.dismiss();
      },
      child: Container(
          width: 229.w,
          height: 40.w,
          decoration: BoxDecoration(
            color: ColorName.colorPrimary,
            borderRadius: BorderRadius.circular(54.w),
          ),
          child: Center(
              child: Text(StringName.nextStep,
                  style: TextStyle(fontSize: 14.sp, color: ColorName.white)))),
    );
  }

  Widget buildAndroidBtnView() {
    return Row(
      mainAxisAlignment: MainAxisAlignment.center,
      children: [
        GestureDetector(
          onTap: () {
            CheckLocationPermissionDialog.dismiss();
          },
          child: Container(
            decoration: BoxDecoration(
              border: Border.all(color: '#AAAAAA'.color, width: 1.w),
              borderRadius: BorderRadius.circular(54.w),
            ),
            width: 109.w,
            height: 40.w,
            child: Center(
              child: Text(
                StringName.dialogRecordLocationNotRequest,
                style: TextStyle(fontSize: 14.sp, color: '#AAAAAA'.color),
              ),
            ),
          ),
        ),
        SizedBox(width: 16.w),
        GestureDetector(
          onTap: () {
            onRequestPermissionClick.call();
            CheckLocationPermissionDialog.dismiss();
          },
          child: Container(
            decoration: getPrimaryBtnDecoration(54.w),
            width: 109.w,
            height: 40.w,
            child: Center(
              child: Text(StringName.dialogRecordLocationRequest,
                  style: TextStyle(fontSize: 14.sp, color: Colors.white)),
            ),
          ),
        )
      ],
    );
  }
}
