import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:test_sa/controllers/providers/api/ppm_provider.dart';
import 'package:test_sa/controllers/providers/api/user_provider.dart';
import 'package:test_sa/extensions/context_extension.dart';
import 'package:test_sa/extensions/int_extensions.dart';
import 'package:test_sa/extensions/widget_extensions.dart';
import 'package:test_sa/models/all_requests_and_count_model.dart';
import 'package:test_sa/models/ppm/ppm.dart';
import 'package:test_sa/new_views/app_style/app_color.dart';
import 'package:test_sa/new_views/common_widgets/app_filled_button.dart';
import 'package:test_sa/new_views/common_widgets/default_app_bar.dart';
import 'package:test_sa/views/widgets/loaders/loading_manager.dart';

import '../../../../../extensions/text_extensions.dart';
import '../../../../widgets/pentry/pentry_calibration_tool_form.dart';
import '../../../../widgets/pentry/pentry_pm_kit_form.dart';
import '../../../../widgets/pentry/pentry_ppm_check_list_form.dart';
import '../../../../widgets/pentry/pentry_tbs_form.dart';

class EditPpm extends StatefulWidget {
  final Ppm ppm;
  final RequestsDetails details;

  const EditPpm({Key key, this.ppm, this.details}) : super(key: key);

  @override
  State<EditPpm> createState() => _EditPpmState();
}

class _EditPpmState extends State<EditPpm> with SingleTickerProviderStateMixin {
  UserProvider _userProvider;
  PpmProvider _regularVisitsProvider;
  Ppm _ppm;
  final GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();

  TabController _tabController;

  _onSubmit() async {
    if (!(await _ppm.validate(context))) {
      setState(() {});
      return;
    }
    _ppm.removeEmptyObjects();
    await _regularVisitsProvider.updatePentry(context, user: _userProvider.user, ppm: widget.ppm);
  }

  @override
  void initState() {
    _ppm = widget.ppm;
    _tabController = TabController(length: 4, vsync: this);
    super.initState();
  }

  @override
  void dispose() {
    _tabController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    _userProvider = Provider.of<UserProvider>(context);
    _regularVisitsProvider = Provider.of<PpmProvider>(context);
    return Scaffold(
      appBar: DefaultAppBar(title: context.translation.preventiveMaintenance),
      key: _scaffoldKey,
      body: SafeArea(
        child: LoadingManager(
          isLoading: false,
          isFailedLoading: false,
          stateCode: 200,
          onRefresh: () async {},
          child: Column(
            children: [
              Container(
                decoration: BoxDecoration(color: context.isDark ? AppColor.neutral50 : AppColor.neutral30, borderRadius: BorderRadius.circular(16)),
                child: TabBar(
                  controller: _tabController,
                  isScrollable: true,
                  padding: EdgeInsets.zero,
                  labelColor: context.isDark ? AppColor.neutral30 : AppColor.neutral60,
                  unselectedLabelColor: context.isDark ? AppColor.neutral10 : AppColor.neutral20,
                  unselectedLabelStyle: AppTextStyles.bodyText,
                  labelStyle: AppTextStyles.bodyText,
                  indicatorPadding: const EdgeInsets.all(4),
                  indicator: BoxDecoration(color: context.isDark ? AppColor.neutral60 : Theme.of(context).cardColor, borderRadius: BorderRadius.circular(13)),
                  onTap: (index) {
                    setState(() {});
                  },
                  tabs: [
                    Tab(text: "TBS", height: 57.toScreenHeight),
                    Tab(text: "Calibration Tool", height: 57.toScreenHeight),
                    Tab(text: "PM Kit", height: 57.toScreenHeight),
                    Tab(text: "PPM Checklist", height: 57.toScreenHeight),
                  ],
                ),
              ),
              TabBarView(
                physics: const NeverScrollableScrollPhysics(),
                controller: _tabController,
                children: [
                  PentryTBSForm(model: _ppm),
                  PentryCalibrationToolForm(models: _ppm.vCalibrationTools),
                  PentryPMKitForm(models: _ppm.vKits, assetId: widget.ppm?.assetId),
                  PentryPMChecklistForm(models: _ppm.vChecklists),
                ],
              ).expanded,
              AppFilledButton(
                onPressed: () {
                  if (_tabController.index == 3) {
                    _onSubmit();
                  } else {
                    _tabController.animateTo(_tabController.index + 1);
                    setState(() {});
                  }
                },
                label: _tabController.index == 3 ? context.translation.submit : context.translation.next,
              ).paddingOnly(top: 16),
            ],
          ).paddingAll(16),
        ),
      ),
    );
  }
}
