// ignore_for_file: unused_local_variable, unused_field, non_constant_identifier_names, depend_on_referenced_packages

import 'package:flutter/material.dart';
import 'package:gozap_package/components/coloredcontainer.dart';
import 'package:gozap_package/components/icons/gozap_icons.dart';
import 'package:gozap_package/components/quantity_input.dart';
import 'package:gozap_package/components/responsive.dart';
import 'package:gozap_package/components/themes/themes.dart';
import 'package:gozap_package/models/user/user_model.dart';
import 'package:gozap_package/views/log_in/log_in_view.dart';
import 'package:intl/intl.dart';
import 'package:provider/provider.dart';
import 'package:qr_flutter/qr_flutter.dart';
// import model
import '/models/dashboard/dashboard_model.dart';
// import controller
import '/controllers/dashboard/dashboard_controller.dart';

enum Menu { itemOne, itemTwo, itemThree, itemFour }

class DashboardView extends StatefulWidget {
  const DashboardView({super.key});

  @override
  State<DashboardView> createState() => _DashboardViewState();
}

class _DashboardViewState extends State<DashboardView> {
  int simpleIntInput = 1;
  final String _selectedMenu = '';
  @override
  Widget build(BuildContext context) {
    DashboardController viewController = DashboardController();
    return ChangeNotifierProvider<DashboardModel>(
      create: (context) => DashboardModel.instance(context),
      child: Consumer<DashboardModel>(
        builder: (context, viewModel, child) {
          if (viewModel.status == DashboardModelStatus.Loading) {
            return const Center(
              child: CircularProgressIndicator.adaptive(),
            );
          } else if (viewModel.swapModule == null) {
            return const Center(
              child: Text("Somthing went wrong"),
            );
          } else {
            var topBarWidget = Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                SizedBox(
                  child: Card(
                    elevation: 10,
                    shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.circular(15),
                    ),
                    child: ClipRRect(
                      borderRadius: const BorderRadius.all(Radius.circular(15)),
                      child: Image.asset(
                        'assets/images/logos/gozap_small_color_logo.png',
                        width: 50,
                        fit: BoxFit.cover,
                        package: "gozap_package",
                      ),
                    ),
                  ),
                ),
                SizedBox(
                  width: 50,
                  child: Card(
                    elevation: 10,
                    shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.circular(15),
                    ),
                    child: ClipRRect(
                      borderRadius: const BorderRadius.all(Radius.circular(15)),
                      child: PopupMenuButton(
                          onSelected: ((Menu item) {
                            if (item == Menu.itemFour) {
                              Provider.of<userChangeNotifier>(context,
                                      listen: false)
                                  .remove(
                                      context,
                                      LogInView(
                                          nextScreen: const DashboardView()));
                            }
                          }),
                          position: PopupMenuPosition.under,
                          padding: const EdgeInsets.all(0),
                          icon: Image.asset(
                            'assets/images/personal_info_profile.png',
                            width: 100,
                            fit: BoxFit.cover,
                            package: "gozap_package",
                          ),
                          itemBuilder: (BuildContext context) =>
                              <PopupMenuEntry<Menu>>[
                                const PopupMenuItem<Menu>(
                                  value: Menu.itemOne,
                                  child: Text('Profile'),
                                ),
                                const PopupMenuItem<Menu>(
                                  value: Menu.itemTwo,
                                  child: Text('Swap Report'),
                                ),
                                const PopupMenuItem<Menu>(
                                  value: Menu.itemThree,
                                  child: Text('Recharge Report'),
                                ),
                                const PopupMenuItem<Menu>(
                                  value: Menu.itemFour,
                                  child: Text('Logout'),
                                ),
                              ]),
                    ),
                  ),
                ),
              ],
            );
            var riderIDTextWidget = SelectableText(
              "Rider ID - ${viewModel.swapModule!.User.PhoneNumber}",
              style: const TextStyle(fontSize: 18, fontWeight: FontWeight.w500),
            );
            var qrCodeWidget = Card(
              elevation: 10,
              child: ColoredContainer(
                containerColor: AppTheme.whiteColor.withOpacity(0.5),
                child: QrImage(
                  data: viewModel.swapModule!.User.PhoneNumber,
                  version: QrVersions.auto,
                  size: 200.0,
                ),
              ),
            );
            var countCardsWidget = Wrap(
              alignment: WrapAlignment.center,
              runAlignment: WrapAlignment.center,
              crossAxisAlignment: WrapCrossAlignment.center,
              runSpacing: 20,
              spacing: 20,
              children: [
                ColoredContainer(
                    child: Row(
                  children: [
                    const Padding(
                      padding: EdgeInsets.all(5),
                      child: Icon(
                        GoZAPPIcon.remaing_swaps,
                      ),
                    ),
                    Expanded(
                      child: Column(
                        children: [
                          const Text("Remaing Swaps"),
                          Text(
                            "${viewModel.swapModule!.RemaingSwap.Available}/${viewModel.swapModule!.RemaingSwap.Total}",
                            style: const TextStyle(
                                fontSize: 18, fontWeight: FontWeight.w700),
                          ),
                        ],
                      ),
                    ),
                  ],
                )),
                ColoredContainer(
                    containerColor: AppTheme.organeColor.withOpacity(0.2),
                    child: Row(
                      children: [
                        const Padding(
                          padding: EdgeInsets.all(5),
                          child: Icon(GoZAPPIcon.free_swaps),
                        ),
                        Expanded(
                          child: Column(
                            children: [
                              const Text("No. free Swaps"),
                              Text(
                                "${viewModel.swapModule!.FreeSwap.Available}/${viewModel.swapModule!.FreeSwap.Total}",
                                style: const TextStyle(
                                    fontSize: 18, fontWeight: FontWeight.w700),
                              ),
                            ],
                          ),
                        ),
                      ],
                    )),
                ColoredContainer(
                    containerColor: AppTheme.pinkColor.withOpacity(0.2),
                    child: Row(
                      children: [
                        const Padding(
                          padding: EdgeInsets.all(5),
                          child: Icon(GoZAPPIcon.month_swaps),
                        ),
                        Expanded(
                          child: Column(
                            children: [
                              const Text("No. Month Swaps"),
                              Text(
                                "${viewModel.swapModule!.SwapHistoryMeta.TotalCount}",
                                style: const TextStyle(
                                    fontSize: 18, fontWeight: FontWeight.w700),
                              ),
                            ],
                          ),
                        ),
                      ],
                    )),
                ColoredContainer(
                    containerColor: AppTheme.blueColor.withOpacity(0.2),
                    child: Row(
                      children: [
                        const Padding(
                          padding: EdgeInsets.all(5),
                          child: Icon(GoZAPPIcon.recharged_swaps),
                        ),
                        Expanded(
                          child: Column(
                            children: [
                              const Text("Recharged Swaps"),
                              Text(
                                "${viewModel.swapModule!.RechargeSwap.Available}/${viewModel.swapModule!.RechargeSwap.Total}",
                                style: const TextStyle(
                                    fontSize: 18, fontWeight: FontWeight.w700),
                              ),
                            ],
                          ),
                        ),
                      ],
                    )),
              ],
            );
            var swapRechargeCountWidget = Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                const Text(
                  "Swaps",
                  style: TextStyle(fontSize: 18, fontWeight: FontWeight.w700),
                ),
                QuantityInput(
                    readOnly: true,
                    value: simpleIntInput,
                    onChanged: (value) => setState(() => simpleIntInput =
                        int.parse(
                            value.replaceAll(',', '').replaceAll('.', '')))),
              ],
            );
            var swapReferTextWidget = const Padding(
              padding: EdgeInsets.all(8.0),
              child: Text("Refer 1 member & get 3 Swaps free"),
            );
            var swapReferAndPayButtonWidget = Row(
              children: [
                Expanded(
                  child: OutlinedButton(
                      onPressed: () {},
                      child: const Text(
                        "Refer",
                        style: TextStyle(
                            fontSize: 16, fontWeight: FontWeight.w500),
                      )),
                ),
                const SizedBox(
                  width: 10,
                ),
                Expanded(
                  child: ElevatedButton(
                      onPressed: () {},
                      child: const Text(
                        "Pay",
                        style: TextStyle(
                            fontSize: 16, fontWeight: FontWeight.w500),
                      )),
                ),
              ],
            );
            return Scaffold(
              body: SafeArea(
                  child: SizedBox(
                width: double.infinity,
                child: SingleChildScrollView(
                  scrollDirection: Axis.vertical,
                  child: Padding(
                    padding: const EdgeInsets.all(15),
                    child: ResponsiveWidget(
                      mobileBody: Column(
                        children: [
                          topBarWidget,
                          const SizedBox(
                            height: 20,
                          ),
                          riderIDTextWidget,
                          qrCodeWidget,
                          const SizedBox(
                            height: 20,
                          ),
                          countCardsWidget,
                          const SizedBox(
                            height: 20,
                          ),
                          swapRechargeCountWidget,
                          swapReferTextWidget,
                          swapReferAndPayButtonWidget
                        ],
                      ),
                      desktopBody: Column(children: [
                        topBarWidget,
                        Row(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: [
                            Expanded(
                                child: Column(
                              crossAxisAlignment: CrossAxisAlignment.start,
                              children: [
                                Padding(
                                  padding: const EdgeInsets.all(10),
                                  child: Text(
                                    "Dashbaord",
                                    style: TextStyle(
                                        fontSize: 24,
                                        color: AppTheme.primaryColor),
                                  ),
                                ),
                                countCardsWidget,
                              ],
                            )),
                            Expanded(
                                child: Column(
                              crossAxisAlignment: CrossAxisAlignment.start,
                              children: [
                                Padding(
                                  padding: const EdgeInsets.all(20),
                                  child: Text(
                                    "Report",
                                    style: TextStyle(
                                        fontSize: 24,
                                        color: AppTheme.primaryColor),
                                  ),
                                ),
                                Column(
                                  children: viewModel.swapModule!
                                      .SwapHistoryMeta.SwapHistoryList
                                      .map(
                                    (History) {
                                      var date = DateFormat('MMM dd, yyyy')
                                          .format(History.SwapCreatedAt);
                                      return ListTile(
                                          leading: CircleAvatar(
                                            radius: 5,
                                            backgroundColor:
                                                (History.IsFreeSwap)
                                                    ? null
                                                    : AppTheme.redColor,
                                          ),
                                          title: Text(History.SwapFormattedCreatedAt));
                                    },
                                  ).toList(),
                                ),
                              ],
                            )),
                            Expanded(
                                child: Column(
                              children: [
                                Padding(
                                  padding: const EdgeInsets.all(10),
                                  child: SelectableText(
                                    "Rider ID - ${viewModel.swapModule!.User.PhoneNumber}",
                                    style: TextStyle(
                                        fontSize: 24,
                                        color: AppTheme.primaryColor),
                                  ),
                                ),
                                qrCodeWidget,
                                const SizedBox(
                                  height: 20,
                                ),
                                // riderIDTextWidget,
                                // const SizedBox(
                                //   height: 20,
                                // ),
                                swapRechargeCountWidget,
                                swapReferTextWidget,
                                swapReferAndPayButtonWidget
                              ],
                            )),
                          ],
                        )
                      ]),
                    ),
                  ),
                ),
              )),
            );
          }
        },
      ),
    );
  }
}
