/*
 * @Author: WuXiangNan
 * @Date: 2021-03-01 14:55:40
 * @LastEditTime: 2021-03-26 15:26:09
 * @LastEditors: WuXiangNan
 * @Description: 日历组件
 */
// part of '../page/rule/index.dart';

import 'package:attendance_package/src/view_models/attendance_rule_store.dart';
import 'package:flutter/material.dart';
import 'package:flutter_calendar_carousel/flutter_calendar_carousel.dart';
import 'package:flutter_calendar_carousel/classes/event.dart';
import 'package:intl/intl.dart' show DateFormat;
import 'package:wisdom_cli/wisdom_cli.dart';
import '../assets.gen.dart';

class CalendarPage extends StatelessWidget {
  const CalendarPage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider(
      create: (_) => RuleStore(context),
      child: Calendar(),
      lazy: false,
    );
  }
}

class Calendar extends StatefulWidget {
  Calendar({Key? key}) : super(key: key);

  @override
  _CalendarState createState() => _CalendarState();
}

class _CalendarState extends State<Calendar> {
  RuleStore get store => Provider.of<RuleStore>(context, listen: false);

  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addPostFrameCallback((timeStamp) async {
      await store
          .onGetScheduleCalendar(DateFormat('yyyy-MM').format(DateTime.now()));
    });
  }

  CalendarCarousel? calendarCarouselNoHeader;

  @override
  Widget build(BuildContext context) {
    final store = Provider.of<RuleStore>(context);
    initCalendar();
    return Wisdom(
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.center,
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          Wisdom.row(
            color: Colors.white,
            padding: EdgeInsets.symmetric(vertical: 15.pt),
            mainAxisAlignment: MainAxisAlignment.spaceAround,
            children: [
              Wisdom(
                child: Image(
                  image: AssetList.$xz_png_image,
                  width: 13.pt,
                  height: 12.pt,
                ),
                onTap: () => store.preMonth(),
              ),
              WisText(
                store.currentMonth,
                size: 15.pt,
              ),
              Wisdom(
                child: Image(
                  image: AssetList.$xy_png_image,
                  width: 13.pt,
                  height: 12.pt,
                ),
                onTap: () => store.nextMonth(),
              ),
            ],
          ),
          Divider(
            height: 0.5,
            color: Color(0xFFE6E6E6),
          ),
          Container(
            color: Colors.white,
            child: calendarCarouselNoHeader,
          ),
          Divider(
            height: 0.5,
            color: Color(0xFFE6E6E6),
          ),
          SingleChildScrollView(
            child: Wisdom.row(
              padding: EdgeInsets.all(15.pt),
              children: [
                WisText(
                  store.selectedDay?.info ?? '当日暂无排班',
                  size: 14.pt,
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }

  void initCalendar() {
    final colorScheme = WTheme.of(context).colorScheme;
    final store = Provider.of<RuleStore>(context);

    calendarCarouselNoHeader = CalendarCarousel<Event>(
      todayBorderColor: Colors.white,
      onDayPressed: (DateTime date, List<Event> events) =>
          store.calendarOnPressed(date, events),
      customWeekDayBuilder: (weekday, weekdayName) {
        @override
        Widget build(BuildContext context) {
          switch (weekday) {
            case 1:
              weekdayName = '一';
              break;
            case 2:
              weekdayName = '二';
              break;
            case 3:
              weekdayName = '三';
              break;
            case 4:
              weekdayName = '四';
              break;
            case 5:
              weekdayName = '五';
              break;
            case 6:
              weekdayName = '六';
              break;
            case 0:
              weekdayName = '日';
              break;
          }
          return Wisdom(
            padding: EdgeInsets.all(15.pt),
            child: WisText(
              weekdayName,
              color: Color(0xFF666666),
            ),
          );
        }

        return build(context);
      },
      daysTextStyle: TextStyle(
        fontSize: 14.pt,
        // fontWeight: FontWeight.w300,
        color: Colors.black,
      ),
      weekendTextStyle: TextStyle(
        fontSize: 14.pt,
        // fontWeight: FontWeight.bold,
        color: Color(0xFF666666),
      ),
      // 圆形边框
      daysHaveCircularBorder: true,
      showOnlyCurrentMonthDate: false,
      weekFormat: false,
      markedDatesMap: store.calendarEvent,
      height: 400.0,
      selectedDateTime: store.currentDate2,
      targetDateTime: store.targetDateTime,
      customGridViewPhysics: NeverScrollableScrollPhysics(),
      markedDateCustomTextStyle: TextStyle(
        fontSize: 18,
        color: Colors.white,
      ),
      markedDateMoreCustomTextStyle: TextStyle(
        fontSize: 18,
        color: Colors.white,
      ),
      showHeader: false,
      showWeekDays: true,
      todayTextStyle: TextStyle(
          color: colorScheme.primary,
          fontWeight: FontWeight.bold,
          fontSize: 14.pt),
      todayButtonColor: Colors.white,
      selectedDayTextStyle: TextStyle(
        color: Colors.white,
        fontSize: 15.pt,
        // fontFamily: 'HiraginoSansGB-W3',
      ),
      selectedDayButtonColor: colorScheme.primary,
      selectedDayBorderColor: colorScheme.primary,
      minSelectedDate: store.currentDate.subtract(Duration(days: 360)),
      maxSelectedDate: store.currentDate.add(Duration(days: 360)),
      prevDaysTextStyle: TextStyle(
        fontSize: 14.pt,
        color: Colors.grey,
      ),
      nextDaysTextStyle: TextStyle(
        fontSize: 14.pt,
        color: Colors.grey,
      ),
      inactiveDaysTextStyle: TextStyle(
        color: Colors.tealAccent,
        fontSize: 16,
      ),
      onCalendarChanged: (DateTime date) => store.calendarOnChange(date),
    );
  }
}
