import 'dart:convert';

import 'package:diplomaticquarterapp/models/Appointments/appoDetailsButtons.dart';
import 'package:diplomaticquarterapp/pages/MyAppointments/models/AppointmentModel.dart';
import 'package:diplomaticquarterapp/pages/MyAppointments/models/AppointmentType.dart';
import 'package:diplomaticquarterapp/pages/MyAppointments/models/BookedButtons.dart';
import 'package:diplomaticquarterapp/uitl/translations_delegate_base.dart';
import 'package:flutter/material.dart';

class AppointmentActions extends StatefulWidget {
  @override
  _AppointmentActionsState createState() => _AppointmentActionsState();
}

class _AppointmentActionsState extends State<AppointmentActions> {
  List<AppoDetailsButton> appoButtonsList = [];
  var appointment = new AppointmentModel();

  @override
  void initState() {
    appointment.PatientStatusType = 0;
    appointment.IsOnlineCheckedIN = true;

    _getAppointmentActionButtons();

    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    var size = MediaQuery.of(context).size;
    final double itemHeight = ((size.height - kToolbarHeight - 24) * 0.42) / 2;
    final double itemWidth = size.width / 2;

    return Container(
      margin: EdgeInsets.all(5.0),
      child: CustomScrollView(
        primary: false,
        physics: NeverScrollableScrollPhysics(),
        slivers: <Widget>[
          SliverPadding(
            padding: const EdgeInsets.fromLTRB(15, 0, 15, 0),
            sliver: SliverGrid.count(
              crossAxisCount: 2,
              childAspectRatio: (itemWidth / itemHeight),
              children: appoButtonsList
                  .map((e) => GestureDetector(
                        onTap: () {
                          _handleButtonClicks(e);
                        },
                        child: Container(
                          height: 100.0,
                          margin: EdgeInsets.all(9.0),
                          decoration: BoxDecoration(
                              boxShadow: [
                                BoxShadow(
                                    color: Colors.grey[400],
                                    blurRadius: 2.0,
                                    spreadRadius: 0.0)
                              ],
                              borderRadius: BorderRadius.circular(10),
                              color: Colors.white),
                          child: Column(
                            crossAxisAlignment: CrossAxisAlignment.start,
                            mainAxisSize: MainAxisSize.min,
                            children: <Widget>[
                              Container(
                                margin:
                                    EdgeInsets.fromLTRB(10.0, 10.0, 10.0, 0.0),
                                child: Text(e.title,
                                    overflow: TextOverflow.clip,
                                    style: TextStyle(
                                        color: new Color(0xFFc5272d),
                                        letterSpacing: 1.0,
                                        fontSize: 20.0)),
                              ),
                              Container(
                                margin:
                                    EdgeInsets.fromLTRB(10.0, 0.0, 10.0, 0.0),
                                child: Text(e.subtitle,
                                    overflow: TextOverflow.clip,
                                    style: TextStyle(
                                        color: Colors.black,
                                        letterSpacing: 1.0,
                                        fontSize: 15.0)),
                              ),
                              Container(
                                alignment: Alignment.bottomRight,
                                margin:
                                    EdgeInsets.fromLTRB(0.0, 20.0, 10.0, 8.0),
                                child: Image.asset(e.icon,
                                    width: 40.0, height: 40.0),
                              ),
                            ],
                          ),
                        ),
                      ))
                  .toList(),
            ),
          ),
        ],
      ),
    );
  }

  _handleButtonClicks(AppoDetailsButton) {
    print(AppoDetailsButton.caller);
  }

  _getAppointmentActionButtons() {
    if (appointment != null) {
      if (isConfirmed()) {
        if (appointment.IsOnlineCheckedIN) {
          _getConfirmedCheckInAppoActionsList();
        } else {
          _getConfirmedAppoActionsList();
        }
      } else {
        print("isConfirmed Null");
      }
      if (isBooked()) {
        if (appointment.IsOnlineCheckedIN) {
          _getBookedCheckInAppoActionsList();
        } else {
          _getBookedAppoActionsList();
        }
      } else {
        print("isBooked Null");
      }
      if (isArrived()) {
        if (appointment.ClinicID == 17) {
          _getArrivedInvoiceAppoActionsList();
        } else {
          _getArrivedAppoActionsList();
        }
      } else {
        print("isArrived Null");
      }
    } else {
      print("Appo Null");
    }
  }

  bool isConfirmed() {
    return AppointmentType.isConfirmed(this.appointment);
  }

  bool isArrived() {
    return AppointmentType.isArrived(this.appointment);
  }

  bool isCheckedIn() {
    return this.appointment.IsOnlineCheckedIN;
  }

  bool isBooked() {
    return AppointmentType.isBooked(this.appointment);
  }

  Future<List<AppoDetailsButton>> _getBookedAppoActionsList() async {
    print("_getBookedAppoActionsList");
    var bookButtonsJson;
    List<AppoDetailsButton> buttonsList = [];
    String data = await DefaultAssetBundle.of(context)
        .loadString("assets/json/bookedButtons.json");
    bookButtonsJson = json.decode(data);
    for (var i = 0; i < bookButtonsJson.length; i++) {
      buttonsList.add(AppoDetailsButton(
        title: bookButtonsJson[i]['title'],
        subtitle: bookButtonsJson[i]['subtitle'],
        icon: bookButtonsJson[i]['icon'],
        caller: bookButtonsJson[i]['caller'],
      ));
    }
    setState(() {
      appoButtonsList = buttonsList;
    });
  }

  Future<List<AppoDetailsButton>> _getBookedCheckInAppoActionsList() async {
//    BookedButtons.getContext(context);
    print("_getBookedCheckInAppoActionsList");
    List<AppoDetailsButton> buttonsList = [];
    for (var i = 0; i < BookedButtons.buttons.length; i++) {
      buttonsList.add(AppoDetailsButton(
        title: BookedButtons.buttons[i]['title'],
        subtitle: BookedButtons.buttons[i]['subtitle'],
        icon: BookedButtons.buttons[i]['icon'],
        caller: BookedButtons.buttons[i]['caller'],
      ));
    }
    setState(() {
      appoButtonsList = buttonsList;
    });
  }

  Future<List<AppoDetailsButton>> _getConfirmedAppoActionsList() async {
    var bookButtonsJson;
    List<AppoDetailsButton> buttonsList = [];
    String data = await DefaultAssetBundle.of(context)
        .loadString("assets/json/bookedButtons.json");
    bookButtonsJson = json.decode(data);
    for (var i = 0; i < bookButtonsJson.length; i++) {
      buttonsList.add(AppoDetailsButton(
        title: bookButtonsJson[i]['title'],
        subtitle: bookButtonsJson[i]['subtitle'],
        icon: bookButtonsJson[i]['icon'],
        caller: bookButtonsJson[i]['caller'],
      ));
    }
    setState(() {
      appoButtonsList = buttonsList;
    });
  }

  Future<List<AppoDetailsButton>> _getConfirmedCheckInAppoActionsList() async {
    var bookButtonsJson;
    List<AppoDetailsButton> buttonsList = [];
    String data = await DefaultAssetBundle.of(context)
        .loadString("assets/json/bookedButtons.json");
    bookButtonsJson = json.decode(data);
    for (var i = 0; i < bookButtonsJson.length; i++) {
      buttonsList.add(AppoDetailsButton(
        title: bookButtonsJson[i]['title'],
        subtitle: bookButtonsJson[i]['subtitle'],
        icon: bookButtonsJson[i]['icon'],
        caller: bookButtonsJson[i]['caller'],
      ));
    }
    setState(() {
      appoButtonsList = buttonsList;
    });
  }

  Future<List<AppoDetailsButton>> _getArrivedAppoActionsList() async {
    var bookButtonsJson;
    List<AppoDetailsButton> buttonsList = [];
    String data = await DefaultAssetBundle.of(context)
        .loadString("assets/json/bookedButtons.json");
    bookButtonsJson = json.decode(data);
    for (var i = 0; i < bookButtonsJson.length; i++) {
      buttonsList.add(AppoDetailsButton(
        title: bookButtonsJson[i]['title'],
        subtitle: bookButtonsJson[i]['subtitle'],
        icon: bookButtonsJson[i]['icon'],
        caller: bookButtonsJson[i]['caller'],
      ));
    }
    setState(() {
      appoButtonsList = buttonsList;
    });
  }

  Future<List<AppoDetailsButton>> _getArrivedInvoiceAppoActionsList() async {
    var bookButtonsJson;
    List<AppoDetailsButton> buttonsList = [];
    String data = await DefaultAssetBundle.of(context)
        .loadString("assets/json/bookedButtons.json");
    bookButtonsJson = json.decode(data);
    for (var i = 0; i < bookButtonsJson.length; i++) {
      buttonsList.add(AppoDetailsButton(
        title: bookButtonsJson[i]['title'],
        subtitle: bookButtonsJson[i]['subtitle'],
        icon: bookButtonsJson[i]['icon'],
        caller: bookButtonsJson[i]['caller'],
      ));
    }
    setState(() {
      appoButtonsList = buttonsList;
    });
  }
}
