import 'dart:convert';
import 'dart:io';

import 'package:flutter/material.dart';
import 'package:test_sa/extensions/context_extension.dart';
import 'package:test_sa/extensions/int_extensions.dart';
import 'package:test_sa/extensions/string_extensions.dart';
import 'package:test_sa/models/ppm/ppm.dart';
import 'package:test_sa/models/ppm/ppm_attachment.dart';

import '../../../models/lookup.dart';
import '../../../new_views/common_widgets/app_text_form_field.dart';
import '../../../new_views/common_widgets/single_item_drop_down_menu.dart';
import '../../../providers/ppm_device_status_provider.dart';
import '../../../providers/ppm_visit_status_provider.dart';
import '../date_and_time/date_picker.dart';
import '../e_signature/e_signature.dart';
import '../images/multi_image_picker.dart';
import '../timer/app_timer.dart';

class PentryTBSForm extends StatefulWidget {
  final Ppm model;

  const PentryTBSForm({Key key, this.model}) : super(key: key);

  @override
  State<PentryTBSForm> createState() => _PentryTBSFormState();
}

class _PentryTBSFormState extends State<PentryTBSForm> {
  @override
  Widget build(BuildContext context) {
    widget.model?.files ??= [];
    widget.model?.files = (widget.model.files ?? []).where((element) => element.attachmentName != null && element.attachmentName.isNotEmpty).toList();

    return SingleChildScrollView(
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.stretch,
        children: [
          8.height,
          SingleItemDropDownMenu<Lookup, PPMVisitStatusProvider>(
            context: context,
            initialValue: widget.model.visitStatusId == null ? null : Lookup(name: widget.model.visitStatusName ?? "", id: widget.model.visitStatusId),
            title: context.translation.ppmVisit,
            onSelect: (value) {
              if (value?.value == 4) {
                "Status cannot be change to ${value.name}.".addTranslation.showToast;
                setState(() {});
                return;
              }

              if (value != null) {
                widget.model.visitStatusName = value.name;
                widget.model.visitStatusId = value.id;
              }
            },
          ),
          8.height,
          AppTimer(
            label: context.translation.timer,
            timer: widget.model.tbsTimer,
            enabled: widget.model.tbsTimer.endAt == null,
            onChange: (timer) async {
              widget.model.tbsTimer = timer;
              return true;
            },
          ),
          8.height,
          SingleItemDropDownMenu<Lookup, PPMDeviceStatusProvider>(
            context: context,
            initialValue: widget.model.deviceStatusId == null ? null : Lookup(name: widget.model.deviceStatusName ?? "", id: widget.model.deviceStatusId),
            title: context.translation.deviceStatus,
            onSelect: (value) {
              if (value != null) {
                widget.model.deviceStatusId = value.id;
                widget.model.deviceStatusName = value.name;
              }
            },
          ),
          8.height,
          ADatePicker(
            label: context.translation.actualVisitDate,
            date: DateTime.tryParse(widget.model.actualDate ?? ""),
            from: DateTime.now().subtract(const Duration(days: 30)),
            onDatePicker: (date) {
              if (date == null) return;
              widget.model.actualDate = date?.toIso8601String();
              setState(() {});
            },
          ),
          8.height,
          ADatePicker(
            label: context.translation.expectedVisitDate,
            date: DateTime.tryParse(widget.model.expectedDate ?? ""),
            from: DateTime.now().subtract(const Duration(days: 30)),
            onDatePicker: (date) {
              if (date == null) return;
              widget.model.expectedDate = date?.toIso8601String();
              setState(() {});
            },
          ),
          8.height,
          AppTextFormField(
            labelText: context.translation.travelingHours,
            initialValue: (widget.model.travelingHours ?? "").toString(),
            textAlign: TextAlign.center,
            style: Theme.of(context).textTheme.titleMedium,
            textInputType: TextInputType.number,
            onChange: (value) {
              widget.model.travelingHours = value;
            },
          ),
          8.height,
          MultiFilesPicker(
            label: context.translation.attachImage,
            files: widget.model.files.map((e) => File(e.attachmentName)).toList(),
            onChange: (files) {
              widget.model.files = files.map((e) => PpmAttachments(attachmentName: e.path)).toList();
            },
          ),
          8.height,
          ESignature(
            title: context.translation.nurseSignature,
            oldSignature: widget.model.nurseSignature,
            newSignature: widget.model.localNurseSignature,
            onChange: (signature) {
              widget.model.localNurseSignature = signature;
              widget.model.nurseSignature = "${DateTime.now().toIso8601String()}.png|${base64Encode(signature)}";
            },
          ),
          8.height,
          ESignature(
            title: context.translation.engSign,
            oldSignature: widget.model.engSignature,
            newSignature: widget.model.localEngineerSignature,
            onChange: (signature) {
              widget.model.localEngineerSignature = signature;
              widget.model.engSignature = "${DateTime.now().toIso8601String()}.png|${base64Encode(signature)}";
            },
          ),
          8.height,
        ],
      ),
    );
  }
}
