import 'package:electronic_assistant/data/repositories/account_repository.dart';
import 'package:electronic_assistant/module/main/controller.dart';
import 'package:electronic_assistant/utils/app_info_util.dart';
import 'package:electronic_assistant/utils/expand.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

import '../../resource/assets.gen.dart';
import '../../resource/colors.gen.dart';
import '../../resource/string.gen.dart';
import '../../router/app_pages.dart';

Widget buildDrawerContent(MainController controller, BuildContext context) {
  return SafeArea(
    child: Padding(
      padding: EdgeInsets.symmetric(horizontal: 16.w),
      child: Column(
        children: [
          SizedBox(height: 12.h),
          buildUserInfoView(controller),
          SizedBox(height: 12.h),
          Container(
            height: 72.w,
            decoration: BoxDecoration(
              borderRadius: BorderRadius.circular(12.w),
              border: Border.all(color: ColorName.white20, width: 2.w),
              gradient: LinearGradient(
                colors: ['#5B618D'.toColor(), '#283B58'.toColor()],
                begin: Alignment.centerLeft,
                end: Alignment.centerRight,
                transform: const GradientRotation(-1.64061),
                stops: const [0, 1.0],
              ),
            ),
            child: Row(
              children: [
                SizedBox(width: 16.w),
                IntrinsicHeight(
                  child: Column(
                    children: [
                      SizedBox(
                          height: 18.h,
                          child: Assets.images.iconChargeCenterTxt.image()),
                      SizedBox(height: 4.h),
                      Text(controller.electricityTxt,
                          style: TextStyle(
                              fontSize: 12.sp, color: ColorName.white))
                    ],
                  ),
                ),
                const Spacer(),
                DecoratedBox(
                  decoration: BoxDecoration(
                    color: Colors.white,
                    borderRadius: BorderRadius.circular(12.w),
                  ),
                  child: Padding(
                    padding:
                        const EdgeInsets.symmetric(vertical: 4, horizontal: 11)
                            .w,
                    child: Text(
                      StringName.mainGoCharge.tr,
                      style: TextStyle(
                          fontSize: 14.sp,
                          color: '#374668'.toColor(),
                          fontWeight: FontWeight.bold),
                    ),
                  ),
                ),
                SizedBox(width: 18.w),
              ],
            ),
          ),
          SizedBox(height: 12.h),
          Container(
            padding: EdgeInsets.symmetric(vertical: 8.h),
            decoration: BoxDecoration(
              color: ColorName.white,
              borderRadius: BorderRadius.circular(12.w),
            ),
            child: Column(
              children: [
                buildOperationItem(
                  Assets.images.iconTempManagement.image(),
                  StringName.mainDrawerTemplateManagement.tr,
                  () {},
                ),
                buildOperationItem(
                  Assets.images.iconIndustryChange.image(),
                  StringName.mainDrawerIndustryPositionChange.tr,
                  () {},
                )
              ],
            ),
          ),
          SizedBox(height: 8.h),
          Container(
            padding: EdgeInsets.symmetric(vertical: 8.h),
            decoration: BoxDecoration(
              color: ColorName.white,
              borderRadius: BorderRadius.circular(12.w),
            ),
            child: Column(
              children: [
                buildOperationItem(
                  Assets.images.iconDrawerPrivacyPolicy.image(),
                  StringName.privacyPolicy.tr,
                  () {},
                ),
                buildOperationItem(
                  Assets.images.iconDrawerUserAgreement.image(),
                  StringName.userAgreement.tr,
                  () {},
                ),
                buildOperationItem(
                  Assets.images.iconDrawerCheckUpdates.image(),
                  StringName.mainDrawerCheckUpdates.tr,
                  () {},
                  rightView: Text(
                    controller.versionName,
                    style: TextStyle(
                        fontSize: 12.sp, color: ColorName.tertiaryTextColor),
                  ),
                ),
                buildOperationItem(
                  Assets.images.iconComplaintAndReport.image(),
                  StringName.mainDrawerComplaintAndReport.tr,
                  () {},
                ),
                buildOperationItem(
                  Assets.images.iconLogoff.image(),
                  StringName.mainDrawerLogoff.tr,
                  () {},
                ),
              ],
            ),
          ),
          SizedBox(height: 8.h),
          GestureDetector(
            onTap: () {
              accountRepository.logout();
              Navigator.pop(context);
            },
            child: Container(
              height: 44.h,
              alignment: Alignment.center,
              decoration: BoxDecoration(
                color: ColorName.white,
                borderRadius: BorderRadius.circular(8.w),
              ),
              child: Text(
                StringName.mainDrawerLogout.tr,
                style: TextStyle(
                  color: "#25262A".toColor(),
                ),
              ),
            ),
          ),
          const Spacer(),
          Padding(
            padding: const EdgeInsets.only(bottom: 22).h,
            child: Text(StringName.mainDrawerRecordNumber.tr,
                style: TextStyle(
                    fontSize: 12.sp, color: ColorName.tertiaryTextColor)),
          )
        ],
      ),
    ),
  );
}

Widget buildUserInfoView(MainController controller) {
  return Row(
    crossAxisAlignment: CrossAxisAlignment.center,
    children: [
      SizedBox(
          width: 44.w,
          height: 44.w,
          child: Assets.images.iconHomeLogged.image()),
      SizedBox(width: 10.w),
      Text(controller.loginTxt,
          style: TextStyle(
              fontSize: 20.sp,
              color: ColorName.primaryTextColor,
              fontWeight: FontWeight.bold)),
      SizedBox(width: 8.w),
    ],
  );
}

Widget buildOperationItem(Image icon, String title, Function onTap,
    {Widget? rightView = const SizedBox.shrink()}) {
  return GestureDetector(
    onTap: () {
      onTap();
    },
    child: Padding(
      padding: const EdgeInsets.symmetric(vertical: 12).h,
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          SizedBox(width: 16.w),
          SizedBox(width: 20.w, height: 20.w, child: icon),
          SizedBox(width: 8.w),
          Expanded(
            child: Text(title,
                style: TextStyle(
                    fontSize: 14.sp, color: ColorName.primaryTextColor)),
          ),
          const Spacer(),
          rightView ?? const SizedBox.shrink(),
          SizedBox(width: 16.w),
        ],
      ),
    ),
  );
}
