import 'package:fis_common/index.dart';
import 'package:fis_measure/interfaces/process/player/play_controller.dart';
import 'package:fis_vid/processors/index.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:fis_i18n/i18n.dart';
import '../controller.dart';
import '../events.dart';

part 'play_btn.dart';
part 'prev_btn.dart';
part 'next_btn.dart';
part 'progress_bar.dart';
part 'tone_bar.dart';

class VidPlayerControlBoard extends StatefulWidget {
  const VidPlayerControlBoard(this.controller, {Key? key}) : super(key: key);

  final VidPlayerController controller;

  @override
  State<VidPlayerControlBoard> createState() => _VidPlayerControlBoardState();
}

class _VidPlayerControlBoardState extends State<VidPlayerControlBoard> {
  String selectStuta = '';

  @override
  Widget build(BuildContext context) {
    return _SharedWidget(
      controller: widget.controller,
      child: Container(
        padding: const EdgeInsets.symmetric(horizontal: 20, vertical: 10),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            _buildPlayOperateBar(),
            _buildOperateBar(),
            _buildPlayStatusBar(),
          ],
        ),
      ),
    );
  }

  Widget _buildPlayStatusBar() {
    return Center(
      child: Row(
        mainAxisSize: MainAxisSize.min,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          _PlayButton(),
          const SizedBox(width: 12),
          _PrevButton(),
          const SizedBox(width: 2),
          Expanded(child: _ProgressBar()),
          const SizedBox(width: 2),
          _NextButton(),
        ],
      ),
    );
  }

  Widget _buildPlayOperateBar() {
    return Center(
      child: Row(
        mainAxisSize: MainAxisSize.min,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          _buildOperateButton(
              Icons.call_made_outlined, i18nBook.measure.arrow.t),
          if (!FPlatform.isPureWeb) ...[
            _buildOperateButton(Icons.wb_sunny, i18nBook.measure.brightness.t),
            _buildOperateButton(Icons.contrast, i18nBook.measure.contrast.t),
          ],
          _buildOperateButton(Icons.undo_rounded, i18nBook.common.revoke.t),
          _buildOperateButton(
              Icons.cleaning_services_rounded, i18nBook.measure.clear.t),
          _buildOperateButton(Icons.fullscreen, i18nBook.measure.fullScreen.t),
          _buildOperateButton(Icons.save_outlined, i18nBook.common.save.t),
          _buildOperateButton(Icons.share, i18nBook.remedical.share.t, true),
        ],
      ),
    );
  }

  Widget _buildOperateButton(IconData icons, String text,
      [bool isLast = false]) {
    const BorderSide borderSide = BorderSide(
      color: Color.fromRGBO(
        155,
        155,
        155,
        1,
      ),
    );
    return InkWell(
      onTap: () {
        selectStuta = text;
        setState(() {});
      },
      child: Container(
        padding: const EdgeInsets.all(8.0),
        decoration: BoxDecoration(
          color: selectStuta == text ? Colors.blue : Colors.white,
          border: Border(
            top: borderSide,
            bottom: borderSide,
            left: borderSide,
            right: isLast
                ? borderSide
                : const BorderSide(color: Colors.transparent),
          ),
        ),
        child: Column(
          children: [
            if (selectStuta == text) ...[
              Icon(
                icons,
                color: Colors.white,
              ),
              Text(
                text,
                style: const TextStyle(
                  color: Colors.white,
                ),
              ),
            ] else ...[
              Icon(icons),
              Text(text),
            ]
          ],
        ),
      ),
    );
  }

  Widget _buildOperateBar() {
    return Container(
      margin: const EdgeInsets.only(
        top: 15,
      ),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          if (selectStuta == i18nBook.measure.brightness.t)
            _BrightnessToneBar(),
          if (selectStuta == i18nBook.measure.contrast.t) _ContrastToneBar(),
        ],
      ),
    );
  }
}

class _SharedWidget extends InheritedWidget {
  const _SharedWidget({
    required Widget child,
    required this.controller,
  }) : super(child: child);

  final VidPlayerController controller;

  static _SharedWidget? of(BuildContext context) {
    try {
      final data = context.dependOnInheritedWidgetOfExactType<_SharedWidget>();
      return data;
    } catch (e) {
      //
    }
    return null;
  }

  @override
  bool updateShouldNotify(covariant InheritedWidget oldWidget) => false;
}
