import 'dart:convert';

import 'package:doctor/common/settings.dart';
import 'package:doctor/models/resources_model.dart';
import 'package:doctor/resources/add_resurces.dart';
import 'package:flutter/material.dart';

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

  @override
  State<Resources> createState() => _ResourcesState();
}

class _ResourcesState extends State<Resources> {

  bool isDataLoading=false;
  bool isSereverIssue=false;
  List<ResourcesModel> resourcecList = [];

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

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

  @override
  void initState() {
    getAllResources();
    super.initState();
  }

  Widget _dos(dos){

    if(dos.length!=0){
      return ListView.builder(
          padding:  EdgeInsets.all(0),
          itemCount: dos.length,
          itemBuilder: (BuildContext context, int index) {
            return Container(
              child: Text(dos[index]['dos'].toString(),style: valuesTextStyle()),
            );
          });
    }
    else{
      return Text('No dos found for this resource',style: valuesTextStyle());
    }

  }

  Widget _donots(donots){

    if(donots.length!=0){
      return ListView.builder(
          padding:  EdgeInsets.all(0),
          itemCount: donots.length,
          itemBuilder: (BuildContext context, int index) {
            return Container(
              child: Text(donots[index]['doNots'].toString(),style: valuesTextStyle()),
            );
          });
    }
    else{
      return Text('No doNots found for this resource',style: valuesTextStyle());
    }

  }

  Widget _urls(urls){

    if(urls.length!=0){
      return ListView.builder(
          padding:  EdgeInsets.all(0),
          itemCount: urls.length,
          itemBuilder: (BuildContext context, int index) {
            return Container(
              child: Text(urls[index]['url'].toString(),style: valuesTextStyle()),
            );
          });
    }
    else{
      return Text('No urls found for this resource',style: valuesTextStyle());
    }

  }

  Widget _resources(){
    if(resourcecList.length!=0){
      return ListView.builder(
          padding:  EdgeInsets.all(0),
          itemCount: resourcecList.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,
                    mainAxisAlignment: MainAxisAlignment.start,
                    children: [
                      Row(
                        mainAxisAlignment: MainAxisAlignment.start,
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [

                          Container(


                            child: Row(
                              mainAxisAlignment: MainAxisAlignment.start,
                              crossAxisAlignment: CrossAxisAlignment.start,
                              children: [
                                Column(
                                  mainAxisAlignment: MainAxisAlignment.start,
                                  crossAxisAlignment: CrossAxisAlignment.start,
                                  children: [
                                    Text(
                                      'Resource Name',
                                      style: labelTextStyle(),
                                    ),
                                    SizedBox(height:MediaQuery.of(context).size.height * .01,),
                                    Container(
                                        height:MediaQuery.of(context).size.height * .10,
                                      child:  Text(
                                        'Dos',
                                        style: labelTextStyle(),
                                      ),
                                    ),

                                    SizedBox(height:MediaQuery.of(context).size.height * .01,),
                                    Container(
                                      height:MediaQuery.of(context).size.height * .10,
                                      child:  Text(
                                        'DoNots',
                                        style: labelTextStyle(),
                                      ),
                                    ),
                                    SizedBox(height:MediaQuery.of(context).size.height * .01,),
                                    Container(
                                      height:MediaQuery.of(context).size.height * .10,
                                      child:  Text(
                                        'Urls',
                                        style: labelTextStyle(),
                                      ),
                                    ),
                                  ],
                                ),
                                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,),
                                    Container(
                                      height:MediaQuery.of(context).size.height * .10,
                                      child:  Text(
                                        ':',
                                        style: labelTextStyle(),
                                      ),
                                    ),
                                    SizedBox(height:MediaQuery.of(context).size.height * .01,),
                                    Container(
                                      height:MediaQuery.of(context).size.height * .10,
                                      child:  Text(
                                        ':',
                                        style: labelTextStyle(),
                                      ),
                                    ),
                                    SizedBox(height:MediaQuery.of(context).size.height * .01,),
                                    Container(
                                      height:MediaQuery.of(context).size.height * .10,
                                      child:  Text(
                                        ':',
                                        style: labelTextStyle(),
                                      ),
                                    ),
                                  ],
                                ),
                                SizedBox(width:MediaQuery.of(context).size.width * .01,),
                                Column(
                                  mainAxisAlignment: MainAxisAlignment.start,
                                  crossAxisAlignment: CrossAxisAlignment.start,
                                  children: [
                                    Text(resourcecList[index].resource_name.toString().toUpperCase(),style: valuesTextStyle()),
                                    SizedBox(height:MediaQuery.of(context).size.height * .01,),
                                    Container(
                                      height:MediaQuery.of(context).size.height * .10,
                                      width:MediaQuery.of(context).size.width * .80,
                                      child: _dos(resourcecList[index].dos),
                                    ),
                                    SizedBox(height:MediaQuery.of(context).size.height * .01,),
                                    Container(
                                      height:MediaQuery.of(context).size.height * .10,
                                      width:MediaQuery.of(context).size.width * .80,
                                      child: _donots(resourcecList[index].donts),
                                    ),
                                    SizedBox(height:MediaQuery.of(context).size.height * .01,),
                                    Container(
                                      height:MediaQuery.of(context).size.height * .10,
                                      width:MediaQuery.of(context).size.width * .80,
                                      child: _urls(resourcecList[index].urls),
                                    )

                                  ],
                                ),
                              ],
                            ),




                          ),

                        ],

                      ),
                    ],
                  ),
                ),
              ),
            );
          });
    }
    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 resource'),
                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) => AddResources())).then((value) {
                        getAllResources();
                      });
                    },
                  ),
                )
              ],
            ),
          )
      );
    }

  }


  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppSettings.appBar('Resources'),
      body:  Container(
        child: isDataLoading?Center(
          child: CircularProgressIndicator(
            color: primaryColor,
            strokeWidth: 5.0,
          ),
        ): _resources(),
      ),
      floatingActionButton: Visibility(
      visible:resourcecList.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) => AddResources())).then((value) {
                  getAllResources();
                });
              },
            ),
          ],
        ),
      ),
    ),
    );
  }
}
