import 'package:flutter/material.dart';
import 'package:tangheem/api/tangheem_user_api_client.dart';
import 'package:tangheem/app_state/app_state.dart';
import 'package:tangheem/classes/colors.dart';
import 'package:tangheem/classes/utils.dart';
import 'package:tangheem/extensions/int_extensions.dart';
import 'package:tangheem/extensions/string_extensions.dart';
import 'package:tangheem/extensions/widget_extensions.dart';
import 'package:tangheem/models/content_info_model.dart';
import 'package:tangheem/widgets/new/CommonHeader.dart';

class AboutScreen extends StatefulWidget {
  AboutScreen({Key key}) : super(key: key);

  @override
  _AboutScreenState createState() {
    return _AboutScreenState();
  }
}

class _AboutScreenState extends State<AboutScreen> {
  List<ContentInfoDataModel> haqooqAlMosasa;
  List<ContentInfoDataModel> tareefAlMosasa;

  @override
  void initState() {
    super.initState();
    haqooqAlMosasa = AppState().haqooqAlMosasa;
    tareefAlMosasa = AppState().tareefAlMosasa;
    getContents();
  }

  void getContents() async {
    Utils.showLoading(context);
    try {
      if ((haqooqAlMosasa ?? []).isEmpty || (tareefAlMosasa ?? []).isEmpty) {
        List<ContentInfoModel> responseList = await Future.wait([TangheemUserApiClient().getContentInfo(2), if ((haqooqAlMosasa ?? []).isEmpty) TangheemUserApiClient().getContentInfo(1)]);
        tareefAlMosasa = responseList[0].data ?? [];
        AppState().tareefAlMosasa = tareefAlMosasa;
        if ((haqooqAlMosasa ?? []).isEmpty) {
          haqooqAlMosasa = responseList[1].data ?? [];
          AppState().haqooqAlMosasa = haqooqAlMosasa;
        }
      } else {
        await Future.delayed(Duration(milliseconds: 500));
      }
    } catch (ex) {
      haqooqAlMosasa = [];
      tareefAlMosasa = [];
      if (mounted) Utils.handleException(ex, null);
    } finally {
      Utils.hideLoading(context);
    }
    setState(() {});
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    bool isPortrait = MediaQuery.of(context).orientation == Orientation.portrait;
    Widget _header = SizedBox(height: isPortrait ? null : double.infinity, width: double.infinity, child: CommonHeader("عن الموسوعة", "assets/icons/new/about_bg.jpg", Color(0xffAE8646)));
    Widget _dataView = Column(
      children: [
        if ((tareefAlMosasa?.length ?? 0) > 0)
          for (ContentInfoDataModel text in tareefAlMosasa) text.content.toText(13, color: ColorConsts.darkText, textAlign: TextAlign.center),
        if ((haqooqAlMosasa?.length ?? 0) > 0)
          Column(
            children: [
              "حقوق الموسوعة".toText(13, color: ColorConsts.greyLightColor),
              for (ContentInfoDataModel text in haqooqAlMosasa) text.content.toText(13, color: ColorConsts.greyLightColor, textAlign: TextAlign.center)
            ],
          ).paddingOnly(top: 24)
      ],
    );

    return SizedBox(
      height: double.infinity,
      child: isPortrait
          ? SingleChildScrollView(
              physics: const AlwaysScrollableScrollPhysics(),
              child: Column(
                children: [
                  _header,
                  _dataView.paddingOnly(top: 70, left: 35, right: 35, bottom: 125),
                ],
              ),
            )
          : Row(
              children: [
                Expanded(
                    child: SizedBox(
                        height: double.infinity,
                        child: SingleChildScrollView(padding: EdgeInsets.only(left: 25, right: 25, top: 45, bottom: 45), physics: const AlwaysScrollableScrollPhysics(), child: _dataView)),
                    flex: 6),
                Expanded(child: _header, flex: 4),
              ],
            ),
    );
  }
}
