import 'package:diplomaticquarterapp/core/enum/viewstate.dart';
import 'package:diplomaticquarterapp/core/model/feedback/COC_items.dart';
import 'package:diplomaticquarterapp/core/viewModels/feedback/feedback_view_model.dart';
import 'package:diplomaticquarterapp/core/viewModels/project_view_model.dart';
import 'package:diplomaticquarterapp/pages/base/base_view.dart';
import 'package:diplomaticquarterapp/pages/feedback/feedback-detail.dart';
import 'package:diplomaticquarterapp/theme/colors.dart';
import 'package:diplomaticquarterapp/uitl/app_toast.dart';
import 'package:diplomaticquarterapp/uitl/translations_delegate_base.dart';
import 'package:diplomaticquarterapp/uitl/utils_new.dart';
import 'package:diplomaticquarterapp/widgets/buttons/defaultButton.dart';
import 'package:diplomaticquarterapp/widgets/dialogs/radio_selection_dialog.dart';
import 'package:diplomaticquarterapp/widgets/mobile-no/mobile_no.dart';
import 'package:diplomaticquarterapp/widgets/my_rich_text.dart';
import 'package:diplomaticquarterapp/widgets/others/app_scaffold_widget.dart';
import 'package:diplomaticquarterapp/widgets/transitions/fade_page.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

class StatusFeedbackPage extends StatefulWidget {
  @override
  _StatusFeedbackPageState createState() => _StatusFeedbackPageState();
}

class _StatusFeedbackPageState extends State<StatusFeedbackPage> {
  String countryCode = '966';
  String mobileNo = "";
  TextEditingController complainNumberController = TextEditingController();
  StatusType statusType = StatusType.ComplaintNumber;
  int selectedStatusIndex = 3;
  ProjectViewModel projectViewModel;
  @override
  Widget build(BuildContext context) {
    projectViewModel = Provider.of(context);
    return BaseView<FeedbackViewModel>(
      allowAny: true,
      onModelReady: (model) {
        model.cOCItemList.clear();
        if (projectViewModel.isLogin) model.getCOC();
      },
      builder: (_, model, widget) => AppScaffold(
        baseViewModel: model,
        isShowDecPage: false,
        body: Column(
          children: [
            Expanded(
              child: projectViewModel.isLogin
                  ? model.cOCItemList.isNotEmpty
                      ? listData(model.cOCItemList, projectViewModel.isArabic, true)
                      : Center(
                          child: Column(
                            children: [
                              SizedBox(
                                height: MediaQuery.of(context).size.height * 0.4,
                              ),
                              Image.asset(
                                'assets/images/comments.png',
                                width: 80,
                                height: 80,
                              ),
                              SizedBox(
                                height: 15,
                              ),
                              Text(
                                TranslationBase.of(context).noSearchResult,
                                style: TextStyle(
                                  fontSize: 16,
                                  fontWeight: FontWeight.w600,
                                  letterSpacing: -0.48,
                                  color: Color(0xff2B353E),
                                ),
                              ),
                            ],
                          ),
                        )
                  : SingleChildScrollView(
                      padding: EdgeInsets.all(21),
                      physics: BouncingScrollPhysics(),
                      child: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          Text(
                            TranslationBase.of(context).selectSearchCriteria,
                            style: TextStyle(fontSize: 16, fontWeight: FontWeight.w600, color: Color(0xff2B353E), letterSpacing: -0.64, height: 23 / 16),
                          ),
                          SizedBox(height: 21),
                          InkWell(
                            onTap: () {
                              List<RadioSelectionDialogModel> list = [
                                RadioSelectionDialogModel(TranslationBase.of(context).complaintNumber, 3),
                                RadioSelectionDialogModel(TranslationBase.of(context).fileNumber, 1),
                                RadioSelectionDialogModel(TranslationBase.of(context).identificationNumber, 2),
                              ];

                              showDialog(
                                context: context,
                                builder: (cxt) => RadioSelectionDialog(
                                  listData: list,
                                  selectedIndex: selectedStatusIndex,
                                  onValueSelected: (index) {
                                    selectedStatusIndex = index;
                                    setState(() {
                                      if (index == 3) {
                                        statusType = StatusType.ComplaintNumber;
                                      } else if (index == 1) {
                                        statusType = StatusType.FileNumber;
                                      } else {
                                        statusType = StatusType.IdentificationNumber;
                                      }
                                    });
                                  },
                                ),
                              );
                            },
                            child: Container(
                              padding: EdgeInsets.only(left: 16, right: 16, bottom: 15, top: 15),
                              alignment: Alignment.center,
                              decoration: BoxDecoration(
                                borderRadius: BorderRadius.circular(15),
                                color: Colors.white,
                                border: Border.all(
                                  color: Color(0xffefefef),
                                  width: 1,
                                ),
                              ),
                              child: Row(
                                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                children: <Widget>[
                                  Text(
                                    getSelected(context),
                                    style: TextStyle(
                                      fontSize: 16,
                                      fontWeight: FontWeight.w600,
                                      letterSpacing: -0.48,
                                      color: Color(0xff2B353E),
                                    ),
                                  ),
                                  Icon(
                                    Icons.keyboard_arrow_down,
                                    size: 22,
                                    color: Colors.grey,
                                  )
                                ],
                              ),
                            ),
                          ),
                          SizedBox(height: 12),
                          Directionality(
                              textDirection: TextDirection.ltr,
                              child: inputWidget(
                                  statusType == StatusType.ComplaintNumber
                                      ? TranslationBase.of(context).enterComplainNumber
                                      : (statusType == StatusType.FileNumber ? TranslationBase.of(context).enterfileNumber : TranslationBase.of(context).enterIdentificationNumber),
                                  "",
                                  complainNumberController,
                                  isInputTypeNum: true)),
                          SizedBox(height: 12),
                          PhoneNumberSelectorWidget(onNumberChange: (value) {
                            setState(() {
                              mobileNo = value;
                            });
                          }, onCountryChange: (value) {
                            setState(() {
                              countryCode = value;
                            });
                          }),
                          if (model.cOCItemList.length > 0) listData(model.cOCItemList, projectViewModel.isArabic, false),
                        ],
                      ),
                    ),
            ),
            if (!projectViewModel.isLogin)
              Container(
                color: Colors.white,
                padding: EdgeInsets.only(top: 16, bottom: 16, right: 21, left: 21),
                child: DefaultButton(
                  TranslationBase.of(context).search,
                  projectViewModel.isLogin || (mobileNo.length < 9) || (complainNumberController.text.isEmpty)
                      ? null
                      : () {
                          model.searchFeedback(countryCode, mobileNo, selectedStatusIndex, complainNumberController.text).then((value) {
                            if (model.state == ViewState.ErrorLocal) {
                              Future.delayed(Duration(seconds: 1), () {
                                AppToast.showErrorToast(message: model.error);
                              });
                            } else {}
                          });
                        },
                  disabledColor: Colors.grey,
                ),
              ),
          ],
        ),
      ),
    );
  }

  Widget listData(List<COCItem> cOCItemList, bool isArabic, bool isLogin) {
    return ListView.builder(
      padding: isLogin ? EdgeInsets.all(21) : null,
      itemCount: cOCItemList.length,
      shrinkWrap: isLogin ? false : true,
      physics: isLogin ? null : NeverScrollableScrollPhysics(),
      itemBuilder: (context, index) => InkWell(
        onTap: () {
        //  sss
        },
        child: Container(
          margin: EdgeInsets.only(top: 12),
          decoration: BoxDecoration(
            color: (cOCItemList[index].statusId==7||cOCItemList[index].statusId==8)?CustomColors.accentColor:(cOCItemList[index].statusId==10||cOCItemList[index].statusId==6||cOCItemList[index].statusId==9)?CustomColors.green:CustomColors.orange,
            borderRadius: BorderRadius.all(
              Radius.circular(10.0),
            ),
            boxShadow: [
              BoxShadow(
                color: Color(0xff000000).withOpacity(.05),
                blurRadius: 27,
                offset: Offset(0, -3),
              ),
            ],
          ),

          child: Container(
            margin: EdgeInsets.only(left: projectViewModel.isArabic ? 0 : 6, right: projectViewModel.isArabic ? 6 : 0),
            padding: EdgeInsets.symmetric(vertical: 14, horizontal: 12),
            decoration: BoxDecoration(
              color: Colors.white,
              border: Border.all(color: Colors.white, width: 1),
              borderRadius: BorderRadius.only(
                bottomRight: projectViewModel.isArabic ? Radius.circular(0) : Radius.circular(10.0),
                topRight: projectViewModel.isArabic ? Radius.circular(0) : Radius.circular(10.0),
                bottomLeft: projectViewModel.isArabic ? Radius.circular(10.0) : Radius.circular(0),
                topLeft: projectViewModel.isArabic ? Radius.circular(10.0) : Radius.circular(0),
              ),
            ),
            child: Padding(
              padding: const EdgeInsets.all(12.0),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Text(isArabic ? cOCItemList[index].statusAr : cOCItemList[index].status, style: TextStyle(fontSize: 14.0, letterSpacing: -0.56, fontWeight: FontWeight.bold)),
                      Container(
                          margin: EdgeInsets.only(top: 5.0),
                          child: Text(cOCItemList[index].formType.toString(),
                              style: TextStyle(fontSize: 14, fontWeight: FontWeight.w600, fontFamily: isArabic ? 'Cairo' : 'Poppins', color: Color(0xff2B353E), letterSpacing: -0.48, height: 18 / 12))),
                      MyRichText(TranslationBase.of(context).number + ": ", cOCItemList[index].itemID.toString(), isArabic),
                      Text(cOCItemList[index].cOCTitle,
                          style: TextStyle(fontSize: 14, fontWeight: FontWeight.w600, fontFamily: isArabic ? 'Cairo' : 'Poppins', color: Color(0xff2B353E), letterSpacing: -0.48, height: 18 / 12)),
                    ],
                  ),
                  Column(
                    crossAxisAlignment: CrossAxisAlignment.end,
                    children: [
                      Text(cOCItemList[index].date.split(" ")[0],
                          style: TextStyle(fontSize: 12, fontWeight: FontWeight.w600, fontFamily: isArabic ? 'Cairo' : 'Poppins', color: Color(0xff2B353E), letterSpacing: -0.48)),
                      Text(cOCItemList[index].date.split(" ")[1].substring(0, 4),
                          style: TextStyle(fontSize: 12, fontWeight: FontWeight.w600, fontFamily: isArabic ? 'Cairo' : 'Poppins', color: Color(0xff2B353E), letterSpacing: -0.48)),
                    ],
                  ),
                ],
              ),
            ),
          ),
        ),
      ),
    );
  }

  Widget inputWidget(String _labelText, String _hintText, TextEditingController _controller,
      {VoidCallback suffixTap, bool isEnable = true, bool hasSelection = false, int lines, bool isInputTypeNum = false}) {
    return Container(
      padding: EdgeInsets.only(left: 16, right: 16, bottom: 15, top: 15),
      alignment: Alignment.center,
      decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(15),
        color: Colors.white,
        border: Border.all(
          color: Color(0xffefefef),
          width: 1,
        ),
      ),
      child: InkWell(
        onTap: hasSelection ? () {} : null,
        child: Row(
          children: [
            Expanded(
              child: Column(
                mainAxisSize: MainAxisSize.min,
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Text(
                    _labelText,
                    style: TextStyle(
                      fontSize: 11,
                      fontWeight: FontWeight.w600,
                      color: Color(0xff2B353E),
                      letterSpacing: -0.44,
                    ),
                  ),
                  TextField(
                    enabled: isEnable,
                    scrollPadding: EdgeInsets.zero,
                    keyboardType: isInputTypeNum ? TextInputType.number : TextInputType.text,
                    controller: _controller,
                    maxLines: lines,
                    onChanged: (value) => {setState(() {})},
                    style: TextStyle(
                      fontSize: 14,
                      height: 21 / 14,
                      fontWeight: FontWeight.w400,
                      color: Color(0xff2B353E),
                      letterSpacing: -0.44,
                    ),
                    decoration: InputDecoration(
                      isDense: true,
                      hintText: _hintText,
                      hintStyle: TextStyle(
                        fontSize: 14,
                        height: 21 / 14,
                        fontWeight: FontWeight.w400,
                        color: Color(0xff575757),
                        letterSpacing: -0.56,
                      ),
                      suffixIconConstraints: BoxConstraints(minWidth: 50),
                      suffixIcon: suffixTap == null ? null : IconButton(icon: Icon(Icons.mic, color: Color(0xff2E303A)), onPressed: suffixTap),
                      contentPadding: EdgeInsets.zero,
                      border: InputBorder.none,
                      focusedBorder: InputBorder.none,
                      enabledBorder: InputBorder.none,
                    ),
                  ),
                ],
              ),
            ),
            if (hasSelection) Icon(Icons.keyboard_arrow_down_outlined),
          ],
        ),
      ),
    );
  }

  gotodetails(item) {
    Navigator.pushReplacement(context, FadePage(page: FeedbackDetails(items: item)));
  }

  String getSelected(BuildContext context) {
    switch (statusType) {
      case StatusType.ComplaintNumber:
        return TranslationBase.of(context).complaintNumber;
        break;
      case StatusType.FileNumber:
        return TranslationBase.of(context).fileNumber;
        break;
      case StatusType.IdentificationNumber:
        return TranslationBase.of(context).identificationNumber;
        break;
    }
    return TranslationBase.of(context).notClassified;
  }
}

enum StatusType {
  ComplaintNumber,
  FileNumber,
  IdentificationNumber,
}
