import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:peasmartcare/blocs/survey_list/survey_list_bloc.dart';
import 'package:peasmartcare/components/pea_app_bar.dart';
import 'package:peasmartcare/components/profile_avatar.dart';
import 'package:peasmartcare/constraints/api_path.dart';
import 'package:peasmartcare/utils/share_data_util.dart';
import 'package:peasmartcare/widgets/sweetalert/sweetalert.dart';
import 'package:webview_flutter/webview_flutter.dart';
import 'package:webview_flutter_plus/webview_flutter_plus.dart';

class SurveyQuestionEdit extends StatefulWidget {
  final String surveyId;
  final String surveyName;
  final String srcqId;
  final String customerId;
  final String fullname;

  const SurveyQuestionEdit({
    Key key,
    @required this.surveyId,
    @required this.surveyName,
    @required this.srcqId,
    @required this.customerId,
    @required this.fullname,
  }) : super(key: key);

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

class _SurveyQuestionEditState extends State<SurveyQuestionEdit> {
  WebViewPlusController _webViewController;
  String urlPath;
  String urlSuccessPath;
  String urlUnsuccessPath;

  @override
  void initState() {
    // context.read<SurveyQuestionEditBloc>().add(SurveyQuestionEditGetList());
    urlPath = "${APIPath.surveyQuestionEditWebView}?id=${widget.surveyId}&customerId=${widget.customerId}&SRCQ_Id=${widget.srcqId}&username=${widget.fullname}&srcqId=${widget.srcqId}&AuthenKey=${ShareDataUtil.getAuthenKey()}";
    urlSuccessPath = APIPath.surveyQuestionSendSuccessWebView;
    urlUnsuccessPath = APIPath.surveyQuestionSendUnsuccessWebView;
    print(urlPath);
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: PEAAppBar(
        title: "แก้ไขแบบสำรวจข้อมูล",
        actions: <Widget>[
          ProfileAvatar(),
        ],
      ),
      body: _buildItem(),
    );
  }

  Widget _buildItem() {
    Size deviceSize = MediaQuery.of(context).size;

    return Container(
      height: deviceSize.height,
      child: WebViewPlus(
        javascriptMode: JavascriptMode.unrestricted,
        onWebViewCreated: (controller) {
          this._webViewController = controller;
          controller.loadUrl(Uri.encodeFull(urlPath));
        },
        onPageFinished: (url) {
          _webViewController.getHeight().then((double height) {});

          if (!url.contains(urlPath) || url.contains("$urlSuccessPath/")) {
            if (url.contains(urlSuccessPath)) {
              context.read<SurveyListBloc>().add(SurveyListGetList());
              SweetAlert.show(
                context,
                subtitle: "ส่งแบบสำรวจสำเร็จ",
                style: SweetAlertStyle.success,
                showCancelButton: false,
                onPress: (bool isConfirm) {
                  if (isConfirm) {
                    Navigator.pop(context);
                    Navigator.pop(context);
                    return false;
                  }
                },
              );
            } else if (url.contains(urlUnsuccessPath) || url.contains("$urlUnsuccessPath/")) {
              SweetAlert.show(
                context,
                subtitle: "ส่งแบบสำรวจไม่สำเร็จ",
                style: SweetAlertStyle.error,
                showCancelButton: false,
                onPress: (bool isConfirm) {
                  if (isConfirm) {
                    Navigator.pop(context);
                    Navigator.pop(context);
                    return false;
                  }
                },
              );
            }
          }
        },
      ),
    );
  }
}
