import 'dart:convert';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:queuedisplay/model/service_model.dart';

import '../app/constant.dart';

class DisplayCounter extends StatelessWidget {
  const DisplayCounter(
      {Key? key,
      required this.isloading,
      required this.listService,
      required this.activeLayanan})
      : super(key: key);

  final ValueNotifier<bool> isloading;
  final ValueNotifier<List<Layanan>> listService;
  // final ValueNotifier<List> activeLayanan;
  final ValueNotifier<List> activeLayanan;

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding:
          EdgeInsets.only(right: Constant.getActualX(context: context, x: 50)),
      child: Container(
        //  width: Constant.getActualX(
        //               context: context, x: 500),
        height: listService.value.length != 1
            ? (Constant.getActualY(context: context, y: 150) /
                    Constant.getActualX(context: context, x: 15)) *
                (listService.value.length * 3.5)
            : Constant.getActualY(context: context, y: 70),
        // color: Colors.red,
        child: (isloading.value)
            ? Column(
                children: [
                  Expanded(
                    child: Center(
                      child: SizedBox(
                        width: Constant.getActualX(context: context, x: 90),
                        height: Constant.getActualY(context: context, y: 90),
                        child: const CircularProgressIndicator(),
                      ),
                    ),
                  ),
                ],
              )
            : GridView.count(
                childAspectRatio:
                    Constant.getActualY(context: context, y: 150) /
                        Constant.getActualX(context: context, x: 15),
                crossAxisCount: 2,
                children: listService.value
                    .map(
                      (e) => Row(
                        children: [
                          FractionallySizedBox(
                            child: CupertinoSwitch(
                              value: e.value,
                              onChanged: (val) {
                                if (val) {
                                  var rwData = json.encode(activeLayanan.value);
                                  List data = json.decode(rwData);
                                  List actlyn = data;
                                  actlyn.add(e.counterID);
                                  activeLayanan.value = actlyn;
                                }
                                if (!val) {
                                  var rwData = json.encode(activeLayanan.value);
                                  List data = json.decode(rwData);
                                  List actlyn = data;
                                  actlyn.remove(e.counterID);
                                  activeLayanan.value = actlyn;
                                }

                                e.value = val;
                              },
                            ),
                          ),
                          Expanded(
                            child: Text(
                              "${e.counterCode} - ${e.locationName}",
                              maxLines: 2,
                              overflow: TextOverflow.ellipsis,
                              style: Constant.normal(context: context),
                            ),
                          )
                        ],
                      ),
                    )
                    .toList(),
              ),
      ),
    );
  }
}
