/*
 * @Author: XianKaiQun
 * @Date: 2020-07-23 11:46:55
 * @LastEditors  : WuWei
 * @LastEditTime : 2023-02-02 11:42:11
 * @Description:
 */

import 'package:app_package/app_package.dart';
import 'package:app_package/src/models/hgj_menu.dart';
import 'package:app_package/src/models/scene_scan.dart';
import 'package:flutter/material.dart';
import 'package:wisdom_cli/wisdom_cli.dart';

class Route2business {
  static get _cmmunityGlobalStore => GetIt.I.get<CommunityGlobalStore>();

  ///跳转到业务路由
  ///
  ///传入[HgjMenuModel] 自动判断是否需要先去选择小区
  static route2business(HgjMenuModel data) async {
    ///判断未开通
    if (!data.exist) {
      return WToastUtil.show('正在建设中...');
    }
    if (data.relationCommunity == true) {
      var boolean = await _cmmunityGlobalStore.intent;
      if (boolean) {
        if (data.route == 'qrcode/clean' || data.route == 'qrcode/security') {
          final context = WNavUtil.instance!.context;
          scan2page(
            context: context!,
            scene: data.route == 'qrcode/clean'
                ? ScanScene.clean
                : ScanScene.security,
          );
        } else {
          WNavUtil.instance!
              .pushNamed(data.route!, arguments: {'appBarTitle': data.name});
        }
      }
    } else if (data.route == 'qrcode/clean' ||
        data.route == 'qrcode/security') {
      final context = WNavUtil.instance!.context;
      scan2page(
        context: context!,
        scene:
            data.route == 'qrcode/clean' ? ScanScene.clean : ScanScene.security,
      );
    } else {
      WNavUtil.instance!
          .pushNamed(data.route!, arguments: {'appBarTitle': data.name});
    }
  }

  ///根据场景，扫码跳转对应页
  static Future scan2page({
    ///场景
    required BuildContext context,
    required ScanScene scene,
    String? communityId,
    String? excutivePlatformLinkId,
  }) async {
    ///确保有小区
    final hasCommunity = await _cmmunityGlobalStore.intent;
    bool flag = false;
    if (!hasCommunity) {
      return;
    }

    ///扫码
    final url = await WisScanUtil.instance.cameraScan(
      gallery: false,
    );
    print(url);
    if (url != null && !flag) {
      flag = true;

      ///请求获取类型
      ///获取[jump]  根据[jump]映射[_route]路由
      WLoadingUtil.show();
      final json = await HttpUtil.instance.get(
        '/app/admin/qrCode/sweepCode',
        queryParameters: {
          'url': url,
          'type': scene.string,
          'communityId': communityId ?? _cmmunityGlobalStore.data.id,
          'excutivePlatformLinkId': excutivePlatformLinkId,
        },
      );
      WLoadingUtil.hide();
      final data = ResModel<SceneScanModel>(
        json: json.data,
        to: (v) => v.toJson(),
        from: (v) => SceneScanModel.fromJson(v),
      ).data;
      if (data?.jump != null) {
        if (data?.jump == 'workStandardList') {
          flag = false;
          return data?.sceneValue;
        } else {
          if (_route[data?.jump] != null) {
            flag = false;
            _route[data?.jump]!(data?.sceneValue);
          } else {
            flag = false;
            WToastUtil.show('无法确认的场景值${data?.jump}');
          }
        }
      }
      flag = false;
    }
  }

  Future getUrlData(json) async {
    return ResModel<SceneScanModel>(
      json: json.data,
      to: (v) => v.toJson(),
      from: (v) => SceneScanModel.fromJson(v),
    ).data;
  }

  static final _route = {
    /// 设备登记页
    'equipmentRegister': (sceneValue) => WNavUtil.instance!.pushNamed(
          'qrcode/register_form',
          arguments: {'id': sceneValue},
        ),

    /// 设备详情页
    'equipmentDetails': (sceneValue) => WNavUtil.instance!.pushNamed(
          'qrcode/repair_detail',
          arguments: {'id': sceneValue},
        ),

    /// 设备巡检页
    'equipmentPqc': (sceneValue) => WNavUtil.instance!.pushNamed(
          'qrcode/repair_handle',
          arguments: {'id': sceneValue, 'type': 'pqc'},
        ),

    /// 设备保养页
    'equipmentUpkeep': (sceneValue) => WNavUtil.instance!.pushNamed(
          'qrcode/repair_handle',
          arguments: {'id': sceneValue, 'type': 'upkeep'},
        ),

    /// 设备维修页
    'equipmentService': (sceneValue) => WNavUtil.instance!.pushNamed(
          'qrcode/repair_maintain',
          arguments: {'id': sceneValue},
        ),

    /// 安保巡逻详情页
    'securityDetails': (sceneValue) => WNavUtil.instance!.pushNamed(
          'qrcode/security_list',
          arguments: {'id': sceneValue},
        ),

    /// 安保巡逻登记页
    'security': (sceneValue) => WNavUtil.instance!.pushNamed(
          'qrcode/security',
          arguments: {'id': sceneValue},
        ),

    /// 保洁清洁详情页
    'cleanDetails': (sceneValue) => WNavUtil.instance!.pushNamed(
          'qrcode/clean_list',
          arguments: {'id': sceneValue},
        ),

    /// 保洁清洁登记页
    'clean': (sceneValue) => WNavUtil.instance!.pushNamed(
          'qrcode/clean',
          arguments: {'id': sceneValue},
        ),

    /// 二维码登记
    'qrRegister': (sceneValue) => WNavUtil.instance!.pushNamed(
          'qrcode/register',
          arguments: {'id': sceneValue},
        ),

    /// 作业表单
    'workStandardList': (sceneValue) => sceneValue,

    /// 主页入口
    'QrTaskList': (sceneValue) => {
          if (sceneValue.length > 0)
            {
              WNavUtil.instance!.pushNamed(
                'task/scan',
                arguments: {'id': sceneValue, 'type': 'pqc'},
              ),
            }
          else
            {
              WToastUtil.show('场景值不能为空'),
            }
        },
  };
}

///扫码场景
enum ScanScene {
  /// 设备巡检
  pqc,

  /// 设备保养
  upkeep,

  /// 设备维修
  service,

  /// 安保巡逻
  security,

  /// 保洁清洁
  clean,

  /// 二维码登记
  qrRegister,

  /// 主页入口
  mainEntrance,

  /// 任务入口
  smallEntrance,
}
List<String> _sceneStrings = [
  'pqc',
  'upkeep',
  'service',
  'security',
  'clean',
  'qrRegister',
  'mainEntrance',
  'smallEntrance',
];

extension SceneExt on ScanScene {
  String get string => _sceneStrings[this.index];
}
