import 'package:electronic_assistant/base/base_page.dart';
import 'package:electronic_assistant/data/consts/Constants.dart';
import 'package:electronic_assistant/data/consts/event_report_id.dart';
import 'package:electronic_assistant/handler/event_handler.dart';
import 'package:electronic_assistant/module/browser/view.dart';
import 'package:electronic_assistant/module/store/view.dart';
import 'package:electronic_assistant/module/template/template_list/view.dart';
import 'package:electronic_assistant/router/app_pages.dart';
import 'package:electronic_assistant/utils/expand.dart';
import 'package:electronic_assistant/utils/toast_util.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import '../../../data/repositories/account_repository.dart';
import '../../../resource/assets.gen.dart';
import '../../../resource/colors.gen.dart';
import '../../../resource/string.gen.dart';
import '../../chat/start/view.dart';
import 'controller.dart';

class MainDrawerView extends BasePage<MainDrawerController> {
  const MainDrawerView({super.key});

  @override
  Widget buildBody(BuildContext context) {
    return SafeArea(
      child: Padding(
        padding: EdgeInsets.symmetric(horizontal: 16.w),
        child: Column(
          children: [
            SizedBox(height: 12.h),
            buildUserInfoView(),
            SizedBox(height: 12.h),
            GestureDetector(
              onTap: () {
                StorePage.start(fromType: StoreFromType.mine);
              },
              child: 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(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          SizedBox(
                              height: 18.h,
                              child: Assets.images.iconChargeCenterTxt.image()),
                          SizedBox(height: 4.h),
                          Row(
                            children: [
                              Text(StringName.mainLoginChargeTips.tr,
                                  style: TextStyle(
                                      fontSize: 12.sp, color: ColorName.white)),
                              Obx(() {
                                return Text(
                                    '${controller.userInfo?.memberInfo?.electric ?? '0'}',
                                    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),
            _buildContent1(),
            SizedBox(height: 8.h),
            _buildContent2(),
            SizedBox(height: 8.h),
            _buildLogOut(),
            const Spacer(),
            Padding(
              padding: const EdgeInsets.only(bottom: 22).h,
              child: Text(StringName.mainDrawerRecordNumber.tr,
                  style: TextStyle(
                      fontSize: 12.sp, color: ColorName.tertiaryTextColor)),
            )
          ],
        ),
      ),
    );
  }

  GestureDetector _buildLogOut() {
    return GestureDetector(
      onTap: () {
        accountRepository.logout();
        Get.back();
      },
      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(),
          ),
        ),
      ),
    );
  }

  Container _buildContent2() {
    return 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,
            () {
              if (GetPlatform.isIOS) {
                BrowserPage.start(Constants.privacyPolicyIos);
              } else {
                BrowserPage.start(Constants.privacyPolicy);
              }
            },
          ),
          buildOperationItem(
            Assets.images.iconDrawerUserAgreement.image(),
            StringName.userAgreement.tr,
            () {
              BrowserPage.start(Constants.userAgreement);
            },
          ),
          buildOperationItem(
            Assets.images.iconOpinionAndSuggestion.image(),
            StringName.mainDrawerOpinionAndSuggestion.tr,
            () {
              Get.toNamed(RoutePath.complaintOpinion, arguments: "意见建议");
            },
          ),
          buildOperationItem(
            Assets.images.iconComplaintAndReport.image(),
            StringName.mainDrawerComplaintAndReport.tr,
            () {
              Get.toNamed(RoutePath.complaintOpinion, arguments: "投诉举报");
            },
          ),
          buildOperationItem(
            Assets.images.iconDrawerCheckUpdates.image(),
            StringName.mainDrawerCheckUpdates.tr,
            () {
              controller.onCheckUpdate();
            },
            rightView: Text(
              controller.versionName,
              style: TextStyle(
                  fontSize: 12.sp, color: ColorName.tertiaryTextColor),
            ),
          ),
          buildOperationItem(
            Assets.images.iconLogoff.image(),
            StringName.mainDrawerLogoff.tr,
            () {
              controller.accountLogout();
            },
          ),
          buildOperationItem(
            Assets.images.iconDrawerModel.image(),
            StringName.mainDrawerModel.tr,
            () {
              controller.modelClick();
            },
          ),
        ],
      ),
    );
  }

  Container _buildContent1() {
    return 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,
            () {
              TemplateListPage.start();
            },
          ),
          buildOperationItem(
            Assets.images.iconIndustryChange.image(),
            StringName.mainDrawerIndustryPositionChange.tr,
            () {
              showStartSheet();
            },
          )
        ],
      ),
    );
  }

  Widget buildUserInfoView() {
    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: Container(
        padding: EdgeInsets.symmetric(vertical: 12.h, horizontal: 16.w),
        color: Colors.transparent,
        child: Row(
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            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(),
          ],
        ),
      ),
    );
  }

  void showStartSheet() {
    EventHandler.report(EventId.event_102001, params: {
      EventId.id: EventId.id_004,
    });
    Get.bottomSheet(const ChatStartPage(),
        isScrollControlled: true,
        barrierColor: ColorName.black55,
        backgroundColor: ColorName.transparent);
  }

  @override
  Color backgroundColor() {
    return '#F6F5F8'.toColor();
  }
}
