import 'package:flutter/widgets.dart';
import 'package:netrain_flutter_app/douwen/custom/BaseViewModel.dart';
import 'package:provider/provider.dart';

import 'EmptyPage.dart';
import 'ErrorPage.dart';
import 'ViewState.dart';

class BaseView extends StatefulWidget {
  BaseViewModel viewModel;
  Widget Function(BuildContext context, BaseViewModel value, Widget child)
      builder;
  Widget _mReplaceView;
  Widget emptyView = EmptyPage();
  Widget errorView = ErrorPage();

  BaseView(this.viewModel) {
  }


  Widget setReplaceView(Widget replaceView) {
    _mReplaceView = replaceView;
    switch (viewModel.viewState ?? ViewState.Normal) {
      case ViewState.Normal:
        return _mReplaceView;
      case ViewState.Empty:
        return emptyView;
      case ViewState.Error:
        return errorView;
      case ViewState.Loading:
        return _mReplaceView;
      default:
        return _mReplaceView;
    }
  }

  @override
  State<StatefulWidget> createState() {
    return _BaseViewState();
  }
}

class _BaseViewState extends State<BaseView> {
  BaseViewModel viewModel;

  @override
  void initState() {
    viewModel = widget.viewModel;
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider(
      create: (content) => viewModel,
      child: Consumer<BaseViewModel>(
        builder: widget.builder,
      ),
    );
  }
}
