import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:healthcare_user/common/settings.dart';
import 'package:healthcare_user/models/get_connected_doctors_model.dart';
import 'package:healthcare_user/my_connections/add-doctor.dart';

class AllConnections extends StatefulWidget {
  const AllConnections({Key? key}) : super(key: key);

  @override
  State<AllConnections> createState() => _AllConnectionsState();
}

class _AllConnectionsState extends State<AllConnections>
    with TickerProviderStateMixin {
  late TabController _controller;
  bool isConnectedDoctorsDataLoading=false;
  bool isSereverIssue=false;

  final List<Tab> topTabs = <Tab>[
    Tab(
        child: Text(
      'Doctors',
      style: TextStyle(fontSize: 14),
    )),
    Tab(
        child: Text(
      'Pharmacies',
      style: TextStyle(fontSize: 14),
    )),
  ];
  List<GetConnectedDoctorsModel> connectedDoctorsListOriginal = [];

  @override
  void initState() {
    _controller = TabController(vsync: this, length: topTabs.length);
    getAllConnectedDoctors();
    super.initState();
  }

  Future<void> getAllConnectedDoctors() async {
    isConnectedDoctorsDataLoading=true;
    try {
      var response = await AppSettings.getAllConnectedDoctors();

      setState(() {
        connectedDoctorsListOriginal = ((jsonDecode(response)) as List)
            .map((dynamic model) {
          return GetConnectedDoctorsModel.fromJson(model);
        }).toList();
        connectedDoctorsListOriginal=connectedDoctorsListOriginal.reversed.toList();
        isConnectedDoctorsDataLoading = false;
      });
    } catch (e) {
      setState(() {
        isConnectedDoctorsDataLoading = false;
        isSereverIssue = true;
      });
    }
  }


  Widget _doctors(){
    if(connectedDoctorsListOriginal.length!=0){
      return ListView.builder(
          padding:  EdgeInsets.all(0),
          itemCount: connectedDoctorsListOriginal.length,
          itemBuilder: (BuildContext context, int index) {
            return GestureDetector(
              onTap: (){

              },
              child: Card(

                //color: prescriptionsList[index].cardColor,
                child: Padding(
                  padding:EdgeInsets.all(8) ,
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        crossAxisAlignment: CrossAxisAlignment.center,
                        children: [

                          Container(


                            child: Row(
                              children: [
                                Column(
                                  mainAxisAlignment: MainAxisAlignment.start,
                                  crossAxisAlignment: CrossAxisAlignment.start,
                                  children: [
                                    Text(
                                      'Doctor Name',
                                      style: labelTextStyle(),
                                    ),
                                    SizedBox(height:MediaQuery.of(context).size.height * .01,),
                                    Text(
                                      'Hospital',
                                      style: labelTextStyle(),
                                    ),
                                    SizedBox(height:MediaQuery.of(context).size.height * .01,),
                                    Text(
                                      'Specialization',
                                      style: labelTextStyle(),
                                    ),
                                    SizedBox(height:MediaQuery.of(context).size.height * .01,),
                                    Text(
                                      'Qualification',
                                      style: labelTextStyle(),
                                    ),
                                    SizedBox(height:MediaQuery.of(context).size.height * .01,),

                                  ],
                                ),
                                SizedBox(width:MediaQuery.of(context).size.width * .01,),
                                Column(
                                  mainAxisAlignment: MainAxisAlignment.start,
                                  crossAxisAlignment: CrossAxisAlignment.start,
                                  children: [
                                    Text(
                                      ':',
                                      style: labelTextStyle(),
                                    ),
                                    SizedBox(height:MediaQuery.of(context).size.height * .01,),
                                    Text(
                                      ':',
                                      style: labelTextStyle(),
                                    ),
                                    SizedBox(height:MediaQuery.of(context).size.height * .01,),
                                    Text(
                                      ':',
                                      style: labelTextStyle(),
                                    ),
                                    SizedBox(height:MediaQuery.of(context).size.height * .01,),
                                    Text(
                                      ':',
                                      style: labelTextStyle(),
                                    ),

                                  ],
                                ),
                                SizedBox(width:MediaQuery.of(context).size.width * .01,),
                                Column(
                                  mainAxisAlignment: MainAxisAlignment.start,
                                  crossAxisAlignment: CrossAxisAlignment.start,
                                  children: [
                                    Text(connectedDoctorsListOriginal[index].doctor_name.toString().toUpperCase(),style: valuesTextStyle()),
                                    SizedBox(height:MediaQuery.of(context).size.height * .01,),
                                    Text(
                                      connectedDoctorsListOriginal[index].hospital_name,
                                      style: valuesTextStyle(),
                                    ),
                                    SizedBox(height:MediaQuery.of(context).size.height * .01,),
                                    Text(
                                      connectedDoctorsListOriginal[index].specialization,
                                      style: valuesTextStyle(),
                                    ),
                                    SizedBox(height:MediaQuery.of(context).size.height * .01,),
                                    Text(
                                      connectedDoctorsListOriginal[index].qualification,
                                      style: valuesTextStyle(),
                                    ),
                                  ],
                                ),
                              ],
                            ),




                          ),

                        ],

                      ),
                    ],
                  ),
                ),
              ),
            );
          });
    }
    else{
      return  Center(
          child: Padding(
            padding: EdgeInsets.fromLTRB(0, 40, 0, 0),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.center,
              children: [
                SizedBox(height: MediaQuery.of(context).size.height * .25,),
                Text('Click below icon to add new doctor'),
                SizedBox(
                  height: 20,
                ),
                CircleAvatar(
                  backgroundColor: primaryColor,
                  radius: 40,
                  child: IconButton(
                    iconSize: 40,
                    icon: const Icon(
                      Icons.add,
                      color: Colors.white,
                    ),
                    onPressed: () async {
                      Navigator.push(context, MaterialPageRoute(builder: (context) => AddDoctor())).then((value) {
                        getAllConnectedDoctors();
                      });
                    },
                  ),
                )
              ],
            ),
          )
      );
    }

  }

  Widget _pharmacies(){
    return Container();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('My Connections'),
        backgroundColor: primaryColor,
        bottom: TabBar(
          controller: _controller,
          tabs: topTabs,
          indicatorColor: buttonColors,
          unselectedLabelColor: Colors.white60,
          indicatorWeight: 2,
        ),
      ),
      body: Container(
        child: TabBarView(controller: _controller, children: [
          Padding(padding: EdgeInsets.all(10),
              child:isConnectedDoctorsDataLoading?Center(
                child: CircularProgressIndicator(
                  color: primaryColor,
                  strokeWidth: 5.0,
                ),
              ): _doctors(),
          ),
          Padding(
            padding: EdgeInsets.all(10),
          )
        ]),
      ),
      floatingActionButton: Visibility(
        visible:connectedDoctorsListOriginal.length!=0,
        child: CircleAvatar(
          backgroundColor: buttonColors,
          radius: 40,
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: <Widget>[
              IconButton(
                iconSize: 40,
                icon: const Icon(
                  Icons.add,
                  color: Colors.black,
                ),
                onPressed: () async{

                  Navigator.push(context, MaterialPageRoute(builder: (context) => AddDoctor())).then((value) {
                    getAllConnectedDoctors();
                  });
                },
              ),

            ],
          ),
        ),
      ),
    );
  }
}
