import 'package:electronic_assistant/base/base_page.dart';
import 'package:electronic_assistant/data/bean/talks.dart';
import 'package:electronic_assistant/resource/colors.gen.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

import '../../../data/bean/talk_original.dart';
import '../common_view.dart';
import 'controller.dart';

class OriginalView extends BasePage<OriginalController> {
  const OriginalView({super.key});

  @override
  Widget buildBody(BuildContext context) {
    return buildOriginalContentView();
  }

  Widget buildOriginalContentView() {
    return Obx(() {
      if (controller.originalList.isEmpty &&
          controller.talkController.talkBean.value?.status ==
              TalkStatus.analysisFail) {
        return getTalkFailView();
      } else if (controller.originalList.isEmpty) {
        return getTalkLoadingView();
      } else {
        return ListView.builder(
          padding: EdgeInsets.only(bottom: 150.h),
          itemBuilder: _buildOriginalItem,
          itemCount: controller.originalList.length,
        );
      }
    });
  }

  Widget _buildOriginalItem(BuildContext context, int index) {
    TalkOriginal item = controller.originalList[index];
    return Padding(
      padding:
          EdgeInsets.only(left: 12.w, right: 12.w, top: 11.h, bottom: 13.h),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Row(
            children: [
              Container(
                decoration: const BoxDecoration(
                  color: ColorName.colorPrimary,
                  shape: BoxShape.circle,
                ),
                width: 20.w,
                height: 20.w,
                child: Center(
                  child: Text(
                    item.speakerId.toString(),
                    style: TextStyle(fontSize: 12.sp, color: Colors.white),
                  ),
                ),
              ),
              SizedBox(width: 6.w),
              Text(item.speaker.toString(),
                  style: TextStyle(
                      fontSize: 14.sp, color: ColorName.secondaryTextColor)),
              SizedBox(width: 4.w),
              Text(formatMilliseconds(item.startMs),
                  style: TextStyle(
                      fontSize: 12.sp, color: ColorName.tertiaryTextColor)),
            ],
          ),
          SizedBox(height: 12.h),
          Text(item.sentence.toString(),
              style: TextStyle(
                  fontSize: 14.sp, color: ColorName.primaryTextColor)),
        ],
      ),
    );
  }

  String formatMilliseconds(int? totalMilliseconds) {
    if (totalMilliseconds == null) {
      return '';
    }
    int totalSeconds = (totalMilliseconds / 1000).round();
    int hours = totalSeconds ~/ 3600;
    int minutes = (totalSeconds % 3600) ~/ 60;
    int seconds = totalSeconds % 60;

    if (hours > 0) {
      return '${hours.toString().padLeft(2, '0')}:${minutes.toString().padLeft(2, '0')}:${seconds.toString().padLeft(2, '0')}';
    } else {
      return '${minutes.toString().padLeft(2, '0')}:${seconds.toString().padLeft(2, '0')}';
    }
  }
}
