/*
 * @Author: WuXiangNan
 * @Date: 2021-05-28 09:53:32
 * @LastEditors  : WuWei
 * @LastEditTime : 2022-01-19 16:25:42
 * @Description: 绘经营store
 */

import 'package:app_package/app_package.dart';
import 'package:business_package/src/models/bill_data.dart';
import 'package:business_package/src/models/bill_data_line_chart.dart';
import 'package:business_package/src/models/community/community_data.dart';
import 'package:business_package/src/models/community_statistics.dart';
import 'package:business_package/src/models/hardware_data/hardware_data.dart';
import 'package:business_package/src/models/platform_data.dart';
import 'package:business_package/src/models/platform_data_line_chart.dart';
import 'package:business_package/src/models/serve_data/serve_data.dart';
import 'package:business_package/src/service/impl/business_service_impl.dart';
import 'package:flutter/material.dart';
// ignore: implementation_imports
import 'package:app_package/src/api/employee.dart';
import 'package:wisdom_cli/wisdom_cli.dart';

class BusinessStore with ChangeNotifier {
  final BuildContext context;

  BusinessStore(this.context);

  CommunityStatistics? communityStatistics;

  String dataType = 'day';

  List<CommunityModel> communityList = [];
  String communityIds = '';

  bool isShowList = false;

  List<CommunityData>? communitiesData = [];

  // 项目详情
  CommunityStatistics projectData = CommunityStatistics();

  // 绘服务数据
  PlatformData platformData = PlatformData();

  // 绘服务折线
  PlatformDataLineChart platformDataLineChart = PlatformDataLineChart();
  PlatformDataLineChart platformDataLineChartLine = PlatformDataLineChart();

  // 收费数据
  BillData billData = BillData();

  // 收费折线
  BillDataLineChart billDataLineChart = BillDataLineChart();
  BillDataLineChart billDataLineChartLine = BillDataLineChart();

  // 客户服务数据
  ServeData serveData = ServeData();

  // 智能设备数据
  HardwareData hardwareData = HardwareData();

  // 筛选项目
  Map? selectedCommunities;

  Future getReady() async {
    if (communityIds == '') await getCommunitysList();
    getCommunitiesByEntCode();
    getProjectData();
  }

  Future getData() async {
    if (communityIds == '') await getCommunitysList();
    // getProjectData();
    getBillData();
    getPlatformData();
    getServeData();
    getHardwareData();
    getPlatformLineChart();
    getBillLineChart();
  }

  ///获取用户管辖的小区列表
  getCommunitysList() async {
    var res = await EmployeeApi.getDomainCommunitysList();
    communityList = res.data ?? [];
    communityList.forEach((community) {
      if (communityIds == '') {
        communityIds = community.id!;
      } else {
        communityIds = communityIds + ',' + community.id!;
      }
    });
    notifyListeners();
  }

  Future getProjectData() async {
    var res = await BusinessServiceImpl()
        .getCommunityStatistics({'communityIds': communityIds});
    projectData = res.data!;
    notifyListeners();
  }

  Future getPlatformData() async {
    var res = await BusinessServiceImpl()
        .getPlatformData({'communityIds': communityIds, 'domain': dataType});
    platformData = res.data!;
    notifyListeners();
  }

  Future getBillData() async {
    var res = await BusinessServiceImpl()
        .getBillData({'communityIds': communityIds, 'domain': dataType});
    billData = res.data!;
    notifyListeners();
  }

  Future getServeData() async {
    var res = await BusinessServiceImpl()
        .getServeData({'communityIds': communityIds, 'domain': dataType});
    serveData = res.data!;
    notifyListeners();
  }

  Future getHardwareData() async {
    var res = await BusinessServiceImpl()
        .getHardwareData({'communityIds': communityIds, 'domain': dataType});
    hardwareData = res.data!;
    notifyListeners();
  }

  Future getPlatformLineChart() async {
    if (dataType != 'year') {
      var res = await BusinessServiceImpl().getPlatformDataLineChart(
          {'communityIds': communityIds, 'domain': dataType});
      platformDataLineChart = res.data!;
      platformDataLineChartLine =
          PlatformDataLineChart.fromJson(res.data!.toJson());
      notifyListeners();
    }
  }

  Future getBillLineChart() async {
    var res = await BusinessServiceImpl().getBillDataLineChart(
        {'communityIds': communityIds, 'domain': dataType});
    billDataLineChart = res.data!;
    billDataLineChartLine = BillDataLineChart.fromJson(res.data!.toJson());
    notifyListeners();
  }

  // 获取项目-小区
  Future getCommunitiesByEntCode() async {
    var res = await BusinessServiceImpl().getCommunities(
        {'entCode': WisStorageUtil.get(key: 'storage-loginInfo')['entCode']});
    communitiesData = res.data;
    notifyListeners();
  }

  // 查看清单
  showProject() {
    if (!isShowList) {
      isShowList = true;
      notifyListeners();
    }
  }

  // 切换Tab
  onChangeTab(index) {
    switch (index) {
      case 0:
        dataType = 'day';
        break;
      case 1:
        dataType = 'month';
        break;
      case 2:
        dataType = 'year';
        break;
    }
  }

  // 绘服务折线 点击隐藏或显示tag 对应线段
  hiddenOrShowPlatformLine(String tag, bool isShow) {
    switch (tag) {
      case 'onUsers':
        platformDataLineChartLine.onUsersLine =
            isShow ? platformDataLineChart.onUsersLine : [];
        break;
      case 'onHouses':
        platformDataLineChartLine.onHousesLine =
            isShow ? platformDataLineChart.onHousesLine : [];
        break;
      case 'onHouseHolds':
        platformDataLineChartLine.onHouseholdsLine =
            isShow ? platformDataLineChart.onHouseholdsLine : [];
    }
  }

  hiddenOrShowBillLine(String tag, bool isShow) {
    switch (tag) {
      case 'IncomeCount':
        billDataLineChartLine.incomeAmountLine =
            isShow ? billDataLineChart.incomeAmountLine : [];
        break;
      case 'IncomeCountOnline':
        billDataLineChartLine.incomeOnlineAmountLine =
            isShow ? billDataLineChart.incomeOnlineAmountLine : [];
        break;
    }
  }
}
