import 'package:flutter/material.dart';

// ignore: must_be_immutable
class CheckListQuestion extends StatefulWidget {
  var questionOptions = [];
  List<bool> _isChecked = [];
  int fieldId;
  Function fillAnswer;
  String fieldValue;
  List<String> selectedOptions = [];
  static List<String> selectedOptionsTemp = [];
  CheckListQuestion(
      this.questionOptions, this.fieldId, this.fieldValue, this.fillAnswer) {
    if (this.fieldValue != '') {
      this.selectedOptions = this.fieldValue.split(',');
    }
    
    for (var item in questionOptions) {
      bool isSelected = selectedOptionsTemp
              .where((element) => element == item['lovOptionID'].toString())
              .toList()
              .length >
          0;

      this._isChecked.add(isSelected);
    }
  }

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

class _CheckListQuestionState extends State<CheckListQuestion> {
  List<String> selectedOptionsNames = [];
  String nameStr = '';

  addRemoveChoise(
      int lovOptionID, String lovName, bool value, int index) async {}

  @override
  Widget build(BuildContext context) {
    return Column(
      mainAxisSize: MainAxisSize.min,
      children: <Widget>[
        ...widget.questionOptions
            .asMap()
            .map(
              (index, element) => MapEntry(
                  index,
                  Stack(
                    children: [
                      CheckboxListTile(
                        onChanged: (bool? value) {
                          setState(() {
                            var lovOptionID =
                                widget.questionOptions[index]['lovOptionID'];
                            var lovName =
                                widget.questionOptions[index]['lovName'];
                            if (value == true) {
                              CheckListQuestion.selectedOptionsTemp
                                  .add(lovOptionID.toString());
                              this.selectedOptionsNames.add(lovName);
                            } else {
                              int index1 = CheckListQuestion.selectedOptionsTemp
                                  .indexWhere((element) =>
                                      element == lovOptionID.toString());
                              int index2 = this
                                  .selectedOptionsNames
                                  .indexWhere((element) => element == lovName);

                              if (index1 > -1) {
                                CheckListQuestion.selectedOptionsTemp =
                                    CheckListQuestion.selectedOptionsTemp
                                      ..removeAt(index1);
                              }
                              if (index2 > -1) {
                                this.selectedOptionsNames.removeAt(index2);
                              }
                            }

                            this.widget._isChecked[index] = value!;

                            if (CheckListQuestion.selectedOptionsTemp.length >
                                0) {
                              widget.fillAnswer(
                                  this.widget.fieldId,
                                  CheckListQuestion.selectedOptionsTemp
                                      .join(','));
                            } else {
                              widget.fillAnswer(this.widget.fieldId, '');
                            }

                            if (this.selectedOptionsNames.length > 0) {
                              this.nameStr =
                                  this.selectedOptionsNames.join(', ');
                            } else {
                              this.nameStr = '';
                            }
                          });
                        },
                        title: Container(
                            child:
                                Text(widget.questionOptions[index]['lovName'])),
                        value: this.widget._isChecked[index],
                      )
                    ],
                  )),
            )
            .values
            .toList(),
      ],
    );
  }
}
