import 'package:custom_pop_up_menu/custom_pop_up_menu.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:trans_lark/generated/assets.dart';
import 'package:trans_lark/global/global_config.dart';
import 'package:trans_lark/page/translator/translator_controller.dart';
import 'package:trans_lark/util/t_object_utils.dart';
import 'package:trans_lark/widget/t_base_appbar_widget.dart';
import 'package:trans_lark/widget/t_language_bar_widget.dart';

class TranslatorView extends GetView<TranslatorController> {
  const TranslatorView({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      appBar: const TBaseAppbarWidget(
        titleWidget: TLanguageBarWidget(),
      ),
      body: Padding(
        padding: const EdgeInsets.symmetric(horizontal: 20),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            const SizedBox(height: 20),
            Container(
              decoration: const BoxDecoration(
                color: GlobalConfig.appColor,
                borderRadius: BorderRadius.all(Radius.circular(16)),
              ),
              child: Row(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Expanded(
                    child: TextField(
                      textInputAction: TextInputAction.done,
                      textAlignVertical: TextAlignVertical.center,
                      textAlign: TextAlign.left,
                      controller: controller.textController,
                      maxLines: null,
                      minLines: 1,
                      autofocus: true,
                      cursorColor: Colors.white,
                      style: const TextStyle(
                        color: Colors.black,
                        fontSize: 20,
                        height: 1.2,
                      ),
                      decoration: const InputDecoration(
                        counterText: '',
                        isCollapsed: true,
                        contentPadding: EdgeInsets.fromLTRB(20, 26, 4, 26),
                        border: InputBorder.none,
                        hintText: "Type to Translate",
                        hintStyle: TextStyle(
                          color: Colors.white,
                          fontSize: 20,
                        ),
                      ),
                      onChanged: (value) => controller.onTextChanged(value),
                      onSubmitted: (value) => controller.onTextSubmit(value),
                    ),
                  ),
                  Obx(() {
                    return Padding(
                      padding: EdgeInsets.only(
                          top: controller.isValue.value ? 26 : 20, right: 20),
                      child: GestureDetector(
                        onTap: controller.onSpeakTranslation,
                        child: ClipOval(
                          child: Visibility(
                            visible: controller.isValue.value,
                            replacement: Container(
                              width: 36,
                              height: 36,
                              color: Colors.white,
                              child: FittedBox(
                                fit: BoxFit.none,
                                child: Image.asset(
                                  Assets.imagesHomeMicrophone,
                                  width: 24,
                                  height: 24,
                                ),
                              ),
                            ),
                            child: Container(
                              width: 24,
                              height: 24,
                              color: Colors.white,
                              child: const Icon(Icons.close, size: 12),
                            ),
                          ),
                        ),
                      ),
                    );
                  }),
                ],
              ),
            ),
            Obx(
              () => controller.historyList.isNotEmpty
                  ? Expanded(
                      child: Column(
                        children: [
                          const SizedBox(height: 30),
                          Row(
                            mainAxisAlignment: MainAxisAlignment.spaceBetween,
                            children: [
                              const Text(
                                "History",
                                style: TextStyle(
                                  fontSize: 16,
                                  color: Color(0xff979797),
                                  fontWeight: FontWeight.w500,
                                ),
                              ),
                              IconButton(
                                onPressed: controller.dropHistory,
                                icon: Image.asset(
                                  Assets.imagesDeleteIcon,
                                  width: 24,
                                  height: 24,
                                ),
                              ),
                            ],
                          ),
                          Expanded(
                            child: ListView.builder(
                              itemCount: controller.historyList.length,
                              itemBuilder: (context, index) {
                                final historyEntity =
                                    controller.historyList[index];
                                return CustomPopupMenu(
                                  arrowColor: Colors.black,
                                  barrierColor: Colors.transparent,
                                  position: PreferredPosition.top,
                                  verticalMargin: 0,
                                  pressType: PressType.longPress,
                                  menuBuilder: () {
                                    return GestureDetector(
                                      onTap: () => controller
                                          .deleteHistoryByIndex(index),
                                      child: Container(
                                        padding: const EdgeInsets.all(10),
                                        decoration: BoxDecoration(
                                          color: Colors.black,
                                          borderRadius:
                                              BorderRadius.circular(8),
                                        ),
                                        child: Image.asset(
                                          Assets.imagesDeleteIcon,
                                          width: 24,
                                          height: 24,
                                        ),
                                      ),
                                    );
                                  },
                                  child: InkWell(
                                    onTap: () =>
                                        controller.toHistoryItem(historyEntity),
                                    child: Column(
                                      crossAxisAlignment:
                                          CrossAxisAlignment.start,
                                      children: [
                                        Padding(
                                          padding: const EdgeInsets.symmetric(
                                              vertical: 10),
                                          child: RichText(
                                            maxLines: 1,
                                            overflow: TextOverflow.ellipsis,
                                            text: TextSpan(
                                              children: [
                                                TextSpan(
                                                  text: TObjectUtils.getStr(
                                                      historyEntity.sourceText),
                                                  style: const TextStyle(
                                                    fontWeight: FontWeight.w500,
                                                    fontSize: 16,
                                                    color: Color(0xff212121),
                                                  ),
                                                ),
                                                const WidgetSpan(
                                                  child: SizedBox(
                                                    width: 6,
                                                  ),
                                                ),
                                                TextSpan(
                                                  text: TObjectUtils.getStr(
                                                      historyEntity.targetText),
                                                  style: const TextStyle(
                                                    color: Color(0xff979797),
                                                    fontSize: 13,
                                                    fontWeight: FontWeight.w400,
                                                  ),
                                                ),
                                              ],
                                            ),
                                          ),
                                        ),
                                      ],
                                    ),
                                  ),
                                );
                              },
                            ),
                          ),
                        ],
                      ),
                    )
                  : Expanded(child: Container()),
            ),
          ],
        ),
      ),
    );
  }
}
