import 'dart:convert';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

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

class CounterSoundSetting extends StatelessWidget {
  const CounterSoundSetting({
    Key? key,
    required this.isLoading,
    required this.listService,
    required this.activeSoundCounter,
  }) : super(key: key);

  final ValueNotifier<bool> isLoading;
  final ValueNotifier<List<Layanan>> listService;
  final ValueNotifier<List> activeSoundCounter;

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding:
          EdgeInsets.only(right: Constant.getActualX(context: context, x: 50)),
      child: SizedBox(
        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),
        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.sound,
                              onChanged: (val) {
                                if (val) {
                                  var rwData =
                                      json.encode(activeSoundCounter.value);
                                  List data = json.decode(rwData);
                                  List actlyn = data;
                                  actlyn.add(e.counterID);
                                  activeSoundCounter.value = actlyn;
                                }
                                if (!val) {
                                  var rwData =
                                      json.encode(activeSoundCounter.value);
                                  List data = json.decode(rwData);
                                  List actlyn = data;
                                  actlyn.remove(e.counterID);
                                  activeSoundCounter.value = actlyn;
                                }

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