import 'dart:convert';

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

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

class DisplayDoctor extends StatelessWidget {
  const DisplayDoctor({
    Key? key,
    required this.isLoadingDoctor,
    required this.listServiceDoctor,
    required this.activeSoundDoctor,
  }) : super(key: key);

  final ValueNotifier<bool> isLoadingDoctor;
  final ValueNotifier<List<LayananDokter>> listServiceDoctor;
  final ValueNotifier<List> activeSoundDoctor;

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

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