import 'package:PiliPlus/http/loading_state.dart';
import 'package:PiliPlus/http/member.dart';
import 'package:PiliPlus/http/video.dart';
import 'package:PiliPlus/models/space/data.dart';
import 'package:PiliPlus/models/space/tab2.dart';
import 'package:PiliPlus/pages/common/common_controller.dart';
import 'package:PiliPlus/utils/storage.dart';
import 'package:PiliPlus/utils/utils.dart';
import 'package:flutter/material.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:get/get.dart';
import 'package:intl/intl.dart';
import 'package:share_plus/share_plus.dart';

enum MemberTabType { none, home, dynamic, contribute, favorite, bangumi }

extension MemberTabTypeExt on MemberTabType {
  String get title => ['默认', '首页', '动态', '投稿', '收藏', '番剧'][index];
}

class MemberControllerNew extends CommonController
    with GetTickerProviderStateMixin {
  MemberControllerNew({required this.mid});
  int? mid;
  RxDouble scrollRatio = 0.0.obs;
  String? username;
  int? ownerMid;
  RxBool isFollow = false.obs;
  RxInt relation = 1.obs;
  TabController? tabController;
  late final List<Tab> tabs;
  List<Tab2>? tab2;
  RxInt contributeInitialIndex = 0.obs;
  double? top;
  bool? hasSeasonOrSeries;

  @override
  void onInit() {
    super.onInit();
    ownerMid = GStorage.userInfo.get('userInfoCache')?.mid;
    queryData();
  }

  dynamic live;

  int? silence;
  String? endTime;

  @override
  bool customHandleResponse(Success response) {
    Data data = response.response;
    username = data.card?.name ?? '';
    isFollow.value = data.card?.relation?.isFollow == 1;
    relation.value = data.relSpecial == 1 ? 2 : data.relation ?? 1;
    tab2 = data.tab2;
    live = data.live;
    silence = data.card?.silence;
    if ((data.ugcSeason?.count != null && data.ugcSeason?.count != 0) ||
        data.series?.item?.isNotEmpty == true) {
      hasSeasonOrSeries = true;
    }
    if (data.card?.endTime != null) {
      if (data.card!.endTime == 0) {
        endTime = ': 永久封禁';
      } else if (data.card!.endTime! >
          DateTime.now().millisecondsSinceEpoch ~/ 1000) {
        endTime =
            '：至 ${DateFormat('yyyy-MM-dd HH:mm:ss').format(DateTime.fromMillisecondsSinceEpoch(data.card!.endTime! * 1000))}';
      }
    }
    if (tab2 != null && tab2!.isNotEmpty) {
      if (!data.tab!.toJson().values.contains(true) &&
          tab2!.first.param == 'home') {
        // remove empty home tab
        tab2!.removeAt(0);
      }
      if (tab2!.isNotEmpty) {
        int initialIndex = -1;
        MemberTabType memberTab = GStorage.memberTab;
        if (memberTab != MemberTabType.none) {
          initialIndex = tab2!.indexWhere((item) {
            return item.param == memberTab.name;
          });
        }
        if (initialIndex == -1) {
          if (data.defaultTab == 'video') {
            data.defaultTab = 'dynamic';
          }
          initialIndex = tab2!.indexWhere((item) {
            return item.param == data.defaultTab;
          });
        }
        tabs = tab2!.map((item) => Tab(text: item.title ?? '')).toList();
        tabController = TabController(
          vsync: this,
          length: tab2!.length,
          initialIndex: initialIndex == -1 ? 0 : initialIndex,
        );
      }
    }
    loadingState.value = response;
    return true;
  }

  @override
  Future<LoadingState> customGetData() => MemberHttp.space(mid: mid);

  Future blockUser(BuildContext context) async {
    if (ownerMid == null) {
      SmartDialog.showToast('账号未登录');
      return;
    }
    await showDialog(
      context: context,
      builder: (context) {
        return AlertDialog(
          title: const Text('提示'),
          content: Text(relation.value != -1 ? '确定拉黑UP主?' : '从黑名单移除UP主'),
          actions: [
            TextButton(
              onPressed: Get.back,
              child: Text(
                '点错了',
                style: TextStyle(color: Theme.of(context).colorScheme.outline),
              ),
            ),
            TextButton(
              onPressed: () async {
                Get.back();
                _onBlock();
              },
              child: const Text('确认'),
            )
          ],
        );
      },
    );
  }

  void shareUser() {
    Share.share('https://space.bilibili.com/$mid');
  }

  void _onBlock() async {
    dynamic res = await VideoHttp.relationMod(
      mid: mid ?? -1,
      act: relation.value != -1 ? 5 : 6,
      reSrc: 11,
    );
    if (res['status']) {
      relation.value = relation.value != -1 ? -1 : 1;
      isFollow.value = false;
    }
  }

  void onFollow(BuildContext context) async {
    if (mid == ownerMid) {
      Get.toNamed('/editProfile');
    } else if (relation.value == -1) {
      _onBlock();
    } else {
      if (ownerMid == null) {
        SmartDialog.showToast('账号未登录');
        return;
      }
      Utils.actionRelationMod(
        context: context,
        mid: mid,
        isFollow: isFollow.value,
        callback: (attribute) {
          relation.value = attribute;
          isFollow.value = attribute != 0;
        },
      );
    }
  }

  @override
  void onClose() {
    tabController?.dispose();
    super.onClose();
  }
}
