import 'dart:convert';

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

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

class SamplingSoundSetting extends StatelessWidget {
  const SamplingSoundSetting({
    Key? key,
    required this.isloading,
    required this.listSamplingLocation,
    required this.activeSoundSampling,
  }) : super(key: key);

  final ValueNotifier<bool> isloading;
  final ValueNotifier<List<SamplingLocation>> listSamplingLocation;
  final ValueNotifier<List> activeSoundSampling;

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding:
          EdgeInsets.only(right: Constant.getActualX(context: context, x: 50)),
      child: SizedBox(
        height: listSamplingLocation.value.length != 1
            ? (Constant.getActualY(context: context, y: 150) /
                    Constant.getActualX(context: context, x: 15)) *
                (listSamplingLocation.value.length * 3.1)
            : 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: listSamplingLocation.value
                    .map(
                      (e) => Row(
                        children: [
                          FractionallySizedBox(
                            child: CupertinoSwitch(
                              value: e.value,
                              onChanged: (val) {
                                if (val) {
                                  var rwData =
                                      json.encode(activeSoundSampling.value);
                                  List data = json.decode(rwData);
                                  List actSnd = data;
                                  actSnd.add(e.mLocationID);
                                  activeSoundSampling.value = actSnd;
                                }
                                if (!val) {
                                  var rwData =
                                      json.encode(activeSoundSampling.value);
                                  List data = json.decode(rwData);
                                  List actSnd = data;
                                  actSnd.remove(e.mLocationID);
                                  activeSoundSampling.value = actSnd;
                                }

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