import 'package:fis_common/index.dart';
import 'package:fis_i18n/i18n.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/index.dart';
import 'package:get/get.dart';

class CourseCard extends FStatefulWidget implements FPage {
  final Course course;
  CourseCard({required this.course});

  @override
  FState<FStatefulWidget> createState() {
    return FCourseCardState();
  }

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

class FCourseCardState extends FState<CourseCard> {
  Course get course => widget.course;
  final flyinsonoCollegeManager = Get.find<IFlyinsonoCollegeManager>();
  @override
  FWidget build(BuildContext context) {
    ImageProvider<Object> coverImage;
    var cover = course.cover;
    if (cover.isNotNullOrEmpty) {
      coverImage = NetworkImage(cover!);
    } else {
      coverImage = AssetImage('assets/images/logo_en_white.png');
    }
    return FColumn(
      children: [
        FContainer(
          height: 140,
          decoration: BoxDecoration(
            image: DecorationImage(
              image: coverImage,
              fit: BoxFit.cover,
            ),
            borderRadius: BorderRadius.all(Radius.circular(8)),
          ),
          child: FColumn(
            children: [
              if (course.type.isNotNullOrEmpty) ...[
                FContainer(
                  alignment: Alignment.topLeft,
                  child: _buildCourseType(course.type!),
                ),
              ],
              if (course.status != null) ...[
                FContainer(
                  alignment: Alignment.topRight,
                  child: _buildCourseType(flyinsonoCollegeManager
                      .statusTranslation(course.status!)),
                ),
              ],
              FExpanded(child: FContainer()),
            ],
          ),
        ),
        const FSizedBox(height: 5),
        FContainer(
          child: FRow(
            crossAxisAlignment: CrossAxisAlignment.center,
            children: [
              FContainer(
                alignment: Alignment.centerLeft,
                child: FText(course.title ?? ''),
              ),
              FExpanded(child: FSizedBox()),
              if (course.isCanSubscribe) ...[
                FContainer(
                  alignment: Alignment.bottomRight,
                  child: _buildSubscriptionButton(i18nBook.college.subscribe.t,
                      () {
                    router.to(
                      RouteNames.College.LiveCourseDetail,
                      id: NavIds.HOME,
                    );
                  }),
                ),
              ],
            ],
          ),
        ),
      ],
    );
  }

  FWidget _buildCourseType(String text) {
    return FOpacity(
      opacity: 0.9,
      child: FContainer(
        //alignment: Alignment.center,
        padding: EdgeInsets.symmetric(horizontal: 5),
        decoration: BoxDecoration(
          borderRadius: BorderRadius.all(Radius.circular(8)),
          color: Colors.grey,
        ),
        child: FText(
          text,
          style: TextStyle(color: Colors.white),
        ),
      ),
    );
  }

  FWidget _buildSubscriptionButton(String text, void Function()? onPressed) {
    return FElevatedButton(
      style: ButtonStyle(
        fixedSize: MaterialStateProperty.all<Size>(
          Size(i18nBook.isCurrentChinese ? 70.0 : 90, 20.0),
        ),
        padding: MaterialStateProperty.all<EdgeInsetsGeometry>(
          EdgeInsets.symmetric(
            vertical: 5,
          ),
        ),
      ),
      child: FText(text),
      onPressed: onPressed,
      businessParent: widget,
      name: 'SubscriptionButton',
    );
  }
}
