import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:solian/models/realm.dart';
import 'package:solian/providers/auth.dart';
import 'package:solian/router.dart';
import 'package:solian/screens/realms/realm_organize.dart';
import 'package:solian/widgets/realms/realm_deletion.dart';
import 'package:solian/widgets/realms/realm_member.dart';

class RealmDetailScreen extends StatefulWidget {
  final String alias;
  final Realm realm;

  const RealmDetailScreen({
    super.key,
    required this.alias,
    required this.realm,
  });

  @override
  State<RealmDetailScreen> createState() => _RealmDetailScreenState();
}

class _RealmDetailScreenState extends State<RealmDetailScreen> {
  bool _isOwned = false;

  void checkOwner() async {
    final AuthProvider auth = Get.find();
    setState(() {
      _isOwned = auth.userProfile.value!['id'] == widget.realm.accountId;
    });
  }

  void showMemberList() {
    showModalBottomSheet(
      context: context,
      useRootNavigator: true,
      isScrollControlled: true,
      builder: (context) => RealmMemberListPopup(
        realm: widget.realm,
      ),
    );
  }

  void promptLeaveChannel() async {
    final did = await showDialog(
      context: context,
      builder: (context) => RealmDeletionDialog(
        realm: widget.realm,
        isOwned: _isOwned,
      ),
    );
    if (did == true && AppRouter.instance.canPop()) {
      AppRouter.instance.pop(false);
    }
  }

  @override
  void initState() {
    super.initState();

    checkOwner();
  }

  @override
  Widget build(BuildContext context) {
    final ownerActions = [
      ListTile(
        leading: const Icon(Icons.settings),
        trailing: const Icon(Icons.chevron_right),
        title: Text('realmSettings'.tr.capitalize!),
        onTap: () async {
          AppRouter.instance
              .pushNamed(
            'realmOrganizing',
            extra: RealmOrganizeArguments(edit: widget.realm),
          )
              .then((resp) {
            if (resp != null) {
              AppRouter.instance.pop(resp);
            }
          });
        },
      ),
    ];

    return Column(
      children: [
        Padding(
          padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 12),
          child: Row(
            children: [
              const CircleAvatar(
                radius: 28,
                backgroundColor: Colors.teal,
                child: Icon(Icons.group, color: Colors.white),
              ),
              const SizedBox(width: 16),
              Expanded(
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Text(widget.realm.name,
                        style: Theme.of(context).textTheme.bodyLarge),
                    Text(widget.realm.description,
                        style: Theme.of(context).textTheme.bodySmall),
                    Text(
                      '#${widget.realm.id.toString().padLeft(8, '0')} · ${widget.realm.alias}',
                      style: const TextStyle(fontSize: 11),
                    ),
                  ],
                ),
              )
            ],
          ),
        ),
        const Divider(thickness: 0.3),
        Expanded(
          child: ListView(
            children: [
              ListTile(
                leading: const Icon(Icons.supervisor_account),
                trailing: const Icon(Icons.chevron_right),
                title: Text('realmMembers'.tr.capitalize!),
                onTap: () => showMemberList(),
              ),
              ...(_isOwned ? ownerActions : List.empty()),
              const Divider(thickness: 0.3),
              ListTile(
                leading: _isOwned
                    ? const Icon(Icons.delete)
                    : const Icon(Icons.exit_to_app),
                title: Text(_isOwned ? 'delete'.tr : 'leave'.tr),
                onTap: () => promptLeaveChannel(),
              ),
            ],
          ),
        ),
      ],
    );
  }
}
