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

import 'package:electronic_assistant/data/bean/version_update_bean.dart';
import 'package:electronic_assistant/data/consts/constants.dart';
import 'package:electronic_assistant/resource/assets.gen.dart';
import 'package:electronic_assistant/resource/colors.gen.dart';
import 'package:electronic_assistant/utils/common_style.dart';
import 'package:electronic_assistant/utils/expand.dart';
import 'package:electronic_assistant/utils/mmkv_util.dart';
import 'package:electronic_assistant/utils/toast_util.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';

import '../utils/launcher_url_util.dart';

class UpdateVersionDialog {
  static const String tag = 'showUpdateVersionDialog';

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

  static Widget _buildVersionView(String version) {
    return Container(
        decoration: BoxDecoration(
            gradient: LinearGradient(
              colors: [
                '#D0C5FF'.toColor(),
                ColorName.white,
                '#647FFF'.toColor()
              ],
              begin: Alignment.topLeft,
              end: Alignment.bottomRight,
            ),
            borderRadius: BorderRadius.all(Radius.circular(11.w))),
        padding: EdgeInsets.all(0.5.w),
        height: 22.h,
        child: Container(
          decoration: getPrimaryBtnDecoration(11.w),
          padding: EdgeInsets.symmetric(horizontal: 10.w, vertical: 2.w),
          child: Text(
            version,
            style: TextStyle(
                fontSize: 13.sp,
                color: ColorName.white,
                fontWeight: FontWeight.bold,
                height: 0),
          ),
        ));
  }

  static Widget _buildUpdateBtn({VoidCallback? onUpdateClick}) {
    return GestureDetector(
      onTap: onUpdateClick,
      child: Container(
          width: 148.w,
          height: 48.w,
          decoration: BoxDecoration(
            gradient: LinearGradient(
              colors: ['#25262A'.toColor(), '#3F424D'.toColor()],
              stops: const [0.3, 1.0],
              begin: Alignment.topLeft,
              end: Alignment.bottomRight,
            ),
            borderRadius: BorderRadius.circular(100.w),
          ),
          child: Center(
            child: Text(
              '立即更新',
              style: TextStyle(
                  fontSize: 14.sp,
                  color: ColorName.white,
                  fontWeight: FontWeight.bold),
            ),
          )),
    );
  }

  static void show(VersionUpdateBean bean) {
    if (SmartDialog.checkExist(tag: tag)) {
      return;
    }
    SmartDialog.show(
        tag: tag,
        backType: SmartBackType.block,
        clickMaskDismiss: false,
        maskColor: ColorName.black55,
        builder: (_) {
          return SizedBox(
            width: 304.w,
            child: IntrinsicHeight(
              child: Column(
                children: [
                  Container(
                    width: 304.w,
                    decoration: BoxDecoration(
                      image: DecorationImage(
                        image: Assets.images.bgUpdateVersion.provider(),
                        fit: BoxFit.cover,
                      ),
                    ),
                    child: AspectRatio(
                        aspectRatio: 960 / 1110,
                        child: Container(
                          padding: EdgeInsets.symmetric(horizontal: 42.w),
                          child: Column(
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: [
                              SizedBox(height: 57.h),
                              Assets.images.iconUpdateNewVersionTitle
                                  .image(height: 29.h),
                              SizedBox(height: 8.h),
                              _buildVersionView(bean.version),
                              SizedBox(height: 20.h),
                              Expanded(
                                  child:
                                      _buildUpdateView(bean.title, bean.desc)),
                              SizedBox(height: 20.h),
                              _buildUpdateBtn(onUpdateClick: () {
                                onUpdate(bean.url);
                              }),
                              SizedBox(height: 48.h),
                            ],
                          ),
                        )),
                  ),
                  SizedBox(height: 12.h),
                  Visibility(
                    visible: !bean.force,
                    child: GestureDetector(
                        onTap: () {
                          KVUtil.putString(
                              Constants.recordServerVersion, bean.version);
                          SmartDialog.dismiss(tag: tag);
                        },
                        child: Assets.images.iconUpdateAppClose
                            .image(width: 40.w, height: 40.w)),
                  )
                ],
              ),
            ),
          );
        });
  }

  static Widget _buildUpdateView(String? title, String? desc) {
    return SingleChildScrollView(
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Text(
            title ?? '',
            style: TextStyle(fontSize: 14.sp, color: '#202A4D'.toColor()),
          ),
          Visibility(
              visible: title != null && title.isNotEmpty,
              child: SizedBox(
                height: 6.h,
              )),
          Text(
            desc ?? '',
            style: TextStyle(fontSize: 14.sp, color: '#202A4D'.toColor()),
          ),
        ],
      ),
    );
  }

  static void onUpdate(String? url) {
    if (Platform.isAndroid) {
      if (url == null) {
        return;
      }
      try {
        LauncherUrlUtil.launchHttpUrl(url);
      } catch (e) {
        ToastUtil.showToast('更新失败');
      }
    } else if (Platform.isIOS) {
      // ios 跳转苹果商店
      LauncherUrlUtil.launchHttpUrl(
          'itms-apps://itunes.apple.com/us/app/apple-store/6683306440');
    }
  }
}
