import 'dart:async';
import 'dart:io';

import 'package:example/pages/visual/visual_debug_design_model.dart';
import 'package:flutter/material.dart';
import 'package:guru_utils/controller/lifecycle_controller.dart';
import 'package:rxdart/rxdart.dart';
import 'package:guru_utils/extensions/extensions.dart';

/// Created by Haoyi on 2022/8/20

class VisualDebugDialogController extends LifecycleController {
  final VisualDebugDesignSpec designModel = VisualDebugDesignSpec.get();

  VisualDebugDialogDesignSpec get dialogDesignModel => designModel.dialogSpec;

  final FocusNode focusNode = FocusNode();
  final TextEditingController editorController = TextEditingController();

  final BehaviorSubject<String> pickedUriSubject = BehaviorSubject.seeded("");

  Stream<String> get observablePickedUri => pickedUriSubject.stream;

  String get currentPickedUri => pickedUriSubject.value;

  String get text => editorController.text;

  VisualDebugDialogController();

  Future onPickedFile(File? file) async {
    if (file != null && file.existsSync()) {
      pickedUriSubject.addEx(file.path);
    }
    // final result = await FilePicker.platform.pickFiles();
    // if (result != null) {
    //   final files = result.files;
    //   if (files.isNotEmpty) {
    //     final filePath = files.first.path;
    //     if (filePath!= null) {
    //       final pickedFile = File(filePath);
    //       if (pickedFile.existsSync()) {
    //         pickedUriSubject.addEx(pickedFile.path);
    //       }
    //     }
    //   }
    // }
  }

  @override
  void onClose() {
    super.onClose();
  }
}
