import 'package:e_commerce_training/res/R.dart';
import 'package:e_commerce_training/src/page/blogs/blog_page.dart';
import 'package:e_commerce_training/src/page/change_pass/change_pass.dart';
import 'package:e_commerce_training/src/page/customer_support/customer_support_page.dart';
import 'package:e_commerce_training/src/page/login/login_page.dart';
import 'package:e_commerce_training/src/page/order_page/order_page.dart';
import 'package:e_commerce_training/src/page/profile/profile_page.dart';
import 'package:e_commerce_training/src/page/rate_product/rate_product_page.dart';
import 'package:e_commerce_training/src/page/setting/setting_cubit.dart';
import 'package:e_commerce_training/src/page/sign_up/sign_up.dart';
import 'package:e_commerce_training/src/page/videos/video_page.dart';
import 'package:e_commerce_training/src/page/viewed_products/viewed_products_page.dart';
import 'package:e_commerce_training/src/util/navigation_util.dart';
import 'package:e_commerce_training/src/util/util.dart';
import 'package:e_commerce_training/src/widget/custom_appbar.dart';
import 'package:e_commerce_training/src/widget/custom_circle_image.dart';
import 'package:e_commerce_training/src/widget/pending_action.dart';
import 'package:e_commerce_training/src/widget/setting_widget.dart';
import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.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';

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

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

class _SettingPageState extends State<SettingPage> {
  late SettingCubit _cubit;
  RefreshController _refreshController = RefreshController();

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _cubit = BlocProvider.of<SettingCubit>(context);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: appBar(context, R.string.individual.tr(), rightIcons: [MdiIcons.cartOutline]),
      backgroundColor: R.color.grey200,
      body: BlocProvider(
        create: (BuildContext context) => _cubit,
        child: BlocConsumer<SettingCubit, SettingState>(
          listener: (context, state) {
            if (state is SettingFailure) Util.showErrorSnackBar(context, state.error);
            if (!(state is SettingLoading))
              _refreshController.refreshCompleted();
            _refreshController.loadComplete();
          },
          builder: (context, state) {
            return Stack(
              children: [
                SmartRefresher(
                  enablePullUp: true,
                  controller: _refreshController,
                  onRefresh: () => _cubit.getSettingTask(isRefresh: true),
                  onLoading: () => _cubit.getSettingTask(isLoadMore: true),
                  child: SingleChildScrollView(
                    child: Container(
                      margin: EdgeInsets.only(bottom: 10),
                      child: Column(
                        children: [
                          _cubit.isLoggedIn ? profileWidget() : loginWidget(),
                          SizedBox(
                            height: 5,
                          ),
                          Container(
                            color: R.color.white,
                            padding: EdgeInsets.symmetric(vertical: 15),
                            child: Row(
                              mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                              children: [
                                orderWidget(R.string.wait_confirmation.tr(), R.drawable.waiting,
                                        () => NavigationUtil.rootNavigatePage(context, OrderPage(1))),
                                orderWidget(
                                  R.string.confirmed.tr(),
                                  R.drawable.confirmed,
                                      () => NavigationUtil.rootNavigatePage(context, OrderPage(2)),
                                ),
                                orderWidget(
                                  R.string.delivering.tr(),
                                  R.drawable.delivery,
                                      () => NavigationUtil.rootNavigatePage(context, OrderPage(3)),
                                ),
                                orderWidget(R.string.successful.tr(), R.drawable.bag,
                                        () => NavigationUtil.rootNavigatePage(context, OrderPage(4))),
                              ],
                            ),
                          ),
                          SizedBox(
                            height: 1,
                          ),
                          SettingWidget(
                            isShowLogin: true,
                            image: R.drawable.cancel,
                            color: R.color.accentColor,
                            title: R.string.order_canceled.tr(),
                          ),
                          _cubit.isLoggedIn
                              ? SizedBox(
                            height: 5,
                          )
                              : SizedBox(),
                          SettingWidget(
                            isShowLogin: _cubit.isLoggedIn,
                            image: R.drawable.wallet_1,
                            color: R.color.red,
                            title: R.string.purchased_product.tr(),
                            callback: () {
                              NavigationUtil.rootNavigatePage(context, RateProductPage());
                            },
                          ),
                          SettingWidget(
                            isShowLogin: _cubit.isLoggedIn,
                            image: R.drawable.eyes,
                            color: R.color.blue,
                            title: R.string.viewed_products.tr(),
                            callback: () {
                              NavigationUtil.rootNavigatePage(context, ViewedProducts());
                            },
                          ),
                          SizedBox(height: 5),
                          SettingWidget(
                            isShowLogin: _cubit.isLoggedIn,
                            image: R.drawable.ic_lock,
                            color: R.color.blue,
                            title: R.string.change_password.tr(),
                            callback: () {
                              NavigationUtil.rootNavigatePage(context, ChangePassPage());
                            },
                          ),
                          Column(
                            children: [
                              Container(
                                padding: EdgeInsets.symmetric(vertical: 3, horizontal: 15),
                                color: R.color.white,
                                child: Row(
                                  children: [
                                    Image.asset(
                                      R.drawable.ic_notification,
                                      height: 30.h,
                                      width: 30.h,
                                      color: R.color.orange,
                                    ),
                                    SizedBox(
                                      width: 15,
                                    ),
                                    Expanded(
                                      child: Text(
                                        R.string.allow_notifications.tr(),
                                        style: TextStyle(
                                          fontSize: 16.sp,
                                          color: R.color.black,
                                        ),
                                      ),
                                    ),
                                    CupertinoSwitch(
                                      value: _cubit.isSwitched,
                                      onChanged: (value) {
                                        _cubit.changeSwitched(value);
                                      },
                                      activeColor: R.color.green,
                                    )
                                  ],
                                ),
                              ),
                              SizedBox(
                                height: 1,
                              )
                            ],
                          ),
                          SettingWidget(
                            isShowLogin: true,
                            image: R.drawable.headphones,
                            color: R.color.primaryColor,
                            title: R.string.help_center.tr(),
                            callback: () {
                              NavigationUtil.rootNavigatePage(context, CustomerSupportPage());
                            },
                          ),
                          SettingWidget(
                            isShowLogin: true,
                            image: R.drawable.blogs,
                            color: R.color.red,
                            title: R.string.blogs.tr(),
                            callback: () {
                              NavigationUtil.rootNavigatePage(context, BlogPage());
                            },
                          ),
                          SettingWidget(
                            isShowLogin: true,
                            image: R.drawable.ic_video,
                            color: R.color.red,
                            title: R.string.video.tr(),
                            callback: () {
                              NavigationUtil.rootNavigatePage(context, VideoPage());
                            },
                          ),
                          Visibility(
                            visible: _cubit.isLoggedIn,
                            child: InkWell(
                              onTap: () async {
                                await _showConfirm(context);
                              },
                              child: Column(
                                children: [
                                  Container(
                                    padding: EdgeInsets.symmetric(vertical: 12, horizontal: 15),
                                    color: R.color.white,
                                    child: Row(
                                      children: [
                                        Image.asset(
                                          R.drawable.ic_logout,
                                          height: 30.h,
                                          width: 30.h,
                                          color: R.color.orange,
                                        ),
                                        SizedBox(
                                          width: 15,
                                        ),
                                        Expanded(
                                          child: Text(
                                            R.string.logout.tr(),
                                            style: TextStyle(
                                              fontSize: 16.sp,
                                              color: R.color.black,
                                            ),
                                          ),
                                        ),
                                      ],
                                    ),
                                  ),
                                  SizedBox(height: 1,)
                                ],
                              ),
                            ),
                          ),
                        ],
                      ),
                    ),
                  ),
                ),
                Visibility(
                  visible: state is SettingLoading,
                  child: PendingAction(),
                ),
              ],
            );
          },
        ),
      ),
    );
  }

  Future _showConfirm(BuildContext context) async {
    Util.showDialogTextTwoButton(
        context: context,
        title: R.string.notice.tr(),
        contentText: R.string.confirm_logout.tr(),
        submitText: R.string.yes.tr(),
        submitCallback: () {
          _cubit.logOut();
        });
  }

  Widget loginWidget() {
    return Container(
      color: R.color.primaryColor,
      padding: EdgeInsets.symmetric(vertical: 10, horizontal: 15),
      child: Row(
        children: [
          CircleAvatar(
            radius: 30.h,
            backgroundColor: R.color.white,
            backgroundImage: AssetImage(R.drawable.admin),
          ),
          Spacer(),
          InkWell(
            child: Container(
              alignment: Alignment.center,
              padding: EdgeInsets.symmetric(vertical: 10, horizontal: 15),
              decoration: BoxDecoration(
                color: R.color.white,
                border: Border.all(width: 1, color: R.color.white),
                borderRadius: BorderRadius.circular(5),
              ),
              child: Text(
                R.string.sign_in.tr(),
                style: TextStyle(fontSize: 16.sp, color: R.color.black),
              ),
            ),
            onTap: () {
              NavigationUtil.rootNavigatePage(context, LoginPage()).then((value) => _cubit.refreshSetting());
            },
          ),
          SizedBox(
            width: 10,
          ),
          InkWell(
            child: Container(
              alignment: Alignment.center,
              padding: EdgeInsets.symmetric(vertical: 10, horizontal: 15),
              decoration: BoxDecoration(
                borderRadius: BorderRadius.circular(5),
                border: Border.all(width: 1, color: R.color.white),
              ),
              child: Text(
                R.string.sign_up.tr(),
                style: TextStyle(fontSize: 16.sp, color: R.color.white),
              ),
            ),
            onTap: () {
              NavigationUtil.rootNavigatePage(context, SignUpPage());
            },
          ),
        ],
      ),
    );
  }

  Widget profileWidget() {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Container(
          color: R.color.white,
          padding: EdgeInsets.all(15),
          child: Row(
            children: [
              SizedBox(
                height: 60.h,
                width: 60.h,
                child: CustomCircleImage(
                    urlImage: _cubit.avatar ?? "",
                    placeHolder: R.drawable.admin,
                    onTapImage: () {
                      NavigationUtil.rootNavigatePage(context, ProfilePage());
                    }),
              ),
              SizedBox(
                width: 15,
              ),
              Expanded(
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  mainAxisAlignment: MainAxisAlignment.start,
                  children: [
                    // Lay tu sharePref
                    Text(_cubit.name ?? "",
                        style: TextStyle(fontSize: 16.sp, fontWeight: FontWeight.w600, color: R.color.black)),
                    SizedBox(
                      height: 3,
                    ),
                    // Lay tu sharePref
                    Text(
                      _cubit.email ?? "",
                      style: TextStyle(fontSize: 16.sp, color: R.color.grey500),
                    ),
                    SizedBox(
                      height: 3,
                    ),
                    Text(
                      R.string.join_at.tr(args: [_cubit.joinAt ?? ""]),
                      style: TextStyle(fontSize: 16.sp, color: R.color.black),
                    ),
                  ],
                ),
              ),
            ],
          ),
        ),
        Container(height: 5, width: double.infinity),
        Container(
          padding: EdgeInsets.all(15),
          color: R.color.white,
          child: Row(
            children: [
              Image.asset(
                R.drawable.money,
                height: 30.h,
                width: 30.h,
                color: R.color.orange,
              ),
              SizedBox(
                width: 15,
              ),
              Text(
                R.string.accumulation.tr(),
                style: TextStyle(
                  fontSize: 16.sp,
                  color: R.color.black,
                ),
              ),
              Text(
                _cubit.point.toString(),
                style: TextStyle(color: R.color.primaryColor, fontSize: 18.sp, fontWeight: FontWeight.bold),
              )
            ],
          ),
        )
      ],
    );
  }

  Widget orderWidget(String title, String image, VoidCallback onClick) {
    return GestureDetector(
      onTap: onClick,
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.center,
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Image.asset(
            image,
            height: 35.h,
            color: R.color.blue,
          ),
          SizedBox(
            height: 5,
          ),
          Text(
            title,
            style: TextStyle(fontSize: 14.sp, color: R.color.black),
            textAlign: TextAlign.center,
          )
        ],
      ),
    );
  }
}
