import 'dart:io';
import 'package:e_commerce_training/res/R.dart';
import 'package:e_commerce_training/src/util/util.dart';
import 'package:e_commerce_training/src/widget/custom_appbar.dart';
import 'package:e_commerce_training/src/widget/pending_action.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_webview_plugin/flutter_webview_plugin.dart';

class WebviewPage extends StatelessWidget {
  final String title;
  final String? url;
  final String? html;

  const WebviewPage({
    Key? key,
    required this.title,
    this.url,
    this.html,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    Util.setStatusColor(R.color.blue);
    return AnnotatedRegion<SystemUiOverlayStyle>(
      value: SystemUiOverlayStyle(
        statusBarColor: R.color.primaryColor,
      ),
      child: Scaffold(
          backgroundColor: R.color.primaryColor,
          body: SafeArea(
            top: true,
            bottom: false,
            child: Container(
                color: R.color.primaryColor,
                height: double.infinity,
                child: Column(children: [appBar(context, title), Expanded(child: contentWidget())])),
          )),
    );
  }

  Widget contentWidget() {
    String? newUrl;
    if (!Util.isEmpty(url)) {
      newUrl = url!;
      if (newUrl.toLowerCase().contains("pdf")) {
        if (Platform.isAndroid) {
          newUrl = "http://drive.google.com/viewerng/viewer?embedded=true&url=$url";
        }
      }
    }
    if (!Util.isEmpty(html)) {
      newUrl = Uri.dataFromString('<html><body>hello world</body></html>', mimeType: 'text/html').toString();
    }
    if (Util.isEmpty(newUrl)) {
      return Container(
        color: R.color.grey,
      );
    } else {
      return WebviewScaffold(
        url: newUrl!,
        // appBar: appBar(context, title),
        withZoom: true,
        withLocalStorage: true,
        hidden: true,
        initialChild: PendingAction(),
      );
    }
  }
}
