// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// ignore_for_file: public_member_api_docs

import 'dart:async';
import 'package:flutter/material.dart';
import 'package:sport/widgets/menu_share_bottom.dart';
import 'package:webview_flutter/webview_flutter.dart';

class WebViewSharePage extends StatefulWidget {
  final String url;
  WebViewSharePage(this.url);
  @override
  _WebViewSharePageState createState() => _WebViewSharePageState();
}

class _WebViewSharePageState extends State<WebViewSharePage> {
  final Completer<WebViewController> _controller =
      Completer<WebViewController>();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        // We're using a Builder here so we have a context that is below the Scaffold
        // to allow calling Scaffold.of(context) so we can show a snackbar.
        body: SafeArea(
      child: Stack(
        children: <Widget>[
          Container(
            child: SingleChildScrollView(
              child: Container(
                height: MediaQuery.of(context).size.height * 1,
                child: WebView(
                  initialUrl: 'https://www.baidu.com',
                  javascriptMode: JavascriptMode.unrestricted,
                  onWebViewCreated: (WebViewController webViewController) {
                    _controller.complete(webViewController);
                  },
                  // ignore: prefer_collection_literals
                  javascriptChannels: <JavascriptChannel>[
                    _toasterJavascriptChannel(context),
                  ].toSet(),
                  onPageStarted: (String url) {
                    print('Page started loading: $url');
                  },
                  onPageFinished: (String url) {
                    print('Page finished loading: $url');
                  },
                  gestureNavigationEnabled: true,
                ),
              ),
              // 要不要取消回弹...
//              physics: new ClampingScrollPhysics(),
            ),
          ),
          Positioned(
            left: 12.0,
            top: 11.0,
            child: Container(
              child: InkWell(
                child: Image.asset("lib/assets/img/topbar_return_white.png"),
                onTap: () {
                  Navigator.pop(context);
                },
              ),
//                color: Colors.red,
            ),
          ),
          Positioned(
            right: 12.0,
            top: 11.0,
            child: Container(
              child: InkWell(
                child: Image.asset("lib/assets/img/bbs_icon_share_white.png"),
                onTap: () {
                  menuShareBottom(context, "Link",
                      url: "http://106.52.104.134:4000/", hasDownload: false);
                },
              ),
//                color: Colors.red,
            ),
          ),
        ],
      ),
    )
//      floatingActionButton: favoriteButton(),
        );
  }

  JavascriptChannel _toasterJavascriptChannel(BuildContext context) {
    return JavascriptChannel(
        name: 'Toaster',
        onMessageReceived: (JavascriptMessage message) {
          Scaffold.of(context).showSnackBar(
            SnackBar(content: Text(message.message)),
          );
        });
  }

  Widget favoriteButton() {
    return FutureBuilder<WebViewController>(
        future: _controller.future,
        builder: (BuildContext context,
            AsyncSnapshot<WebViewController> controller) {
          if (controller.hasData) {
            return FloatingActionButton(
              onPressed: () async {
                final String url = await controller.data.currentUrl();
                Scaffold.of(context).showSnackBar(
                  SnackBar(content: Text('Favorited $url')),
                );
              },
              child: const Icon(Icons.favorite),
            );
          }
          return Container();
        });
  }
}
