import 'dart:io';

import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:open_file/open_file.dart';
import 'package:test_sa/extensions/widget_extensions.dart';
import 'package:test_sa/views/app_style/colors.dart';
import 'package:test_sa/views/app_style/sizing.dart';
import 'package:test_sa/views/widgets/buttons/app_back_button.dart';
import 'package:test_sa/views/widgets/loaders/image_loader.dart';
import 'package:url_launcher/url_launcher.dart';

class MultiFilesPickerItem extends StatelessWidget {
  final File file;
  final bool enabled;
  final Function(File) onRemoveTap;

  const MultiFilesPickerItem({Key key, this.file, this.enabled, this.onRemoveTap}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    var isImage = file.path.split(".").last.toLowerCase() == "png" || file.path.split(".").last.toLowerCase() == "jpg" || file.path.split(".").last.toLowerCase() == "jpeg";
    var isPdf = file.path.split(".").last.toLowerCase() == "pdf";
    var isExcel = file.path.split(".").last.toLowerCase() == "xlsx";
    return Container(
      width: 80 * AppStyle.getScaleFactor(context),
      height: 80 * AppStyle.getScaleFactor(context),
      decoration: BoxDecoration(
        boxShadow: [BoxShadow(color: isImage ? Colors.black38 : AColors.cyan.withOpacity(0.5), blurRadius: 2)],
        image: DecorationImage(
          image: isImage
              ? (_isLocalUrl(file.path) ? FileImage(file) : NetworkImage(file.path))
              : AssetImage("assets/images/${isPdf ? "pdf" : isExcel ? "excel" : "doc"}.png"),
          fit: BoxFit.cover,
        ),
        borderRadius: BorderRadius.circular(8),
      ),
      child: MaterialButton(
        padding: EdgeInsets.zero,
        onPressed: () async {
          if (isImage) {
            Navigator.of(context).push(
              MaterialPageRoute(
                builder: (_) => Scaffold(
                  body: SafeArea(
                    child: Stack(
                      children: [
                        InteractiveViewer(
                                child: _isLocalUrl(file.path)
                                    ? Image.file(file)
                                    : ImageLoader(
                                        url: file.path,
                                        boxFit: BoxFit.cover,
                                      ))
                            .center,
                        const ABackButton(),
                      ],
                    ),
                  ),
                ),
              ),
            );
          } else if (_isLocalUrl(file.path)) {
            OpenFile.open(file.path);
          } else {
            if (!await launchUrl(Uri.parse(file.path), mode: LaunchMode.externalApplication)) {
              Fluttertoast.showToast(msg: "UnExpected Error with file.");
              throw Exception('Could not launch ');
            }
          }
        },
        child: enabled
            ? Align(
                alignment: Alignment.topRight,
                child: IconButton(
                  padding: const EdgeInsets.all(2.0),
                  icon: Container(
                    padding: const EdgeInsets.all(1),
                    decoration: BoxDecoration(color: Theme.of(context).scaffoldBackgroundColor.withOpacity(.3), borderRadius: BorderRadius.circular(8)),
                    child: const Icon(
                      Icons.remove_circle,
                      color: AColors.red,
                    ),
                  ),
                  onPressed: () {
                    onRemoveTap(file);
                  },
                ),
              )
            : const SizedBox(),
      ),
    );
  }

  bool _isLocalUrl(String url) {
    if (url?.isEmpty != false) return false;
    return url.startsWith("/") || url.startsWith("file://") || url.substring(1).startsWith(':\\');
  }
}
