import 'dart:async';

import 'package:eitc_erm_dental_flutter/entity/db/local_patient_info.dart';
import 'package:eitc_erm_dental_flutter/exts.dart';
import 'package:eitc_erm_dental_flutter/funcs.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

///咨询人信息条
class VideoPatientInfoBar extends StatefulWidget {
  final LocalPatientInfo? info;
  final Orientation orientation;
  final String area;
  final String deviceModel;
  final DateTime? time;

  const VideoPatientInfoBar(
      {super.key,
      this.info,
      required this.orientation,
      required this.area,
      required this.deviceModel,
      this.time});

  @override
  State<VideoPatientInfoBar> createState() => _VideoPatientInfoBarState();
}

class _VideoPatientInfoBarState extends State<VideoPatientInfoBar> {
  Timer? _timer;

  @override
  void initState() {
    super.initState();
    if (widget.time == null) {
      _startTimer();
    }
  }

  void _startTimer() {
    _timer = Timer.periodic(Duration(seconds: 1), (_) {
      setState(() {});
    });
  }

  @override
  void dispose() {
    super.dispose();
    _timer?.cancel();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      color: Colors.black,
      width: double.infinity,
      height: widget.orientation == Orientation.landscape ? 30.h : null,
      alignment: Alignment.center,
      child: widget.orientation == Orientation.landscape
          ? _getLandscapeView()
          : _getPortraitView(),
    );
  }

  Widget _getLandscapeView() {
    return Row(
      children: [
        _getText(name),
        SizedBox(
          width: 50.w,
        ),
        _getText(toothAreaTranslate(widget.area)),
        SizedBox(
          width: 50.w,
        ),
        _getText(timeStr),
        Spacer(),
        _getText(widget.deviceModel.isEmpty ? "--" : widget.deviceModel)
      ],
    );
  }

  Widget _getPortraitView() {
    return Column(
      mainAxisSize: MainAxisSize.min,
      children: [
        Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [_getText(name), _getText(toothAreaTranslate(widget.area))],
        ),
        Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            _getText(timeStr),
            _getText(widget.deviceModel.isEmpty ? "--" : widget.deviceModel)
          ],
        )
      ],
    );
  }

  Widget _getText(String str) {
    return Text(
      str,
      style: context.bodyMedium?.copyWith(color: Colors.white),
    );
  }

  ///时间
  String get timeStr => (widget.time ?? DateTime.now()).yyyyMMddHHmmss;

  ///名字
  String get name {
    LocalPatientInfo? info = widget.info;
    if (info == null) {
      return "--";
    }
    String idCard = info.idCardDecrypt;
    int sex = getGenderFromIdCard(idCard);
    int age = getAgeFromIdCard(idCard);
    String sexStr = sex < 0 ? "--" : (sex == 0 ? getS().male : getS().female);
    return "${info.name ?? "--"}/$sexStr/${age < 0 ? "--" : age}";
  }
}
