import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

import '../push/push.dart';
import './ui_model.dart';
import './widgets.dart';

export 'package:flutter/material.dart';

class BaseUIController extends StatefulWidget {
  final BaseUI stateWidget;

  const BaseUIController({super.key, required this.stateWidget});

  @override
  State<BaseUIController> createState() {
    // ignore: no_logic_in_create_state
    return stateWidget;
  }
}

abstract class BaseUI extends State<BaseUIController>
    with AutomaticKeepAliveClientMixin {
  @override
  Widget build(BuildContext context) {
    super.build(context);
    final provider = getProvider(context, listen: true);
    return Stack(
      children: [
        Scaffold(
          backgroundColor: getBackgroundColor(),
          appBar: getAppBar(context),
          body: _buildBody(context),
          bottomNavigationBar: getBottomNavigationBar(context),
          floatingActionButton: getFloatActionButton(context),
        ),
        if (provider.showProgressHUD)
          Material(
            color: Colors.black.withAlpha(50),
            child: Center(
              child: Card(
                child: Container(
                  width: 100,
                  height: 100,
                  color: const Color(0xaa000000),
                  child: const Center(
                    child: SizedBox(
                      width: 40,
                      height: 40,
                      child: CircularProgressIndicator(),
                    ),
                  ),
                ),
              ),
            ),
          ),
      ],
    );
  }

  dismissKeyboard(BuildContext context) {
    FocusScope.of(context).requestFocus(FocusNode());
  }

  Widget? getFloatActionButton(BuildContext context) {
    return null;
  }

  Widget? getBottomNavigationBar(BuildContext context) {
    return null;
  }

  @protected
  BaseUIModel getProvider(BuildContext context, {bool listen = true});

  @protected
  String? getTitleStr(BuildContext context);

  Future pushToPage<T extends BaseUIModel>(
      BuildContext context, BaseUIModel model) {
    return YJPush.pushWidget(
      context,
      getBuild<T>(model),
    );
  }

  Widget getBuild<T extends BaseUIModel>(BaseUIModel model) {
    return ChangeNotifierProvider(
      create: (BuildContext context) {
        return model as T;
      },
      child: BaseUIController(
        stateWidget: this,
      ),
    );
  }

  AppBar? getAppBar(BuildContext context) {
    return makeAppbar(context, getTitleStr(context));
  }

  Color getBackgroundColor() {
    return appBackGroundColor;
  }

  bool hiddenBackBtn() {
    return false;
  }

  Widget _buildBody(BuildContext context) {
    return GestureDetector(
      child: Container(
        color: getBackgroundColor(),
        width: MediaQuery.of(context).size.width,
        height: MediaQuery.of(context).size.height,
        child: buildBody(context),
      ),
      onTap: () {
        dismissKeyboard(context);
      },
    );
  }

  @protected
  Widget buildBody(BuildContext context);

  @override
  bool get wantKeepAlive => false;
}
