import 'package:doctor_app_flutter/core/viewModel/project_view_model.dart';
import 'package:doctor_app_flutter/models/SOAP/master_key_model.dart';
import 'package:doctor_app_flutter/models/SOAP/my_selected_history.dart';
import 'package:doctor_app_flutter/util/translations_delegate_base.dart';
import 'package:doctor_app_flutter/widgets/shared/app_texts_widget.dart';
import 'package:flutter/material.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import 'package:hexcolor/hexcolor.dart';
import 'package:provider/provider.dart';

import '../../shared_soap_widgets/SOAP_open_items.dart';
import 'add_history_dialog.dart';

class UpdateHistoryWidget extends StatefulWidget {
  final List<MySelectedHistory> myHistoryList;

  const UpdateHistoryWidget({Key key, this.myHistoryList}) : super(key: key);

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

class _UpdateHistoryWidgetState extends State<UpdateHistoryWidget>
    with TickerProviderStateMixin {
  PageController _controller;

  changePageViewIndex(pageIndex) {
    _controller.jumpToPage(pageIndex);
  }

  @override
  void initState() {
    _controller = new PageController();

    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    ProjectViewModel projectViewModel = Provider.of(context);
    return Column(
      children: [

        SOAPOpenItems(label: "${TranslationBase.of(context).addHistory}",onTap: () {
          openHistoryList(context);

        },),
        SizedBox(
          height: 20,
        ),
        Container(
          margin:
          EdgeInsets.only(left: 15, right: 15, top: 15),
          child: Column(
            children: widget.myHistoryList.map((myHistory) {
              return Column(
                children: [
                  Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      Container(
                        child: AppText(
                            projectViewModel.isArabic
                                ? myHistory.selectedHistory.nameAr
                                : myHistory.selectedHistory.nameEn,
                            fontSize: 15,
                            textDecoration: myHistory.isChecked
                                ? null
                                : TextDecoration.lineThrough,
                            color: Colors.black),
                        width: MediaQuery
                            .of(context)
                            .size
                            .width * 0.5,
                      ),
                      if (myHistory.isChecked)
                        InkWell(
                          child: Row(
                            children: [
                              Container(
                                child: AppText(
                                  TranslationBase
                                      .of(context)
                                      .remove,
                                  fontSize: 15,
                                  variant: "bodyText",
                                  textDecoration: myHistory.isChecked
                                      ? null
                                      : TextDecoration.lineThrough,
                                  color: HexColor("#B8382C"),),
                              ),
                              Icon(
                                FontAwesomeIcons.times,
                                color: HexColor("#B8382C"),
                                size: 17,
                              ),
                            ],
                          ),
                          onTap: () => removeHistory(myHistory.selectedHistory),
                        )
                    ],
                  ),
                  SizedBox(
                    height: 20,
                  ),
                ],
              );
            }).toList(),
          ),
        )
      ],
    );
  }

  removeHistory(MasterKeyModel historyKey) {
    List<MySelectedHistory> history =
    // ignore: missing_return
    widget.myHistoryList.where((element) =>
    historyKey.id ==
        element.selectedHistory.id &&
        historyKey.typeId ==
            element.selectedHistory.typeId
    ).toList();


    if (history.length > 0)
      setState(() {
        history[0].isChecked = false;
      });
  }

  openHistoryList(BuildContext context) {
    showModalBottomSheet(
        backgroundColor: Colors.white,
        isDismissible: false,
        isScrollControlled: true,
        context: context,
        builder: (context) {
          return FractionallySizedBox(
            heightFactor: 1,
            child: AddHistoryDialog(
              changePageViewIndex: changePageViewIndex,
              controller: _controller,
              myHistoryList: widget.myHistoryList,
              addSelectedHistories: () {
                setState(() {
                  Navigator.of(context).pop();
                });
              },
              removeHistory: (masterKey) => removeHistory(masterKey),
            ),
          );
        });
  }
}



