import 'package:flutter/material.dart';
import 'commonFun.dart';
import '../res/listContacts.dart';
import 'package:flutter/services.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'doJSON.dart';
import 'hyxx_data_handle.dart';
import 'dart:convert';
import '../pages/MyMsics/03_personal/ContactModify.dart';
import '../pages/MyMsics/03_personal/ContactAdd.dart';

//删除消息对话框。自定义透明背景窗口，类似对话框
class customDialogG extends StatefulWidget {
  customDialogG({Key key, this.title = "", this.index = -1}) : super(key: key);
  String title;
  int index;

  _CheckBoxDemoState createState() => _CheckBoxDemoState();
}

class _CheckBoxDemoState extends State<customDialogG> {
  String getTitle(int index) {
    // return listContacts2[index]["姓名"] +
    //     '， ' +
    //     listContacts2[index]["部门"] +
    //     '， ' +
    //     listContacts2[index]["职务"] +
    //     '\n' +
    //     listContacts2[index]["手机"] +
    //     '， ' +
    //     listContacts2[index]["邮箱"];
  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    bFlash = false;
    getContent();
  }

  String strContent = '';

  getContent() async {
    if (0 == widget.title.compareTo('收到的消息')) {
      strContent = "第 ${widget.index + 1} 条（共 ${listMessagesInbox2.length} 条）" +
          "\n" +
          "时间：" +
          listMessagesInbox2[widget.index]['date'] +
          ", " +
          listMessagesInbox2[widget.index]['time'] +
          "\n\n" +
          "内容：" +
          listMessagesInbox2[widget.index]['content'];
    } else if (0 == widget.title.compareTo('发送的消息')) {
      strContent = "第 ${widget.index + 1} 条（共 ${listMessagesOutbox2.length} 条）" +
          "\n" +
          "时间：" +
          listMessagesOutbox2[widget.index]['date'] +
          ", " +
          listMessagesOutbox2[widget.index]['time'] +
          "\n\n" +
          "内容：" +
          listMessagesOutbox2[widget.index]['content'];
    }
    setState(() {});
  }

  bool flagSelect = false;
  bool flagInbox = false;
  bool flagOutbox = false;

  Widget getCheckBoxs() {
    return Column(
      mainAxisAlignment: MainAxisAlignment.center,
      children: <Widget>[
        Row(children: <Widget>[
          SizedBox(width: 10),
          Checkbox(
            value: flagInbox || flagOutbox ? false : flagSelect,
            onChanged: flagInbox || flagOutbox
                ? null
                : (v) {
                    setState(() {
                      flagSelect = v;
                    });
                  },
          ),
          Text('删除当前选择的消息'),
        ]),
        Row(children: <Widget>[
          SizedBox(width: 10),
          Checkbox(
            value: flagInbox,
            onChanged: (v) {
              setState(() {
                flagInbox = v;
              });
            },
          ),
          Text('删除所有收到的消息'),
        ]),
        Row(children: <Widget>[
          SizedBox(width: 10),
          Checkbox(
            value: flagOutbox,
            onChanged: (v) {
              setState(() {
                flagOutbox = v;
              });
            },
          ),
          Text('删除所有发送的消息'),
        ]),
      ],
    );
  }

  Widget getBtnSizeX({@required text, width = 60.0, height = 30.0, onPressedFun}) {
    return Container(
      color: Colors.white12, //onPressedFun为null时无效
      width: width,
      height: height,
      child: RaisedButton(
        padding: EdgeInsets.all(0),
        textColor: Colors.black,
        child: Text(text),
        onPressed: onPressedFun,
      ),
    );
  }

  Future<bool> alertDialog(String title, String content, var onPresse) async {
    return await showDialog(
      barrierDismissible: false, //表示点击灰色背景的时候是否消失弹出框
      context: context,
      builder: (context) {
        return AlertDialog(
          title: Text(title),
          content: Text(content),
          actions: <Widget>[
            FlatButton(
              child: Text("确定"),
              onPressed: onPresse,
            ),
            FlatButton(
              child: Text("取消"),
              onPressed: () {
                Navigator.pop(context, false);
              },
            ),
          ],
        );
      },
    );
  }

  @override
  Widget build(BuildContext context) {
    SystemUiOverlayStyle(statusBarColor: Colors.transparent);
    Size mediaSize = MediaQuery.of(context).size;
    myController.text = getTitle(widget.index);
    // TODO: implement build
    return WillPopScope(
      child: Scaffold(
        //type: MaterialType.transparency,
        //color: Colors.transparent,
        //backgroundColor: Colors.transparent,
        //color: Colors.blue,
        backgroundColor: Color.fromRGBO(212, 212, 212, 0.6),
        body: SafeArea(
          child: Stack(
            children: <Widget>[
              Container(
                alignment: Alignment(0, -0.4),
                child: Container(
                  height: mediaSize.height * 0.7,
                  width: mediaSize.width * 0.95,
                  decoration: BoxDecoration(
                    color: Colors.white,
                    //border: Border.all(width: 1.0, color: Colors.black),
                    borderRadius: BorderRadius.all(
                      Radius.circular(3),
                    ),
                  ),
                  child: Column(
                    children: <Widget>[
                      Padding(
                        padding: EdgeInsets.fromLTRB(10, 10, 10, 0),
                        child: Stack(
                          children: <Widget>[
                            Align(
                              alignment: Alignment.center,
                              child: Text(
                                "${widget.title}",
                                style: TextStyle(
                                  fontSize: 15.0,
                                ),
                              ),
                            ),
                            Align(
                              alignment: Alignment.centerRight,
                              child: InkWell(
                                child: Icon(Icons.close),
                                onTap: () {
                                  //Navigator.pop(context, index);
                                  Navigator.pop(context); //关闭弹框，播放输入视频地址
                                },
                              ),
                            )
                          ],
                        ),
                      ),
                      Divider(),
                      // Container(
                      //   padding: EdgeInsets.fromLTRB(20, 10, 20, 10),
                      //   width: double.infinity,
                      //   child: SingleChildScrollView(
                      //     child: Container(
                      //       child: Text(strContent),
                      //     ),
                      //   ),
                      // ),
                      Container(
                        padding: EdgeInsets.fromLTRB(20, 10, 20, 10),
                        width: double.infinity,
                        child: Container(
                          decoration: BoxDecoration(
                            color: Colors.white,
                            border: Border.all(width: 1.0, color: Colors.blue),
                            borderRadius: BorderRadius.all(
                              Radius.circular(3),
                            ),
                          ),
                          padding: EdgeInsets.fromLTRB(20, 10, 20, 10),
                          width: double.infinity,
                          height: mediaSize.height * 0.2,
                          child: SingleChildScrollView(
                            child: Container(
                              child: Text(strContent),
                            ),
                          ),
                        ),
                      ),
                      SizedBox(height: 10),
                      getCheckBoxs(),
                      SizedBox(height: 10),
                      Expanded(
                        child: Container(),
                      ),
                      Row(
                        mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                        children: <Widget>[
                          getBtnSizeX(
                            text: "删除",
                            onPressedFun: () async {
                              // listContacts2.removeAt(widget.index);
                              // bFlash = true;
                              // print("ContactDel bFlash = $bFlash");
                              // writeJSON(json.encode(listContacts2), 'listContacts2.json');
                              bool ret = await alertDialog('删除确认', '是否确定要删除选中的项目？', () {
                                print("flagInbox = $flagInbox");
                                print("flagOutbox = $flagOutbox");
                                print("flagSelect = $flagSelect");

                                if (flagInbox || flagOutbox) {
                                  bFlash = true;
                                  if (flagInbox) {
                                    listMessagesInbox2.clear();
                                    writeJSON(json.encode(listMessagesInbox2),
                                        'listMessagesInbox02.json');
                                  }
                                  if (flagOutbox) {
                                    listMessagesOutbox2.clear();
                                    writeJSON(json.encode(listMessagesOutbox2),
                                        'listMessagesOutbox02.json');
                                  }
                                } else if (flagSelect) {
                                  bFlash = true;
                                  if (0 == widget.title.compareTo('收到的消息')) {
                                    listMessagesInbox2.removeAt(widget.index);
                                    writeJSON(json.encode(listMessagesInbox2),
                                        'listMessagesInbox02.json');
                                  } else if (0 == widget.title.compareTo('发送的消息')) {
                                    listMessagesOutbox2.removeAt(widget.index);
                                    writeJSON(json.encode(listMessagesOutbox2),
                                        'listMessagesOutbox02.json');
                                  }
                                }

                                print("bFlash = $bFlash");
                                Navigator.pop(context, true);
                              });
                              if (ret) {
                                Navigator.pop(context); //关闭弹框，播放输入视频地址
                              }
                            },
                          ),
                          getBtnSizeX(
                            text: "取消",
                            onPressedFun: () {
                              Navigator.pop(context); //关闭弹框，播放输入视频地址
                            },
                          ),
                        ],
                      ),
                      SizedBox(height: 50),
                    ],
                  ),
                ),
              ),
            ],
          ),
        ),
      ),
      onWillPop: () {
        // 屏蔽点击返回键的操作
        //player.pause();
        Navigator.pop(context);
      },
    );
  }
}
