import 'package:e_commerce_training/res/R.dart';
import 'package:e_commerce_training/src/model/preference/app_preference.dart';
import 'package:e_commerce_training/src/model/repository/app_repository.dart';
import 'package:e_commerce_training/src/page/customer_support/customer_support_cubit.dart';
import 'package:e_commerce_training/src/util/const.dart';
import 'package:e_commerce_training/src/util/util.dart';
import 'package:e_commerce_training/src/widget/custom_appbar.dart';
import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:material_design_icons_flutter/material_design_icons_flutter.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'package:shared_preferences/shared_preferences.dart';

class CustomerSupportPage extends StatefulWidget {
  const CustomerSupportPage({Key? key}) : super(key: key);

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

class _CustomerSupportPageState extends State<CustomerSupportPage> {
  late CustomerSupportCubit _cubit;
  RefreshController _refreshController = RefreshController();
  late SharedPreferences _preferences;

  @override
  void initState() {
    // TODO: implement initState
    AppRepository repository = AppRepository();
    _cubit = CustomerSupportCubit(repository);
    _cubit.getListPage();
    _cubit.getVersion();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: R.color.grey300,
      appBar: appBar(context, R.string.customer_support.tr(), rightIcons: [MdiIcons.cartOutline]),
      body: BlocProvider(
        create: (BuildContext context) => _cubit,
        child: BlocConsumer<CustomerSupportCubit, CustomerSupportState>(
          listener: (context, state) {
            if (state is SupportFailure) Util.showErrorSnackBar(context, state.error);
            if (!(state is SupportLoading)) _refreshController.refreshCompleted();
          },
          builder: (context, state) {
            return SingleChildScrollView(
              child: Column(
                children: [
                  ListView.builder(
                    shrinkWrap: true,
                    itemCount: _cubit.listPage.length,
                    itemBuilder: (BuildContext context, int index) {
                      return InkWell(
                        onTap: () {},
                        child: Container(
                          padding: EdgeInsets.symmetric(vertical: 15, horizontal: 15),
                          color: R.color.white,
                          child: Row(
                            children: [
                              Text(
                                _cubit.listPage[index].vietnameseName ?? "",
                                style: TextStyle(fontSize: 16.sp, color: R.color.black),
                              ),
                            ],
                          ),
                        ),
                      );
                    },
                  ),
                  SizedBox(
                    height: 10,
                  ),
                  Container(
                    padding: EdgeInsets.symmetric(vertical: 15, horizontal: 15),
                    color: R.color.white,
                    child: Row(
                      children: [
                        Expanded(
                          child: Text(
                            R.string.version.tr(),
                            style: TextStyle(fontSize: 16.sp, color: R.color.black),
                          ),
                        ),
                        Text(
                          _cubit.version,
                          style: TextStyle(fontSize: 16.sp, color: R.color.primaryColor),
                        )
                      ],
                    ),
                  ),
                  SizedBox(
                    height: 10,
                  ),
                  buildCustomerCare(voidCallback: () {
                    Util.launchURL(
                      Util.getTypeUrlLauncher(
                        appPreference.getData(Const.PHONE_NUMBER_SUPPORT) ?? "",
                        Const.TYPE_PHONE,
                      ),
                    );
                  }),
                  SizedBox(
                    height: 10,
                  ),
                ],
              ),
            );
          },
        ),
      ),
    );
  }

  Widget buildCustomerCare({VoidCallback? voidCallback}) {
    return InkWell(
      onTap: voidCallback,
      child: Container(
        margin: EdgeInsets.symmetric(horizontal: 90),
        decoration: BoxDecoration(
          color: R.color.white,
          borderRadius: BorderRadius.all(
            Radius.circular(5),
          ),
        ),
        padding: EdgeInsets.symmetric(vertical: 10, horizontal: 10),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Expanded(
              child: Icon(
                Icons.headset_mic,
                size: 30.h,
              ),
            ),
            SizedBox(
              width: 10,
            ),
            Text(
              R.string.customer_care.tr(),
              style: TextStyle(fontSize: 16.sp),
            )
          ],
        ),
      ),
    );
  }
}
