import 'package:diplomaticquarterapp/models/Appointments/DoctorListResponse.dart';
import 'package:diplomaticquarterapp/uitl/translations_delegate_base.dart';
import 'package:diplomaticquarterapp/widgets/others/app_scaffold_widget.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';

import 'DoctorView.dart';

class BranchView extends StatefulWidget {
  final List<DoctorList> doctorsList;

  final List<String> result;
  final List<String> resultDistance;
  final int num;

  const BranchView(
      {Key key, this.doctorsList, this.result, this.resultDistance, this.num})
      : super(key: key);

  @override
  _BranchViewState createState() => _BranchViewState();
}

class _BranchViewState extends State<BranchView> {
  @override
  Widget build(BuildContext context) {
    return AppScaffold(
      appBarTitle: TranslationBase.of(context).bookAppo,
      isShowAppBar: true,
      body: new ListView.builder(
        itemBuilder: (BuildContext context, int index) {
          return new ExpandableListView(
              result2: widget.result,
              resultDistance: widget.resultDistance,
              val: index,
              doctorsList2: widget.doctorsList);
        },
        itemCount: widget.num, //5,
      ),
    );
  }
}

class ExpandableListView extends StatefulWidget {
  final List<String> result2;
  final List<String> resultDistance;
  final List<DoctorList> doctorsList2;
  final val;

  const ExpandableListView(
      {Key key, this.result2, this.resultDistance, this.val, this.doctorsList2})
      : super(key: key);

  @override
  _ExpandableListViewState createState() => new _ExpandableListViewState();
}

class _ExpandableListViewState extends State<ExpandableListView> {
  bool expandFlag = false;

  @override
  Widget build(BuildContext context) {
    return new Container(
      width: MediaQuery.of(context).size.width * 0.6,
      margin: EdgeInsets.fromLTRB(20.0, 10.0, 20.0, 0.0),
      child: Card(
        color: Colors.white,
        shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.circular(10),
        ),
        child: Container(
          decoration: BoxDecoration(),
          padding: EdgeInsets.all(5.0),
          width: MediaQuery.of(context).size.width,
          child: new Column(
            children: <Widget>[
              new Container(
                margin: EdgeInsets.only(left: 5.0, right: 5.0),
                color: Colors.white,
                child: new Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: <Widget>[
                    new Text(
                      widget.result2[widget.val].toString() +
                          " " +
                          getProjectDistance(
                              widget.resultDistance[widget.val].toString()),
                      style: new TextStyle(
                          fontWeight: FontWeight.bold, color: Colors.black),
                    ),
                    new IconButton(
                        icon: new Container(
                          height: 28.0,
                          width: 30.0,
                          decoration: new BoxDecoration(
                            color: Colors.red,
                            shape: BoxShape.circle,
                          ),
                          child: new Center(
                            child: new Icon(
                              expandFlag
                                  ? Icons.keyboard_arrow_up
                                  : Icons.keyboard_arrow_down,
                              color: Colors.white,
                              size: 30.0,
                            ),
                          ),
                        ),
                        onPressed: () {
                          setState(() {
                            expandFlag = !expandFlag;
                          });
                        }),
                  ],
                ),
              ),
              // SizedBox(height: 15),
              new ExpandableContainer(
                expanded: expandFlag,
                child: ListView.builder(
                  scrollDirection: Axis.vertical,
                  shrinkWrap: true,
                  physics: ScrollPhysics(),
                  padding: EdgeInsets.all(0.0),
                  itemCount: widget.doctorsList2.length,
                  itemBuilder: (context, index) {
                    return widget.result2[widget.val].toString() ==
                            widget.doctorsList2[index].projectName.toString()
                        ? DoctorView(
                            //AJ note
                            doctor: widget.doctorsList2[index]

                            //  widget.doctorsList2[index]
                            )
                        : Container();
                  },
                ),
              )
            ],
          ),
        ),
      ),
    );
  }

  String getProjectDistance(String distance) {
    if (distance != "0")
      return "   -   " + distance + " " + TranslationBase.of(context).km;
    else {
      return "";
    }
  }
}

class ExpandableContainer extends StatelessWidget {
  final bool expanded;
  final double collapsedHeight;
  final double expandedHeight;
  final Widget child;

  ExpandableContainer({
    @required this.child,
    this.collapsedHeight = 0.0,
    this.expandedHeight = 300.0,
    this.expanded = true,
  });

  @override
  Widget build(BuildContext context) {
    double screenWidth = MediaQuery.of(context).size.width;
    return new AnimatedContainer(
      duration: new Duration(milliseconds: 500),
      curve: Curves.easeInOut,
      width: screenWidth,
      height: expanded ? MediaQuery.of(context).size.height : collapsedHeight,
      child: new Container(
        child: child,
        decoration: new BoxDecoration(
            border: new Border.all(width: 1.0, color: Colors.white)),
      ),
    );
  }
}
