/*
 * @Author: WuXiangNan
 * @Date: 2021-06-28 08:52:05
 * @LastEditors: WuXiangNan
 * @LastEditTime: 2021-07-22 11:54:33
 * @Description: 
 */

import 'package:app_package/app_package.dart';
import 'package:cabinet_package/src/models/auth_company.dart';
import 'package:cabinet_package/src/models/cabinet_file_details.dart';
import 'package:cabinet_package/src/models/cabinet_file.dart';
import 'package:cabinet_package/src/models/cabinet_directory.dart';
import 'package:cabinet_package/src/models/cabinet_tree.dart';
import 'package:cabinet_package/src/service/cabinet_service.dart';

class CabinetServiceImp extends CabinetService {
  static String prefixDirectory = '/app/admin/cabinetDirectory';
  static String prefixFile = '/app/admin/cabinetFile';

  @override
  Future<ResModel<List<CabinetTree>?>> getCabinetTree(companyId) async {
    var res = await HttpUtil.instance.get(
      '$prefixDirectory/tree/$companyId',
    );

    return ResModel<List<CabinetTree>?>(
      json: res.data,
      to: (v) => (v ?? []).map((e) => e.toJson()).toList(),
      from: (v) => (v ?? [])
          .map<CabinetTree>(
            (f) => CabinetTree.fromJson(f),
          )
          .toList(),
    );
  }

  @override
  Future<ResModel<CabinetDirectory>> getCabinetDrectoryList(params) async {
    var res = await HttpUtil.instance
        .get('$prefixDirectory', queryParameters: params);

    return ResModel<CabinetDirectory>(
        json: res.data,
        to: (v) => v.toJson(),
        from: (v) => CabinetDirectory.fromJson(v));
  }

  @override
  Future<ResModel<FileDetails>> getCabinetFileDetails(id) async {
    var res = await HttpUtil.instance.get(
      '$prefixFile/$id',
    );

    return ResModel<FileDetails>(
        json: res.data,
        to: (v) => v.toJson(),
        from: (v) => FileDetails.fromJson(v));
  }

  @override
  Future<ResModel<CabinetFile>> getCabinetFileList(params) async {
    var res =
        await HttpUtil.instance.get('$prefixFile', queryParameters: params);

    return ResModel<CabinetFile>(
        json: res.data,
        to: (v) => v.toJson(),
        from: (v) => CabinetFile.fromJson(v));
  }

  @override
  Future<ResModel?> getCabinetPreview(id) async {
    var res = await HttpUtil.instance.get('$prefixFile/preview/$id');
    if (res.data) {
      return res.data;
    }
    return null;
  }

  @override
  Future postCabinetPwdMatch(data) async {
    var res =
        await HttpUtil.instance.post('/app/admin/user/psw/match', data: data);
    return res.data;
  }

  @override
  Future<ResModel<List<AuthCompany>?>> getCabinetauthCompanyDict() async {
    var res = await HttpUtil.instance
        .get('/app/admin/cabinetFileAuth/authCompanyDict');

    return ResModel<List<AuthCompany>?>(
      json: res.data,
      to: (v) => (v ?? []).map((e) => e.toJson()).toList(),
      from: (v) => (v ?? [])
          .map<AuthCompany>(
            (f) => AuthCompany.fromJson(f),
          )
          .toList(),
    );
  }
}
