import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:syncrow_app/features/app_layout/bloc/home_cubit.dart';
import 'package:syncrow_app/features/shared_widgets/default_container.dart';
import 'package:syncrow_app/features/shared_widgets/default_scaffold.dart';
import 'package:syncrow_app/features/shared_widgets/text_widgets/body_large.dart';
import 'package:syncrow_app/features/shared_widgets/text_widgets/body_medium.dart';
import 'package:syncrow_app/generated/assets.dart';
import 'package:syncrow_app/utils/context_extension.dart';
import 'package:syncrow_app/utils/helpers/snack_bar.dart';
import 'package:syncrow_app/utils/resource_manager/color_manager.dart';

class JoinHomeView extends StatelessWidget {
  const JoinHomeView({super.key});

  @override
  Widget build(BuildContext context) {
    TextEditingController textEditingController = TextEditingController();
    return DefaultScaffold(
      title: "Join a Home",
      child: Column(
        children: [
          SvgPicture.asset(
            Assets.assetsIconsMenuIconsHomeManagementIconsJoinAHome,
            width: 70,
            height: 70,
          ),
          const Padding(
            padding: EdgeInsets.symmetric(
              vertical: 30,
            ),
            child: BodyMedium(
                textAlign: TextAlign.center,
                text:
                    'Please contact with the\nadministration to get an invitation\n(Menu → Manage your Home → Add Member)'),
          ),
          DefaultContainer(
            padding: const EdgeInsets.symmetric(
              vertical: 5,
            ),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Flexible(
                  child: TextField(
                    controller: textEditingController,
                    decoration: InputDecoration(
                      hintText: 'Invitatoin code',
                      hintStyle:
                          context.bodyMedium.copyWith(color: Colors.grey),
                      border: InputBorder.none,
                    ),
                  ),
                ),
                IconButton(
                  onPressed: () async {
                    if (textEditingController.text.isEmpty) {
                      CustomSnackBar.displaySnackBar(
                          'Please enter the invitation code');
                      return;
                    }
                    if (await HomeCubit.getInstance()
                        .activationCode(textEditingController.text)) {
                      await HomeCubit.getInstance().fetchUnitsByUserId();
                      CustomSnackBar.displaySnackBar('Done successfully');
                      Navigator.of(context).pop();
                    } else {
                      // CustomSnackBar.displaySnackBar('Wrong code!');
                      showDialog(
                        context: context,
                        barrierDismissible: false,
                        builder: (context) {
                          return AlertDialog(
                            contentPadding: EdgeInsets.zero,
                            content: SizedBox(
                              height: MediaQuery.of(context).size.height * 0.2,
                              child: Column(
                                mainAxisSize: MainAxisSize.min,
                                children: <Widget>[
                                  const SizedBox(
                                    height: 10,
                                  ),
                                  const BodyLarge(
                                    text: "Warning",
                                    fontWeight: FontWeight.w700,
                                    fontColor: ColorsManager.red,
                                    fontSize: 16,
                                  ),
                                  const Padding(
                                    padding:
                                        EdgeInsets.only(left: 15, right: 15),
                                    child: Divider(
                                      color: ColorsManager.textGray,
                                    ),
                                  ),
                                  Padding(
                                    padding: const EdgeInsets.only(
                                        left: 15,
                                        right: 20,
                                        top: 15,
                                        bottom: 20),
                                    child: Column(
                                      children: [
                                        Center(
                                            child: Text(
                                          HomeCubit.getInstance().errorMsg,
                                          textAlign: TextAlign.center,
                                        )),
                                      ],
                                    ),
                                  ),
                                  Expanded(
                                    child: Container(
                                      decoration: const BoxDecoration(
                                          border: Border(
                                        top: BorderSide(
                                          color: ColorsManager.textGray,
                                          width: 1.0,
                                        ),
                                      )),
                                      child: InkWell(
                                          onTap: () {
                                            Navigator.of(context).pop();
                                          },
                                          child: Padding(
                                            padding: const EdgeInsets.only(
                                                top: 5, bottom: 5),
                                            child: Center(
                                              child: Text(
                                                'Ok',
                                                style: TextStyle(
                                                    color: ColorsManager
                                                        .switchButton
                                                        .withOpacity(0.6),
                                                    fontSize: 14,
                                                    fontWeight:
                                                        FontWeight.w400),
                                              ),
                                            ),
                                          )),
                                    ),
                                  )
                                ],
                              ),
                            ),
                          );
                        },
                      );
                    }
                  },
                  icon: const Icon(
                    Icons.arrow_right_alt,
                  ),
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }
}
