import 'package:fis_i18n/i18n.dart';
import 'package:fis_measure/define.dart';
import 'package:fis_ui/base_define/page.dart';
import 'package:fis_ui/index.dart';
import 'package:flutter/material.dart';
import 'package:flyinsono/architecture/router/index.dart';
import 'package:flyinsono/managers/interfaces/flyinsono_college.dart';
import 'package:flyinsono/pages/college/controller.dart';
import 'package:flyinsono/pages/college/widgets/upload_media_library/upload_media_library_web.dart';
import 'package:get/get.dart';

import 'media_librarys_card.dart';

class MediaLibrary extends FStatefulWidget implements FPage {
  @override
  FState<FStatefulWidget> createState() {
    return _MediaLibraryState();
  }

  @override
  String get pageName => 'MediaLibrary';
}

class _MediaLibraryState extends FState<MediaLibrary> {
  final controller = Get.find<FlyinsonoCollegeController>();

  final flyinsonoCollegeManager = Get.find<IFlyinsonoCollegeManager>();
  final _buttonStyle = ButtonStyle(
    fixedSize: MaterialStateProperty.all(
        Size(i18nBook.isCurrentChinese ? 100 : 120, 25)),
  );

  @override
  void initState() {
    flyinsonoCollegeManager.onUpdateLibrary.addListener(_onUpdateLibrary);
    super.initState();
  }

  @override
  void dispose() {
    flyinsonoCollegeManager.onUpdateLibrary.removeListener(_onUpdateLibrary);
    super.dispose();
  }

  @override
  FWidget build(BuildContext context) {
    FWidget child = FColumn(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        _buildTopOperateButton(),
        _buildMediaLibraryCards(),
      ],
    );
    if (kIsMobile) {
      return FScaffold(
        appBar: FAppBar.simple(
          title: i18nBook.college.myLibrary.t,
          onBackClicked: (() => router.back()),
        ),
        body: child,
      );
    }
    return FExpanded(
      child: FSafeArea(
        child: child,
      ),
    );
  }

  FWidget _rowSpaceing() {
    return const FSizedBox(
      width: 15,
    );
  }

  ///搜索和上传素材按钮
  FWidget _buildTopOperateButton() {
    return FContainer(
      margin: EdgeInsets.only(left: kIsMobile ? 0 : 15),
      child: FRow(
        mainAxisAlignment:
            kIsMobile ? MainAxisAlignment.center : MainAxisAlignment.start,
        children: [
          if (!kIsMobile) ...[
            _rowSpaceing(),
          ],
          FContainer(
            margin: EdgeInsets.symmetric(vertical: 10),
            height: 30,
            width: kIsMobile ? 210 : 230,
            child: FTextField(
              decoration: InputDecoration(
                contentPadding:
                    EdgeInsets.symmetric(vertical: 0, horizontal: 10),
                border: OutlineInputBorder(
                  borderRadius: BorderRadius.circular(8.0), // 设置圆角的半径
                ),
                hintText: i18nBook.common.search.t,
              ),
              controller: controller.searchController,
            ),
          ),
          _rowSpaceing(),
          FElevatedButton(
            child: FText(i18nBook.common.search.t),
            onPressed: () async {
              controller.getMediaLibrarys(
                  keyword: controller.searchController.text);
            },
            style: kIsMobile
                ? ButtonStyle(
                    minimumSize: MaterialStateProperty.all<Size>(Size(50, 30)))
                : _buttonStyle,
            businessParent: widget,
            name: 'search',
          ),
          if (!kIsMobile) ...[
            _rowSpaceing(),
            FElevatedButton(
              style: _buttonStyle,
              child: FText(i18nBook.common.refresh.t),
              onPressed: () {
                controller.getMediaLibrarys();
              },
              businessParent: widget,
              name: 'search',
            ),
            _rowSpaceing(),
            FElevatedButton(
              style: _buttonStyle.copyWith(
                  padding: MaterialStatePropertyAll(
                      EdgeInsets.symmetric(horizontal: 2))),
              child: FText(i18nBook.college.uploadMaterial.t),
              onPressed: () {
                router.dialog(() => UploadMediaLibrary());
              },
              businessParent: widget,
              name: 'search',
            ),
          ],
        ],
      ),
    );
  }

  FWidget _buildMediaLibraryCards() {
    return FExpanded(
      child: FContainer(
        margin: EdgeInsets.only(left: kIsMobile ? 0 : 30),
        alignment: kIsMobile ? Alignment.center : null,
        child: FSingleChildScrollView(
          scrollDirection: Axis.vertical,
          child: FObx(
            () => FWrap(
              spacing: 20,
              runSpacing: 20,
              children: controller.state.mediaLibrarys
                  .map((e) => MediaLibrarysCard(e))
                  .toList(),
            ),
          ),
        ),
      ),
    );
  }

  void _onUpdateLibrary(Object sender, bool e) {
    controller.getMediaLibrarys(keyword: controller.searchController.text);
  }
}
