import 'dart:ui';
import 'dart:convert';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';
import 'package:mohem_flutter_app/api/profile_api_client.dart';
import 'package:mohem_flutter_app/app_state/app_state.dart';
import 'package:mohem_flutter_app/classes/utils.dart';
import 'package:mohem_flutter_app/models/get_employee_basic_details.model.dart';
import 'package:mohem_flutter_app/models/member_information_list_model.dart';
import 'package:mohem_flutter_app/ui/screens/profile/widgets/header.dart';
import 'package:mohem_flutter_app/ui/screens/profile/widgets/profile_panel.dart';
import 'package:mohem_flutter_app/widgets/bottom_sheet.dart';

class ProfileScreen extends StatefulWidget {
  const ProfileScreen({Key? key}) : super(key: key);

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

class _ProfileScreenState extends State<ProfileScreen> {
  late MemberInformationListModel memberInformationList;
  final ImagePicker _picker = ImagePicker();
  List<GetEmployeeBasicDetailsList> getEmployeeBasicDetailsList = [];

  @override
  void initState() {
    super.initState();
    memberInformationList = AppState().memberInformationList!;
    //getEmployeeBasicDetails();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        extendBody: true,
        backgroundColor: const Color(0xffefefef),
        body: Stack(children: [
          Container(
              height: 300,
              margin: EdgeInsets.only(top: 50),
              decoration: BoxDecoration(image: DecorationImage(image: MemoryImage(Utils.getPostBytes(memberInformationList.eMPLOYEEIMAGE)), fit: BoxFit.cover)),
              child: new BackdropFilter(
                filter: new ImageFilter.blur(sigmaX: 10.0, sigmaY: 10.0),
                child: new Container(
                  decoration: new BoxDecoration(color: Colors.white.withOpacity(0.0)),
                ),
              )),
          SingleChildScrollView(
            scrollDirection: Axis.vertical,
            child: Column(crossAxisAlignment: CrossAxisAlignment.center, children: [
              SizedBox(
                height: 80,
              ),
              Container(
                  padding: EdgeInsets.only(left: 15, right: 15),
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      IconButton(
                          onPressed: () {
                            Navigator.pop(context);
                          },
                          icon: Icon(
                            Icons.arrow_back_ios,
                            color: Colors.white,
                          )),
                      InkWell(
                          onTap: () {
                            startImageSheet();
                          },
                          child: Container(
                              padding: EdgeInsets.only(left: 10, right: 10, top: 5, bottom: 5),
                              decoration: BoxDecoration(borderRadius: BorderRadius.circular(15), color: Colors.black.withOpacity(.3)),
                              child: Row(children: [
                                Icon(Icons.photo, color: Colors.white),
                                Text(
                                  'Edit',
                                  style: TextStyle(color: Colors.white, fontSize: 12),
                                )
                              ]))),
                    ],
                  )),
              HeaderPanel(memberInformationList),
              ProfilePanle(memberInformationList)
            ]),
          )
        ]));
  }

  startImageSheet() {
    showMyBottomSheet(context,
        child: Column(
          children: [
            Container(
                padding: EdgeInsets.only(left: 20, right: 20),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [Text('OK'), Text('CANCEL')],
                )),
            BottomSheetItem(
              onTap: () {
                openGallery(false);
              },
              title: 'Open Gallery',
              icon: Icons.browse_gallery_outlined,
            ),
            BottomSheetItem(
              onTap: () {
                openGallery(true);
              },
              title: 'Open Camera',
              icon: Icons.camera,
            )
          ],
        ));
  }

  void openGallery(bool isCamera) async {
    final XFile? image = await _picker.pickImage(source: isCamera ? ImageSource.camera : ImageSource.gallery);

    if (image != null) {
      String img = base64.encode(await image!.readAsBytes());
      Utils.showLoading(context);
      dynamic empImageUpdteResp = await ProfileApiClient().updateEmpImage(img);
      Utils.hideLoading(context);
      if (empImageUpdteResp['P_RETURN_STATUS'] == 'S') {
        setState(() {
          memberInformationList.eMPLOYEEIMAGE = img;
        });
      }
    }
  }
}
