import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:get/get.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 '../utils/common_style.dart';
import 'common_confirm_dialog.dart';

class LocationPermissionDialog {
  static const String _tag = "LocationPermissionDialog";

  static void show({required VoidCallback onNextStep}) {
    SmartDialog.show(
        tag: _tag,
        builder: (_) {
          if (Platform.isAndroid) {
            return AndroidLocationView(onNextStep: onNextStep);
          } else {
            return CupertinoLocationView(onNextStep: onNextStep);
          }
        });
  }

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

class AndroidLocationView extends Dialog {
  final VoidCallback onNextStep;

  const AndroidLocationView({super.key, required this.onNextStep});

  @override
  Widget build(BuildContext context) {
    return CommonConfirmView(
        titleWidget: Text(StringName.kindlyReminder,
            style: TextStyle(
                fontSize: 17.sp,
                color: '#333333'.color,
                fontWeight: FontWeight.bold)),
        descWidget: Text(StringName.dialogLocationPermissionTips,
            style: TextStyle(fontSize: 15.sp, color: '#404040'.color)),
        confirmText: StringName.dialogLocationPermissionConfirmTxt,
        cancelOnTap: () {
          LocationPermissionDialog.dismiss();
        },
        confirmOnTap: () {
          LocationPermissionDialog.dismiss();
          onNextStep.call();
        });
  }
}

class CupertinoLocationView extends Dialog {
  final VoidCallback onNextStep;

  const CupertinoLocationView({super.key, required this.onNextStep});

  @override
  Widget build(BuildContext context) {
    return IntrinsicHeight(
      child: Container(
        width: 300.w,
        decoration: BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.circular(10.w),
        ),
        child: Column(
          children: [
            AspectRatio(
              aspectRatio: 900 / 438,
              child: Assets.images.bgDialogLocationPermissionIos
                  .image(width: double.infinity),
            ),
            SizedBox(height: 34.w),
            SizedBox(
              width: 210.w,
              child: RichText(
                textAlign: TextAlign.center,
                text: TextSpan(
                    style: TextStyle(fontSize: 14.sp, color: '#404040'.color),
                    children: [
                      TextSpan(text: '为保位置展示在地图上，需要允许使用'),
                      TextSpan(
                          text: '位置权限',
                          style: TextStyle(
                              color: '#1448CC'.color,
                              fontWeight: FontWeight.bold)),
                    ]),
              ),
            ),
            SizedBox(height: 19.w),
            GestureDetector(
              onTap: () {
                LocationPermissionDialog.dismiss();
                onNextStep.call();
              },
              child: Container(
                width: 252.w,
                height: 36.w,
                decoration: getPrimaryBtnDecoration(46.w),
                child: Center(
                  child: Text(StringName.nextStep,
                      style: TextStyle(
                          fontSize: 16.sp,
                          color: Colors.white,
                          fontWeight: FontWeight.bold)),
                ),
              ),
            ),
            SizedBox(height: 23.w),
          ],
        ),
      ),
    );
  }
}

class LocationAlwaysPermissionDialog {
  static const String _tag = "LocationAlwaysPermissionDialog";

  static void show({required VoidCallback onNextStep}) {
    SmartDialog.show(
        tag: _tag,
        alignment:
            Platform.isAndroid ? Alignment.center : Alignment.bottomCenter,
        builder: (_) {
          if (Platform.isAndroid) {
            return AndroidLocationAlwaysView(onNextStep: onNextStep);
          } else {
            return CupertinoLocationAlwaysView(onNextStep: onNextStep);
          }
        });
  }

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

class AndroidLocationAlwaysView extends Dialog {
  final VoidCallback onNextStep;

  const AndroidLocationAlwaysView({super.key, required this.onNextStep});

  @override
  Widget build(BuildContext context) {
    return CommonConfirmView(
        titleWidget: Text(StringName.dialogLocationAlwaysPermissionTitle,
            style: TextStyle(
                fontSize: 17.sp,
                color: '#333333'.color,
                fontWeight: FontWeight.bold)),
        descWidget: RichText(
            text: TextSpan(
                style: TextStyle(fontSize: 15.sp, color: '#404040'.color),
                children: [
              TextSpan(text: '如需要定位实时更新您的位置，需要在后台访问您的位置，避免轨迹缺失，请在定位权限中选择'),
              TextSpan(
                  text: '“始终允许”',
                  style: TextStyle(color: ColorName.colorPrimary)),
            ])),
        confirmText: StringName.dialogLocationAlwaysPermissionConfirmTxt,
        cancelOnTap: () {
          LocationAlwaysPermissionDialog.dismiss();
        },
        confirmOnTap: () {
          LocationAlwaysPermissionDialog.dismiss();
          onNextStep.call();
        });
  }
}

class CupertinoLocationAlwaysView extends Dialog {
  final VoidCallback onNextStep;

  final RxInt _currentPage = 0.obs;

  int get currentPage => _currentPage.value;

  final PageController pageController = PageController(initialPage: 0);

  final List<Widget> pageList = [
    Assets.images.imgDialogLocationAlwaysTip1.image(width: double.infinity),
    Assets.images.imgDialogLocationAlwaysTip2.image(width: double.infinity),
    Assets.images.imgDialogLocationAlwaysTip3.image(width: double.infinity),
  ];

  CupertinoLocationAlwaysView({super.key, required this.onNextStep});

  @override
  Widget build(BuildContext context) {
    return IntrinsicHeight(
      child: Container(
        width: double.infinity,
        decoration: BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.only(
            topLeft: Radius.circular(16.w),
            topRight: Radius.circular(16.w),
          ),
        ),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            SizedBox(height: 28.w),
            Container(
              margin: EdgeInsets.symmetric(horizontal: 16.w),
              child: RichText(
                text: TextSpan(
                    style: TextStyle(fontSize: 20.sp, color: '#333333'.color),
                    children: [
                      TextSpan(
                          text: '应用使用需开启',
                          style: TextStyle(fontWeight: FontWeight.bold)),
                      TextSpan(
                          text: '精准位置',
                          style: TextStyle(
                              color: '#1448CC'.color,
                              fontWeight: FontWeight.bold)),
                      TextSpan(
                          text: '权限',
                          style: TextStyle(fontWeight: FontWeight.bold))
                    ]),
              ),
            ),
            Text(StringName.locationBackgroundAlwaysDesc,
                style: TextStyle(fontSize: 14.sp, color: '#666666'.color)),
            SizedBox(height: 24.w),
            SizedBox(
              width: 304.w,
              height: 230.w,
              child: PageView(
                onPageChanged: (index) {
                  _currentPage.value = index;
                },
                controller: pageController,
                children: pageList,
              ),
            ),
            SizedBox(height: 8.w),
            indicator(),
            SizedBox(height: 30.w),
            GestureDetector(
              onTap: () => onNextClick(),
              child: Container(
                decoration: getPrimaryBtnDecoration(46.w),
                width: 312.w,
                height: 44.w,
                child: Center(
                  child: Text(StringName.nextStep,
                      style: TextStyle(fontSize: 15.sp, color: Colors.white)),
                ),
              ),
            ),
            SizedBox(height: 28.w),
          ],
        ),
      ),
    );
  }

  Widget indicator() {
    return Obx(() {
      return Row(
        mainAxisAlignment: MainAxisAlignment.center,
        children: List.generate(pageList.length, (index) {
          return Container(
            margin: EdgeInsets.symmetric(horizontal: 3.w),
            width: 6.w,
            height: 6.w,
            decoration: BoxDecoration(
              color: currentPage == index ? '#A7A7A7'.color : '#E5E5E5'.color,
              shape: BoxShape.circle,
            ),
          );
        }),
      );
    });
  }

  void onNextClick() async {
    double? index = pageController.page;
    if (index == pageList.length - 1) {
      LocationAlwaysPermissionDialog.dismiss();
      onNextStep.call();
      return;
    }
    await pageController.nextPage(
        duration: const Duration(milliseconds: 300), curve: Curves.easeInOut);
  }
}
