import 'package:fis_ui/index.dart';
import 'package:fis_ui/interface/interactive_container.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flyinsono/architecture/define.dart';
import 'package:flyinsono/pages/main/home/widgets/listener.dart';
import 'package:flyinsono/pages/schedule/calendar/utils/calendar_utils.dart';

import './widgets.dart';
import '../index.dart';

/// 移动端日历-月视图
class MonthView extends FisView<MobileCalendarController>
    implements FInteractiveContainer {
  const MonthView({Key? key}) : super(key: key);

  @override
  String get pageName => "mobile month view";

  MobileCalendarState get state => controller.state;

  @override
  FWidget build(BuildContext context) {
    return FColumn(
      children: [
        _buildWeekRow(),
        FExpanded(
            child: FContainer(
                key: controller.monthViewContainerKey,
                child: _buildDaysContainer(context))),
      ],
    );
  }

  /// 构建星期指示器
  static List<int> weekList = [7, 1, 2, 3, 4, 5, 6];
  FWidget _buildWeekRow() {
    return FContainer(
      padding: EdgeInsets.only(bottom: 6),
      decoration: BoxDecoration(
        border: Border(
          bottom: BorderSide(
            color: Colors.grey,
            width: 0.5,
          ),
        ),
      ),
      child: FRow(
        mainAxisAlignment: MainAxisAlignment.spaceAround,
        children: [
          ...weekList
              .map((e) => FText(
                    CalendarUtils.getSingleWeekMap(e),
                    style: TextStyle(
                      color: Colors.grey,
                      fontSize: 12,
                    ),
                  ))
              .toList(),
        ],
      ),
    );
  }

  /// 构建日期容器
  FWidget _buildDaysContainer(context) {
    return FListener(
      onPointerUp: controller.onMonthViewPagePointerUp,
      child: FNotificationListener(
        onNotification: controller.monthViewPageListener,
        child: FObx(() => FPageView(
              allowImplicitScrolling: true,
              dragStartBehavior: DragStartBehavior.down,
              physics: controller.state.isShowMonthViewDetailPopup
                  ? NeverScrollableScrollPhysics()
                  : null,
              controller: controller.monthViewPageController,
              children: state.monthIndexList.map(_buildEachMonthView).toList(),
              onPageChanged: controller.onMonthViewPageChanged,
            )),
      ),
    );
  }

  /// 构建每一个月视图
  FWidget _buildEachMonthView(int index) {
    return FStack(
      children: [
        FObx(() => MonthViewCalendarPage(
              dayOfMonth: CalendarUtils.getMonthAfter(
                state.currDateTime,
                index,
              ),
            )),
        FObx(() => FOffstage(
              child: FContainer(
                color: Color.fromARGB(210, 245, 245, 242),
                child: FCenter(
                  child: FContainer(
                    margin: EdgeInsets.only(bottom: 10),
                    child: FText(
                      CalendarUtils.formatYearMonth(CalendarUtils.getMonthAfter(
                          state.currDateTime, index)),
                      style: TextStyle(
                        color: Colors.black,
                        fontSize: 22,
                        fontWeight: FontWeight.bold,
                        letterSpacing: 1.3,
                      ),
                    ),
                  ),
                ),
              ),
              offstage: !state.isPageChanging,
            )),
        MonthViewSchedulePopup(),
      ],
    );
  }
}

class CustomPageViewScrollPhysics extends ScrollPhysics {
  const CustomPageViewScrollPhysics({ScrollPhysics? parent})
      : super(parent: parent);

  @override
  CustomPageViewScrollPhysics applyTo(ScrollPhysics? ancestor) {
    return CustomPageViewScrollPhysics(parent: buildParent(ancestor)!);
  }

  @override
  SpringDescription get spring => const SpringDescription(
        mass: 50,
        stiffness: 100,
        damping: 0.8,
      );
}
