import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'dart:io';

import 'color_util.dart';

showHintDialog(
  BuildContext context,
  String? content,
  Function onConfirmTap, {
  String title = "提示",
  btnWidth = 70.0,
  btnHeight = 32.0,
  bool hideCancelBtn = false,
  Function? onCancelTap,
}) {
  if (Platform.isIOS) {
    showCupertinoDialog(
        context: context,
        builder: (_) => HintIOSDialog(
            title, content, hideCancelBtn, onCancelTap, onConfirmTap)
    );
  } else {
    showDialog(
        context: context,
        barrierDismissible: true,
        builder: (_) => HintDialog(
              title,
              content,
              btnWidth,
              btnHeight,
              hideCancelBtn,
              onCancelTap,
              onConfirmTap,
            ));
  }
}

/// 提示dialog new CircularProgressIndicator(),
class HintDialog extends StatefulWidget {
  final String title; //标题
  final String? content; //提示内容
  final double btnWidth; //按钮宽度
  final double btnHeight; //按钮高度
  final bool hideCancelBtn; //是否隐藏取消btn
  final Function? onCancelTap; //取消事件
  final Function onConfirmTap; //确认时件

  HintDialog(
    this.title,
    this.content,
    this.btnWidth,
    this.btnHeight,
    this.hideCancelBtn,
    this.onCancelTap,
    this.onConfirmTap,
  );

  @override
  State<StatefulWidget> createState() {
    return HintDialogState();
  }
}

class HintDialogState extends State<HintDialog> {
  @override
  Widget build(BuildContext context) {
    return Center(
      child: Container(
        height: 168.0,
        margin: EdgeInsets.only(left: 20.0, right: 20.0),
        padding: EdgeInsets.only(left: 15.0, top: 15.0),
        decoration: BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.circular(4.0),
        ),
        child: _contentStyle(),
      ),
    );
  }

  ///内容区域样式
  _contentStyle() {
    return Column(
      children: <Widget>[
        _title(),
        Expanded(
          child: _hintMsg(),
        ),
        Container(
          margin: EdgeInsets.only(bottom: 15.0, right: 15.0),
          child: _bottomBtn(),
        ),
      ],
    );
  }

  ///提示标题布局
  _title() {
    return Container(
      alignment: Alignment.centerLeft,
      child: Text(widget.title,
          style: TextStyle(
              fontSize: 16,
              color: ColorUtil.Color_85_117_242,
              decoration: TextDecoration.none)),
    );
  }

  ///提示信息布局
  _hintMsg() {
    return Container(
      alignment: Alignment.topLeft,
      margin: EdgeInsets.all(10.0),
      child: Text(widget.content!,
          style: TextStyle(
              fontSize: 14.0,
              color: ColorUtil.Color_85_117_242,
              decoration: TextDecoration.none)),
    );
  }

  ///取消 确认按钮布局
  _bottomBtn() {
    return Row(
      mainAxisSize: MainAxisSize.max,
      mainAxisAlignment: MainAxisAlignment.end,
      children: <Widget>[
        Container(
          child: Text('取消'),
        ),
        Container(
          child: Text('确认'),
        ),
      ],
    );
  }

  ///取消事件
  _onCancelTap() {
    Navigator.of(context).pop();
    widget.onCancelTap!();
  }

  ///确认事件
  _onConfirmTap() {
    Navigator.of(context).pop();
    widget.onConfirmTap();
  }
}

class HintIOSDialog extends StatefulWidget {
  final String title; //标题
  final String? content; //提示内容
  final bool hideCancelBtn; //是否隐藏取消btn
  final Function? onCancelTap; //取消事件
  final Function onConfirmTap; //确认时件
  HintIOSDialog(this.title, this.content, this.hideCancelBtn, this.onCancelTap,
      this.onConfirmTap);

  @override
  _HintIOSDialogState createState() => _HintIOSDialogState();
}

class _HintIOSDialogState extends State<HintIOSDialog> {
  @override
  Widget build(BuildContext context) {
    return CupertinoAlertDialog(
      title: Text(widget.title),
      content: Text(widget.content!),
      actions:!widget.hideCancelBtn ?
      <Widget>[
        _cancelIOSBtn(),
        CupertinoDialogAction(
          child: Text('确认'),
          onPressed: () {
            Navigator.of(context).pop();
            widget.onConfirmTap();
          },
        )
      ] : <Widget>[
        CupertinoDialogAction(
          child: Text('确认'),
          onPressed: () {
            Navigator.of(context).pop();
            widget.onConfirmTap();
          },
        )
      ],
    );
  }

  ///取消
  _cancelIOSBtn() {
    return CupertinoDialogAction(
            child: Text('取消'),
            onPressed: () {
              Navigator.of(context).pop();
              widget.onCancelTap!();
            });
  }
}
