import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_webview_pro/webview_flutter.dart';

import '../base/YSNetWorking.dart';
import '../base/YSTools.dart';

class YSUrlPage extends StatefulWidget {
  final String url;
  final String title;
  final Map wiki;
  final bool isCollection;
  const YSUrlPage({Key key, this.url, this.title = '', this.wiki, this.isCollection}) : super(key: key);

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

class _YSUrlPageState extends State<YSUrlPage> {
  bool _collection = false;
  WebViewController _webViewController;
  @override
  void initState() {
    if(widget.isCollection!=null){
      _collection = widget.isCollection;
    }
    super.initState();
  }
  @override
  Widget build(BuildContext context) {
    return WillPopScope(
      onWillPop: () async{
        bool canBack = await _webViewController.canGoBack();
        if(canBack){
          _webViewController.goBack();
        }else{
          Navigator.pop(context);
        }
        return false;
      },
      child: Scaffold(
        backgroundColor: Colors.white,
        body: Stack(
          children: [
            Container(
              height: MediaQuery.of(context).padding.top+70,
              width: MediaQuery.of(context).size.width,
              color: Color(0xFFDB5278),
            ),
            Container(
              margin: EdgeInsets.only(top: 20),
              padding: EdgeInsets.only(left: 15,right: 15),
              height: 30+MediaQuery.of(context).padding.top,
              width: MediaQuery.of(context).size.width,
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  Container(
                    height: 30,
                    width: 20,
                    margin: EdgeInsets.only(right: 5),
                    child: CupertinoButton(
                      padding: EdgeInsets.all(0),
                      child: Icon(Icons.arrow_back_ios,size: 20,color: Colors.white,),
                      onPressed: () async{
                        bool canBack = await _webViewController.canGoBack();
                        if(canBack){
                          _webViewController.goBack();
                        }else{
                          Navigator.pop(context);
                        }
                      },
                    ),
                  ),
                  // Container(
                  //   height: 30,
                  //   width: 20,
                  //   child: CupertinoButton(
                  //     padding: EdgeInsets.all(0),
                  //     child: Icon(Icons.close,size: 20,color: Colors.white,),
                  //     onPressed: (){
                  //       Navigator.pop(context);
                  //     },
                  //   ),
                  // ),
                  Container(
                    width: ysWidth(context)-150,
                    child: Text(widget.title,style: TextStyle(color: Colors.white,fontSize: 16,decoration: TextDecoration.none,fontWeight: FontWeight.w600),
                      textAlign: TextAlign.center,maxLines: 1,),
                  ),
                  Container(
                    width: 55,
                    height: 30,
                    alignment: Alignment.centerRight,
                    child: widget.isCollection!=null?GestureDetector(
                      child: Image(height: 15,width: 15,image: AssetImage(_collection==false?'lib/images/uncollection.png':'lib/images/collection.png'),),
                      onTap: (){
                        _postCollectionData();
                      },
                    ):Container(),
                  ),
                ],
              ),
            ),
            Container(
              margin: EdgeInsets.only(top: 50+MediaQuery.of(context).padding.top),
              height: MediaQuery.of(context).size.height-MediaQuery.of(context).padding.top-50,
              width: MediaQuery.of(context).size.width,
              decoration: BoxDecoration(
                  color: Color(0xFFFAF8F5),
                  borderRadius: BorderRadius.only(topLeft: Radius.circular(20),topRight: Radius.circular(20))
              ),
              child: Container(
                  width: MediaQuery.of(context).size.width,
                  height: MediaQuery.of(context).size.height,
                  child: WebView(
                  initialUrl: widget.url,
                  javascriptMode: JavascriptMode.unrestricted,
                    onWebViewCreated: (value){
                    _webViewController = value;
                    },
                ),
              ),
            )
          ],
        ),
      ),
    );
  }

  _postCollectionData() async{
    Map dict = await ysRequestHttpNoLoading(context, requestType.put, 'wike/favorite', {'wike_id':widget.wiki['id']});
    if(dict!=null){
      _collection = dict['data']['is_favorite'];
      setState(() {});
    }
  }

}
