import 'package:diplomaticquarterapp/models/Appointments/DoctorListResponse.dart';
import 'package:diplomaticquarterapp/services/appointment_services/GetDoctorsList.dart';
import 'package:diplomaticquarterapp/uitl/app_toast.dart';
import 'package:diplomaticquarterapp/uitl/translations_delegate_base.dart';
import 'package:flutter/material.dart';
import 'package:smart_progress_bar/smart_progress_bar.dart';

import '../SearchResults.dart';

class SearchByDoctor extends StatefulWidget {
  @override
  _SearchByDoctorState createState() => _SearchByDoctorState();
}

class _SearchByDoctorState extends State<SearchByDoctor> {
  TextEditingController doctorNameController = new TextEditingController();
  bool _isButtonDisabled;

  @override
  void initState() {
    _isButtonDisabled = true;
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      margin: EdgeInsets.all(20.0),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        mainAxisSize: MainAxisSize.max,
        children: <Widget>[
          Container(
            margin: EdgeInsets.only(top: 10.0, bottom: 15.0),
            child: Text(TranslationBase.of(context).searchByDocText,
                style: TextStyle(fontSize: 16.0, letterSpacing: 0.9)),
          ),
          Container(
            decoration: BoxDecoration(color: Colors.white),
            child: TextField(
                controller: doctorNameController,
                onChanged: (content) {
                  _onDocTextChanged(content);
                },
                decoration: InputDecoration(
                    labelText: TranslationBase.of(context).enterDocName,
                    fillColor: Colors.white,
                    prefixIcon: Icon(Icons.search),
                    border: OutlineInputBorder())),
          ),
          Expanded(
            child: Align(
              alignment: FractionalOffset.bottomCenter,
              child: _buildCounterButton(),
            ),
          ),
        ],
      ),
    );
  }

  Widget _buildCounterButton() {
    return new ButtonTheme(
      shape: RoundedRectangleBorder(
        borderRadius: BorderRadius.circular(10.0),
      ),
      minWidth: MediaQuery.of(context).size.width,
      height: 45.0,
      child: RaisedButton(
        color: new Color(0xFF60686b),
        textColor: Colors.white,
        disabledTextColor: Colors.white,
        disabledColor: new Color(0xFFbcc2c4),
        onPressed: _isButtonDisabled ? null : _searchDoctor(context),
        child: Text(TranslationBase.of(context).search,
            style: TextStyle(fontSize: 18.0)),
      ),
    );
  }

  getDoctorsList(BuildContext context) {
    List<DoctorList> doctorsList = [];
    DoctorsListService service = new DoctorsListService();
    service.getDoctorsListByName(doctorNameController.text, context).then((res) {
      if (res['MessageStatus'] == 1) {
        setState(() {
        if(res['DoctorList'].length != 0) {
          res['DoctorList'].forEach((v) {
            doctorsList.add(new DoctorList.fromJson(v));
          });
        } else {}
        });
        navigateToSearchResults(context, doctorsList);
      } else {
        AppToast.showErrorToast(message: res['ErrorEndUserMessage']);
      }
    }).catchError((err) {
      print(err);
    }).showProgressBar(text: "Loading", backgroundColor: Colors.blue.withOpacity(0.6));
  }

  _onDocTextChanged(content) {
    print(content);
    if (content.length >= 4) {
      setState(() {
        _isButtonDisabled = false;
      });
    } else {
      setState(() {
        _isButtonDisabled = true;
      });
    }
    print(_isButtonDisabled);
  }

  _searchDoctor(BuildContext context) {
    getDoctorsList(context);
  }

  Future navigateToSearchResults(context, List<DoctorList> docList) async {
    Navigator.push(
        context,
        MaterialPageRoute(
            builder: (context) => SearchResults(doctorsList: docList)));
  }
}
