import 'dart:async';

import 'package:flutter/material.dart';

abstract class BasePageScreen extends StatefulWidget {
  const BasePageScreen({Key? key}) : super(key: key);
}

abstract class BasePageScreenState<T extends BasePageScreen> extends State<T> {
  bool _isBack = true;
  bool _isHome = true;
  bool _isShowTitle = true;

  final _loadingController = StreamController<bool>();

  String appBarTitle();
  String appBarSubTitle();

  void onClickBackButton();

  void onClickHome();

  void isBackButton(bool isBack) {
    _isBack = isBack;
  }

  void isHomeButton(bool isHome) {
    _isHome = isHome;
  }

  void isShowTitle(bool isShowTitle) {
    _isShowTitle = isShowTitle;
  }

  void showLoading() {
    _loadingController.add(true);
  }

  void hideLoading() {
    _loadingController.add(false);
  }
}

mixin BaseScreen<T extends BasePageScreen> on BasePageScreenState<T> {
  Widget body();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          // flexibleSpace: Container(
          //   decoration: const BoxDecoration(
          //       image: DecorationImage(
          //           image: AssetImage('assets/images/bg_header.jpeg'),
          //           fit: BoxFit.cover)),
          // ),
          title: Column(
            children: [
              Visibility(
                visible: _isShowTitle,
                child: Text(
                  appBarTitle(),
                  style: const TextStyle(
                    color: Colors.white,
                    fontSize: 16,
                  ),
                ),
              ),
              Text(
                appBarSubTitle(),
                maxLines: _isShowTitle ? 1 : 2,
                style: const TextStyle(
                  color: Colors.white,
                  fontSize: 20,
                  fontWeight: FontWeight.bold,
                ),
              )
            ],
          ),
          leading: _isBack
              ? IconButton(
                  icon: const Icon(
                    Icons.arrow_back_ios,
                    color: Colors.white,
                  ),
                  onPressed: () {
                    // onClickBackButton();
                    Navigator.of(context).pushNamedAndRemoveUntil(
                        "/main", (Route<dynamic> route) => false);
                  },
                )
              : Container(),
          actions: [
            _isHome
                ? IconButton(
                    icon: const Icon(
                      Icons.home,
                      color: Colors.white,
                    ),
                    onPressed: () {
                      onClickHome();
                    },
                  )
                : Container()
          ],
        ),
        body: Stack(
          children: [
            Container(
              height: double.infinity,
              color: Colors.white,
              child: body(),
            ),
            StreamBuilder(
              stream: _loadingController.stream,
              builder: (_, snapshot) {
                return snapshot.data == true
                    ? Positioned.fill(
                        child: _buildLoader(),
                      )
                    : const SizedBox();
              },
            ),
          ],
        ));
  }

  Widget _buildLoader() {
    return Container(
      color: Colors.black54,
      child: const Center(
        child: CircularProgressIndicator(),
      ),
    );
  }

  @override
  void dispose() {
    _loadingController.close();
    super.dispose();
  }
}
