import 'package:doctor_app_flutter/config/size_config.dart';
import 'package:doctor_app_flutter/core/viewModel/project_view_model.dart';
import 'package:doctor_app_flutter/core/model/SOAP/master_key_model.dart';
import 'package:doctor_app_flutter/core/model/SOAP/selected_items/my_selected_history.dart';
import 'package:doctor_app_flutter/screens/patients/profile/soap_update/shared_soap_widgets/remove_button.dart';
import 'package:doctor_app_flutter/utils/translations_delegate_base_utils.dart';
import 'package:doctor_app_flutter/widgets/shared/app_texts_widget.dart';
import 'package:flutter/material.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!,
                          textDecoration: myHistory.isChecked! ? TextDecoration.none : TextDecoration.lineThrough,
                          color: Color(0xFF2B353E),
                          fontSize: SizeConfig.getTextMultiplierBasedOnWidth() * 3.5,
                          fontWeight: FontWeight.w700,
                          letterSpacing: -0.48,
                        ),
                        width: MediaQuery.of(context).size.width * 0.5,
                      ),
                      if (myHistory.isChecked!)
                        RemoveButton(
                          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) {
      if (history.first.isLocal!) {
        setState(() {
          widget.myHistoryList!.remove(history.first);
        });
      } else {
        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),
            ),
          );
        });
  }
}
