import 'dart:ui';

import 'package:camera/camera.dart';
import 'package:diplomaticquarterapp/config/config.dart';
import 'package:diplomaticquarterapp/models/LiveCare/IncomingCallData.dart';
import 'package:diplomaticquarterapp/models/LiveCare/room_model.dart';
import 'package:diplomaticquarterapp/pages/conference/web_rtc/call_home_page_.dart';
import 'package:diplomaticquarterapp/pages/conference/widgets/platform_exception_alert_dialog.dart';
import 'package:diplomaticquarterapp/pages/landing/landing_page.dart';
import 'package:diplomaticquarterapp/pages/livecare/video-call-web-page.dart';
import 'package:diplomaticquarterapp/pages/webRTC/OpenTok/OpenTok.dart';
import 'package:diplomaticquarterapp/pages/webRTC/signaling.dart';
import 'package:diplomaticquarterapp/services/livecare_services/livecare_provider.dart';
import 'package:diplomaticquarterapp/uitl/translations_delegate_base.dart';
import 'package:diplomaticquarterapp/uitl/utils_new.dart';
import 'package:diplomaticquarterapp/widgets/others/app_scaffold_widget.dart';
import 'package:flutter/material.dart';
import 'package:just_audio/just_audio.dart';


class IncomingCall extends StatefulWidget {
  IncomingCallData incomingCallData;

  IncomingCall({@required this.incomingCallData});

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

class _IncomingCallState extends State<IncomingCall> with SingleTickerProviderStateMixin {
  AnimationController _animationController;
  bool openCallInWeb = true;

  final player = AudioPlayer();
  CameraController _controller;
  Future<void> _initializeControllerFuture;
  bool isCameraReady = false;
  Signaling signaling = Signaling()..init();

  @override
  void initState() {
    _animationController = AnimationController(vsync: this, duration: Duration(milliseconds: 500));
    isCameraReady = false;

    WidgetsBinding.instance.addPostFrameCallback((_) => _runAnimation());
    //
    // print(widget.incomingCallData.doctorname);
    // print(widget.incomingCallData.clinicname);
    // print(widget.incomingCallData.speciality);

    super.initState();
  }

  @override
  void dispose() {
    _animationController.dispose();
    player.stop();
    _controller.dispose();
    disposeAudioResources();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return AppScaffold(
      isShowAppBar: false,
      isShowDecPage: false,
      body: FutureBuilder<void>(
        future: _initializeControllerFuture,
        builder: (context, snapshot) {
          if (snapshot.connectionState == ConnectionState.done) {
            return Stack(
              alignment: FractionalOffset.center,
              children: <Widget>[
                new Positioned.fill(
                  child: new AspectRatio(aspectRatio: _controller.value.aspectRatio, child: new CameraPreview(_controller)),
                ),
                new Positioned.fill(
                  child: new ClipRect(
                    child: new BackdropFilter(
                      filter: new ImageFilter.blur(sigmaX: 10.0, sigmaY: 10.0),
                      child: new Container(
                        decoration: new BoxDecoration(color: Colors.grey[800].withOpacity(0.8)),
                        child: Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          mainAxisSize: MainAxisSize.max,
                          children: [
                            Container(
                              margin: const EdgeInsets.all(21.0),
                              child: Row(
                                children: [
                                  Image.asset(
                                    "assets/images/new/logo.png",
                                    height: 70,
                                    width: 70,
                                  ),
                                  Container(
                                    margin: const EdgeInsets.only(left: 10.0, right: 10.0),
                                    child: Column(
                                      crossAxisAlignment: CrossAxisAlignment.start,
                                      mainAxisSize: MainAxisSize.min,
                                      mainAxisAlignment: MainAxisAlignment.spaceAround,
                                      children: [
                                        Text(
                                          widget.incomingCallData.doctorname,
                                          style: TextStyle(fontSize: 21, fontWeight: FontWeight.bold, color: Colors.white, letterSpacing: -1.26, height: 23 / 12),
                                        ),
                                        Text(
                                          TranslationBase.of(context).videoAppo,
                                          style: TextStyle(fontSize: 16, fontWeight: FontWeight.w600, color: Color(0xffC6C6C6), letterSpacing: -0.48, height: 23 / 24),
                                        ),
                                        SizedBox(height: 2),
                                      ],
                                    ),
                                  ),
                                ],
                              ),
                            ),
                            Container(
                              margin: const EdgeInsets.all(21.0),
                              width: MediaQuery.of(context).size.width,
                              decoration: cardRadius(15.0, color: Colors.black),
                              child: Column(
                                crossAxisAlignment: CrossAxisAlignment.start,
                                mainAxisSize: MainAxisSize.min,
                                children: [
                                  Container(
                                    padding: const EdgeInsets.fromLTRB(16.0, 16.0, 16.0, 6.0),
                                    child: Text(
                                      TranslationBase.of(context).appoInfo,
                                      style: TextStyle(fontSize: 16, fontWeight: FontWeight.w600, color: Colors.white, letterSpacing: -0.64, height: 23 / 12),
                                    ),
                                  ),
                                  Container(
                                    padding: const EdgeInsets.only(left: 16.0, right: 16.0),
                                    child: Text(
                                      widget.incomingCallData.appointmentdate + ", " + widget.incomingCallData.appointmenttime,
                                      style: TextStyle(fontSize: 12.0, letterSpacing: -0.48, color: Color(0xff8E8E8E), fontWeight: FontWeight.w600),
                                    ),
                                  ),
                                  Container(
                                    padding: const EdgeInsets.only(left: 16.0, right: 16.0, bottom: 21.0),
                                    child: Text(
                                      widget.incomingCallData.clinicname,
                                      style: TextStyle(fontSize: 12.0, letterSpacing: -0.48, color: Color(0xff8E8E8E), fontWeight: FontWeight.w600),
                                    ),
                                  ),
                                ],
                              ),
                            ),
                            Spacer(),
                            Container(
                              margin: EdgeInsets.only(bottom: 70.0, left: 49, right: 49),
                              child: Row(
                                mainAxisSize: MainAxisSize.max,
                                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                children: <Widget>[
                                  RotationTransition(
                                      turns: Tween(begin: 0.0, end: -.1).chain(CurveTween(curve: Curves.elasticIn)).animate(_animationController),
                                      child: Container(
                                        child: RawMaterialButton(
                                          onPressed: () {
                                            _submit();
                                          },
                                          elevation: 2.0,
                                          fillColor: Colors.green,
                                          child: Icon(
                                            Icons.call,
                                            color: Colors.white,
                                            size: 35.0,
                                          ),
                                          padding: EdgeInsets.all(15.0),
                                          shape: CircleBorder(),
                                        ),
                                      )),
                                  Container(
                                    child: RawMaterialButton(
                                      onPressed: () {
                                        backToHome();
                                      },
                                      elevation: 2.0,
                                      fillColor: Colors.red,
                                      child: Icon(
                                        Icons.call_end,
                                        color: Colors.white,
                                        size: 35.0,
                                      ),
                                      padding: EdgeInsets.all(15.0),
                                      shape: CircleBorder(),
                                    ),
                                  ),
                                ],
                              ),
                            ),
                          ],
                        ),
                      ),
                    ),
                  ),
                ),
              ],
            );
          } else {
            return const Center(child: CircularProgressIndicator());
          }
        },
      ),
    );
  }

  void _runAnimation() async {
    final cameras = await availableCameras();
    final firstCamera = cameras[1];

    _controller = CameraController(
      // Get a specific camera from the list of available cameras.
      firstCamera,
      // Define the resolution to use.
      ResolutionPreset.medium,
    );
    _initializeControllerFuture = _controller.initialize();

    setState(() {
      isCameraReady = true;
    });

    setAudioFile();
    for (int i = 0; i < 100; i++) {
      await _animationController.forward();
      await _animationController.reverse();
    }
  }

  Future<void> _submit() async {
    try {
      // backToHome();
      // final roomModel = RoomModel(name: widget.incomingCallData.name, token: widget.incomingCallData.sessionId, identity: widget.incomingCallData.identity);
      await _controller.dispose();
      changeCallStatusAPI(4);
      await Navigator.of(context).pushReplacement(
        MaterialPageRoute(
          // fullscreenDialog: true,
          builder: (BuildContext context) {
            //   return VideoCallWebPage(receiverId: widget.incomingCallData.receiverID, callerId: widget.incomingCallData.callerID); // Web WebRTC VideoCall
            //   return CallHomePage(receiverId: widget.incomingCallData.receiverID, callerId: widget.incomingCallData.callerID); // App WebRTC VideoCall
            // return OpenTokConnectCallPage(
            //     apiKey: OPENTOK_API_KEY,
            //     sessionId: '1_MX40NjIwOTk2Mn5-MTY0NzE3MzcwNjA0MH55RUJoZnd0ZGh2U3BPc01ENVZBelQvT1Z-fg',
            //     token: 'T1==cGFydG5lcl9pZD00NjIwOTk2MiZzaWc9M2I4ODYxYTIzMGQ0ZDA3OTYyNDhkODIxNzI5ZjIzODM1NjY2YzExMzpzZXNzaW9uX2lkPTFfTVg0ME5qSXdPVGsyTW41LU1UWTBOekUzTXpjd05qQTBNSDU1UlVKb1puZDBaR2gyVTNCUGMwMUVOVlpCZWxRdlQxWi1mZyZjcmVhdGVfdGltZT0xNjQ3MTczNzA2Jm5vbmNlPTAuMjA3NDI0MzA1NzcwMzY4MiZyb2xlPW1vZGVyYXRvciZleHBpcmVfdGltZT0xNjQ3MjYwMTA2JmluaXRpYWxfbGF5b3V0X2NsYXNzX2xpc3Q9'
            // );
            return OpenTokConnectCallPage(
                apiKey: OPENTOK_API_KEY,
                sessionId: widget.incomingCallData.sessionId,
                token: widget.incomingCallData.token
            );
          },
        ),
      );
    } catch (err) {
      print(err);
      await PlatformExceptionAlertDialog(
        exception: err,
      ).show(context);
    }
  }

  void changeCallStatusAPI(int sessionStatus) {
    LiveCareService service = new LiveCareService();
    service.endCallAPI(widget.incomingCallData.sessionId, sessionStatus, context).then((res) {
    }).catchError((err) {
      print(err);
    });
  }

  void backToHome() async {
    // final connected = await signaling.declineCall(widget.incomingCallData.callerID, widget.incomingCallData.receiverID);
    LandingPage.isOpenCallPage = false;
    player.stop();
    changeCallStatusAPI(3);
    Navigator.of(context).pop();
  }

  disposeAudioResources() async {
    await player.dispose();
  }

  void setAudioFile() async {
    player.stop();
    await player.setVolume(1.0); // full volume
    try {
      await player.setAsset('assets/sounds/ring_60Sec.mp3').then((value) {
        player.setLoopMode(LoopMode.one); // loop ring sound
        player.play();
      }).catchError((err) {
        print("Error: $err");
      });
    } catch (e) {
      print("Error: $e");
    }
  }
}
