import 'package:flutter/material.dart';
import 'package:flutter_svg/svg.dart';
import 'package:tangheem/api/tangheem_user_api_client.dart';
import 'package:tangheem/classes/colors.dart';
import 'package:tangheem/classes/consts.dart';
import 'package:tangheem/classes/utils.dart';
import 'package:tangheem/models/content_info_model.dart';
import 'package:tangheem/ui/misc/no_data_ui.dart';

class ContentInfoScreen extends StatefulWidget {
  static const String routeName = "/content_info";
  final int contentId;

  ContentInfoScreen({Key key, this.contentId}) : super(key: key);

  @override
  _ContentInfoScreenState createState() => _ContentInfoScreenState();
}

class _ContentInfoScreenState extends State<ContentInfoScreen> {
  List<ContentInfoDataModel> contentList;

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

  void getContents() async {
    Utils.showLoading(context);
    try {
      var membersData = await TangheemUserApiClient().getContentInfo(widget.contentId);
      contentList = membersData?.data ?? [];
    } catch (ex) {
      contentList = [];
      if (mounted) Utils.handleException(ex, null);
    } finally {
      Utils.hideLoading(context);
    }
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    return contentList == null
        ? SizedBox()
        : contentList.isEmpty
            ? NoDataUI()
            : ListView.separated(
                physics: BouncingScrollPhysics(),
                padding: EdgeInsets.all(16),
                itemCount: contentList.length,
                separatorBuilder: (context, index) {
                  return SizedBox(height: 8);
                },
                itemBuilder: (context, index) {
                  return ListTile(
                    tileColor: Colors.white,
                    leading: Container(
                      width: 50.0,
                      height: 50.0,
                      decoration: BoxDecoration(
                        image: contentList.length < 1
                            ? null
                            : DecorationImage(
                                fit: BoxFit.cover,
                                image: NetworkImage(ApiConsts.baseUrl +contentList.elementAt(index).exposeFilePath),
                              ),
                        borderRadius: BorderRadius.all(
                          Radius.circular(12.0),
                        ),
                      ),
                      child: contentList.length < 1
                          ? ClipRRect(
                              borderRadius: BorderRadius.all(
                                Radius.circular(30.0),
                              ),
                              child: SvgPicture.asset(
                                "assets/icons/chat_user.svg",
                                clipBehavior: Clip.antiAlias,
                              ),
                            )
                          : null,
                    ),
                    title: Text(
                      contentList[index].contentTypeNameAr,
                      style: TextStyle(fontSize: 14, color: ColorConsts.primaryBlue),
                    ),
                    subtitle: Text(
                      " ${contentList[index].content}",
                      style: TextStyle(fontSize: 12, color: ColorConsts.primaryBlue),
                    ),
                    isThreeLine: true,
                  );
                },
              );
  }
}
