import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:yumi/app_localizations.dart';
import 'package:yumi/ui_kit/components/sc_debounce_widget.dart';
import 'package:yumi/ui_kit/components/text/sc_text.dart';
import 'package:yumi/shared/tools/sc_loading_manager.dart';
import 'package:yumi/shared/tools/sc_pick_utils.dart';
import 'package:yumi/shared/data_sources/sources/local/user_manager.dart';
import 'package:yumi/ui_kit/components/appbar/socialchat_appbar.dart';
import 'package:yumi/ui_kit/components/custom_cached_image.dart';
import 'package:yumi/ui_kit/components/sc_tts.dart';
import 'package:yumi/app/constants/sc_screen.dart';
import 'package:yumi/app/constants/sc_global_config.dart';
import 'package:yumi/shared/data_sources/sources/repositories/sc_room_repository_imp.dart';

///举报
class ReportPage extends StatefulWidget {
  final String type;

  ///被举报的id(用户id或者房间id)
  final String tageId;

  const ReportPage({Key? key, required this.type, required this.tageId})
    : super(key: key);

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

class _ReportPageState extends State<ReportPage> {
  num selectedIndex = 0;
  String reportReason = "";
  String pic01 = "";
  String pic02 = "";
  String pic03 = "";
  final TextEditingController _descriptionController = TextEditingController();

  @override
  Widget build(BuildContext context) {
    return SCDebounceWidget(child: Stack(
      children: [
        Image.asset(
          SCGlobalConfig.businessLogicStrategy.getReportPageBackgroundImage(),
          width: ScreenUtil().screenWidth,
          height: ScreenUtil().screenHeight,
          fit: BoxFit.fill,
        ),
        Scaffold(
          backgroundColor: Colors.transparent,
          resizeToAvoidBottomInset: false,
          appBar: SocialChatStandardAppBar(
            actions: [],
            title: SCAppLocalizations.of(context)!.report,
            backgroundColor: Colors.transparent,
          ),
          body: SafeArea(
            top: false,
            child: SingleChildScrollView(
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Row(
                    children: [
                      SizedBox(width: 15.w),
                      text(
                        SCAppLocalizations.of(
                          context,
                        )!.pleaseSelectTheTypeContent,
                        fontSize: 14.sp,
                        textColor: SCGlobalConfig.businessLogicStrategy.getReportPageHintTextColor(),
                      ),
                      SizedBox(width: 15.w),
                    ],
                  ),
                  Container(
                    margin: EdgeInsets.only(top: 10.w, left: 15.w, right: 15.w),
                    padding: EdgeInsets.symmetric(horizontal: 15.w),
                    child: Column(
                      children: [
                        _item(SCAppLocalizations.of(context)!.spam, 3),
                        _item(SCAppLocalizations.of(context)!.fraud, 4),
                        _item(
                          SCAppLocalizations.of(context)!.maliciousHarassment,
                          0,
                        ),
                        _item(SCAppLocalizations.of(context)!.other, 1),
                        Row(
                          children: [
                            text(
                              SCAppLocalizations.of(context)!.description,
                              fontSize: 14.sp,
                              textColor: SCGlobalConfig.businessLogicStrategy.getReportPagePrimaryTextColor(),
                            ),
                            Spacer(),
                          ],
                        ),
                        SizedBox(height: 8.w),
                        Container(
                          padding: EdgeInsets.all(5.w),
                          decoration: BoxDecoration(
                            borderRadius: BorderRadius.circular(12),
                            color: SCGlobalConfig.businessLogicStrategy.getReportPageContainerBackgroundColor(),
                          ),
                          child: TextField(
                            controller: _descriptionController,
                            onChanged: (text) {},
                            maxLength: 1000,
                            maxLines: 5,
                            decoration: InputDecoration(
                              hintText:
                              SCAppLocalizations.of(context)!.reportInputTips,
                              hintStyle: TextStyle(
                                color: SCGlobalConfig.businessLogicStrategy.getReportPageSecondaryHintTextColor(),
                                fontSize: 14.sp,
                              ),
                              contentPadding: EdgeInsets.only(top: 0.w),
                              isDense: true,
                              filled: false,
                              focusColor: Colors.transparent,
                              hoverColor: Colors.transparent,
                              border: InputBorder.none,
                              enabledBorder: InputBorder.none,
                              focusedBorder: InputBorder.none,
                              disabledBorder: InputBorder.none,
                              errorBorder: InputBorder.none,
                              focusedErrorBorder: InputBorder.none,
                              // enabledBorder: UnderlineInputBorder(
                              //   borderSide: BorderSide(width: 0.5,color: Colors.white,style: BorderStyle.solid),),
                              // focusedBorder: UnderlineInputBorder(
                              //   borderSide: BorderSide(width: 0.5,color: Colors.white,style: BorderStyle.solid),),
                              // prefixIcon: Padding(padding: EdgeInsets.all(8.w), child: Image.asset("images/login/sc_icon_phone.png",width: 20.w, height: 20.w,fit: BoxFit.fill,),),
                              fillColor: Colors.white54,
                              counterStyle: TextStyle(color: Colors.white)
                            ),
                            style: TextStyle(
                              fontSize: sp(15),
                              color: SCGlobalConfig.businessLogicStrategy.getReportPagePrimaryTextColor(),
                              textBaseline: TextBaseline.alphabetic,
                            ),
                          ),
                        ),
                        SizedBox(height: 5.w),
                        Row(
                          children: [
                            text(
                              SCAppLocalizations.of(context)!.screenshotTips,
                              fontSize: 14.sp,
                              textColor: SCGlobalConfig.businessLogicStrategy.getReportPagePrimaryTextColor(),
                            ),
                            Spacer(),
                          ],
                        ),
                        SizedBox(height: 8.w),
                        Row(
                          children: [
                            Expanded(
                              child: GestureDetector(
                                child: Stack(
                                  children: [
                                    pic01.isNotEmpty
                                        ? CustomCachedImage(
                                      imageUrl: pic01,
                                      height: 100.w,
                                    )
                                        : Image.asset(
                                      SCGlobalConfig.businessLogicStrategy.getReportPageIcon('addPic'),
                                      height: 100.w,
                                    ),
                                    pic01.isNotEmpty
                                        ? Positioned(
                                      top: 5.w,
                                      right: 5.w,
                                      child: GestureDetector(
                                        child: Image.asset(
                                          SCGlobalConfig.businessLogicStrategy.getReportPageIcon('closePic'),
                                          width: 14.w,
                                          height: 14.w,
                                        ),
                                        onTap: () {
                                          setState(() {
                                            pic01 = "";
                                          });
                                        },
                                      ),
                                    )
                                        : Container(),
                                  ],
                                ),
                                onTap: () {
                                  SCPickUtils.pickImage(context, (
                                      bool success,
                                      String url,
                                      ) {
                                    if (success) {
                                      setState(() {
                                        pic01 = url;
                                      });
                                    }
                                  });
                                },
                              ),
                            ),
                            SizedBox(width: 5.w),
                            Expanded(
                              child: GestureDetector(
                                child: Stack(
                                  children: [
                                    pic02.isNotEmpty
                                        ? CustomCachedImage(
                                      imageUrl: pic02,
                                      height: 100.w,
                                    )
                                        : Image.asset(
                                      SCGlobalConfig.businessLogicStrategy.getReportPageIcon('addPic'),
                                      height: 100.w,
                                    ),
                                    pic02.isNotEmpty
                                        ? Positioned(
                                      top: 5.w,
                                      right: 5.w,
                                      child: GestureDetector(
                                        child: Image.asset(
                                          SCGlobalConfig.businessLogicStrategy.getReportPageIcon('closePic'),
                                          width: 14.w,
                                          height: 14.w,
                                        ),
                                        onTap: () {
                                          setState(() {
                                            pic02 = "";
                                          });
                                        },
                                      ),
                                    )
                                        : Container(),
                                  ],
                                ),
                                onTap: () {
                                  SCPickUtils.pickImage(context, (
                                      bool success,
                                      String url,
                                      ) {
                                    if (success) {
                                      setState(() {
                                        pic02 = url;
                                      });
                                    }
                                  });
                                },
                              ),
                            ),
                            SizedBox(width: 5.w),
                            Expanded(
                              child: GestureDetector(
                                child: Stack(
                                  children: [
                                    pic03.isNotEmpty
                                        ? CustomCachedImage(
                                      imageUrl: pic03,
                                      height: 100.w,
                                    )
                                        : Image.asset(
                                      SCGlobalConfig.businessLogicStrategy.getReportPageIcon('addPic'),
                                      height: 100.w,
                                    ),
                                    pic03.isNotEmpty
                                        ? Positioned(
                                      top: 5.w,
                                      right: 5.w,
                                      child: GestureDetector(
                                        child: Image.asset(
                                          SCGlobalConfig.businessLogicStrategy.getReportPageIcon('closePic'),
                                          width: 14.w,
                                          height: 14.w,
                                        ),
                                        onTap: () {
                                          setState(() {
                                            pic03 = "";
                                          });
                                        },
                                      ),
                                    )
                                        : Container(),
                                  ],
                                ),
                                onTap: () {
                                  SCPickUtils.pickImage(context, (
                                      bool success,
                                      String url,
                                      ) {
                                    if (success) {
                                      setState(() {
                                        pic03 = url;
                                      });
                                    }
                                  });
                                },
                              ),
                            ),
                          ],
                        ),
                        SizedBox(height: 15.w),
                      ],
                    ),
                  ),
                  SizedBox(height: 45.w),
                  SCDebounceWidget(
                    onTap: () async {
                      SCLoadingManager.show();
                      String imageUrls = "";
                      if (pic01.isNotEmpty) {
                        imageUrls = "$imageUrls,$pic01";
                      }
                      if (pic02.isNotEmpty) {
                        imageUrls = "$imageUrls,$pic02";
                      }
                      if (pic03.isNotEmpty) {
                        imageUrls = "$imageUrls,$pic03";
                      }
                      SCChatRoomRepository()
                          .reported(
                        AccountStorage().getCurrentUser()?.userProfile?.id ??
                            "",
                        widget.tageId,
                        selectedIndex,
                        reportedContent: _descriptionController.text,
                        imageUrls: imageUrls,
                      )
                          .then((result) {
                        SCLoadingManager.hide();
                        SCTts.show(
                          SCAppLocalizations.of(context)!.reportSucc,
                        );
                        Navigator.of(context).pop();
                      })
                          .catchError((_) {
                        SCLoadingManager.hide();
                      });

                    },
                    child: Container(
                      height: 40.w,
                      width: double.infinity,
                      alignment: Alignment.center,
                      margin: EdgeInsets.symmetric(horizontal: 35.w),
                      decoration: BoxDecoration(
                        color: SCGlobalConfig.businessLogicStrategy.getReportPageSubmitButtonBackgroundColor(),
                        borderRadius: BorderRadius.circular(25.w),
                      ),
                      child: Text(
                        SCAppLocalizations.of(context)!.submit,
                        style: TextStyle(color: SCGlobalConfig.businessLogicStrategy.getReportPageSubmitButtonTextColor(), fontSize: 16.sp),
                      ),
                    ),
                  ),
                  SizedBox(height: 35.w),
                ],
              ),
            ),
          ),
        ),
      ],
    ), onTap: (){
      FocusScope.of(context).unfocus();
    });
  }

  Widget _item(String str, int index) {
    return GestureDetector(
      behavior: HitTestBehavior.opaque,
      onTap: () {
        FocusScope.of(context).unfocus(); // 彻底取消当前上下文的所有焦点
        setState(() {
          reportReason = str;
          selectedIndex = index;
        });
      },
      child: Container(
        width: double.infinity,
        height: 40.w,
        child: Row(
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            text(str, fontSize: 14.sp, textColor: SCGlobalConfig.businessLogicStrategy.getReportPagePrimaryTextColor()),
            Spacer(),
            selectedIndex == index
                ? Image.asset(
                  SCGlobalConfig.businessLogicStrategy.getReportPageIcon('checked'),
                  width: 20.w,
                  fit: BoxFit.fitWidth,
                )
                : Container(
                  width: 20.w,
                  height: 20.w,
                  decoration: BoxDecoration(
                    shape: BoxShape.circle,
                    border: Border.all(color: SCGlobalConfig.businessLogicStrategy.getReportPageUnselectedBorderColor(), width: 2.w),
                  ),
                ),
          ],
        ),
      ),
    );
  }
}
