import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_svg/svg.dart';
import 'package:get/get.dart';
import 'package:glassmorphism/glassmorphism.dart';
import 'package:traderscircuit/Utils/Common/CommonAppBar.dart';
import 'package:traderscircuit/Utils/Common/CommonBottomNavigation.dart';
import 'package:traderscircuit/Utils/Common/CommonTabBar.dart';
import 'package:traderscircuit/Utils/Common/commonBotton.dart';
import 'package:traderscircuit/Utils/Common/comonGlassmorphicContainer.dart';
import 'package:traderscircuit/Utils/Common/sized_box.dart';
import 'package:traderscircuit/Utils/text.dart';
import 'package:traderscircuit/view/MainScreen/MainScreen.dart';
import 'package:traderscircuit/view/Sidemenu/Sidemenu.dart';
import 'package:traderscircuit/view/onBoarding/splashScreen1.dart';

class ShortTrade extends StatefulWidget {
  const ShortTrade({super.key});

  @override
  State<ShortTrade> createState() => _ShortTradeState();
}

class _ShortTradeState extends State<ShortTrade> {
  GlobalKey<ScaffoldState> _scaffoldKey1 = GlobalKey<ScaffoldState>();
  List<String> containerTexts = ["Swing Trade", "Multibagger", "Options"];
  final selectedIndex = 0.obs;
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      key: _scaffoldKey1,
      backgroundColor: Colors.black,
      drawer: Container(width: 320.w, child: SideMenu()),
      extendBody: true,
      appBar: AppBar(
        scrolledUnderElevation: 0.0,
        backgroundColor: Colors.black,
        elevation: 0,
        automaticallyImplyLeading: false,
        titleSpacing: 0,
        leading: InkWell(
          onTap: () {
            _scaffoldKey1.currentState?.openDrawer();
          },
          child: Center(
            child: Image.asset(
              'assets/images/png/menu.png',
              height: 15.h,
              width: 20.w,
            ),
          ),
        ),
      ),

      // CommonAppbar(
      //   titleTxt: '',
      //   // preferredSize: Size.fromHeight(60.h),
      //   showLeading: false,
      //   customleading: InkWell(
      //     onTap: () {
      //       _scaffoldKey1.currentState?.openDrawer();
      //     },
      //     child: Center(
      //       child: Image.asset(
      //         'assets/images/png/menu.png',
      //         height: 15.h,
      //         width: 20.w,
      //       ),
      //     ),
      //     // Icon(
      //     //   Icons.menu,
      //     //   color: Colors.white,
      //     //   size: 27.sp,
      //     // ),
      //   ),
      // ),
      body: Stack(
        children: [
          CommonBlurLeft(),
          CommonBlurRight(),
          Stack(children: [
            Padding(
                padding: EdgeInsets.symmetric(horizontal: 16, vertical: 16),
                child: ListView(physics: BouncingScrollPhysics(), children: [
                  sizedBoxHeight(15.h),
                  SizedBox(
                    height: 60,
                    width: double.infinity,
                    // color: Colors.amber,
                    child: ListView.builder(
                        shrinkWrap: true,
                        scrollDirection: Axis.horizontal,
                        itemCount: containerTexts.length,
                        itemBuilder: (context, index) {
                          return GestureDetector(
                            onTap: () {
                              selectedIndex.value = index;
                            },
                            child: Row(
                              children: [
                                topContainer(containerTexts[index], index),
                                sizedBoxWidth(10.w)
                              ],
                            ),
                          );
                        }),
                  ),
                  sizedBoxHeight(20.h),
                  DefaultTabController(
                    length: 2,
                    // initialIndex: selectedIndex.value,
                    child: Column(
                      children: [
                        MyTabBar(),
                        SizedBox(
                          height: 700.h,
                          child: TabBarView(
                            children: [
                              ActiveCallsTab(),
                              ExitedCallsTab(),
                            ],
                          ),
                        ),
                      ],
                    ),
                  ),
                  sizedBoxHeight(20.h),
                ]))
          ])
        ],
      ),
      bottomNavigationBar: bottomnavigationbar(mainController),
    );
  }

  Widget ActiveCallsTab() {
    return Obx(() {
      WidgetsBinding.instance.addPostFrameCallback((_) {
        if (selectedIndex == 1) _unlockbottomsheet();
      });
      return selectedIndex == 0
          ? Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                sizedBoxHeight(20.h),
                cardSwingWidget(
                    text: 'Indiabulls Housing Finance Ltd',
                    amount: '₹ 196.50 - ₹ 197',
                    targetamount: '₹ 204',
                    stoploss: '₹ 190',
                    time: '4-5 Days'),
                sizedBoxHeight(25.h),
                text22W600('Content Bytes'),
                SingleChildScrollView(
                  scrollDirection: Axis.horizontal,
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.end,
                    children: [
                      text16W400_DADADA('The Beauty and Power of Video'),
                      sizedBoxWidth(10.w),
                      Container(
                        height: 35.h,
                        width: 105.w,
                        decoration: BoxDecoration(
                            color: Color(0xFF3A3A3A).withOpacity(0.6),
                            borderRadius: BorderRadius.circular(5.r),
                            border: Border.all(
                              color: Color(0xFF3A3A3A),
                            )),
                        child: Center(child: text16W500('View More')),
                      ),
                    ],
                  ),
                ),
                sizedBoxHeight(20.h),
                commonGlassContainer(
                  borderradius: 8,
                  width: double.infinity,
                  height: 300.h,
                  customWidget: Padding(
                    padding:
                        EdgeInsets.symmetric(vertical: 10.h, horizontal: 10.w),
                    child: Column(
                      children: [
                        Container(
                          height: 200.h,
                          width: double.infinity,
                          decoration: BoxDecoration(
                              borderRadius: BorderRadius.circular(8.r),
                              image: DecorationImage(
                                  image: AssetImage(
                                      'assets/images/png/Rectangle 17934.png'))),
                          child: Center(
                            child: SvgPicture.asset(
                              'assets/images/svg/gridicons_play.svg',
                              height: 56.h,
                              width: 56.w,
                            ),
                          ),
                        ),
                        sizedBoxHeight(20.h),
                        Row(
                          children: [
                            CircleAvatar(
                              radius: 23.r,
                              backgroundImage: AssetImage(
                                  'assets/images/png/Ellipse 1494.png'),
                            ),
                            sizedBoxWidth(10.w),
                            Column(
                              crossAxisAlignment: CrossAxisAlignment.start,
                              children: [
                                text18W500('Week of 21st February 2024'),
                                // sizedBoxHeight(10.h),
                                text12W400_979797('20k views . 2 days ago'),
                              ],
                            )
                          ],
                        )
                      ],
                    ),
                  ),
                ),
              ],
            )
          : selectedIndex == 1
              ? Column(
                  children: [],
                )
              : Column(
                  children: [],
                );
    });
  }

  void _unlockbottomsheet() {
    showModalBottomSheet(
      // isDismissible: false,
      // enableDrag: false,
      backgroundColor: Colors.black.withOpacity(0.3),
      context: context,
      builder: (BuildContext context) {
        return commonGlassContainer(
          width: double.infinity,
          height: 500,
          borderradius: 0,
          customWidget: SizedBox(
            height: 100,
            child: Column(
              mainAxisAlignment: MainAxisAlignment.start,
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                // Image.asset(
                //   'assets/images/png/Group 1000003722.png',
                //   height: 100.h,
                // ),
                // sizedBoxHeight(25.h),
                text20W400('Please subscribed to unlock'),
                sizedBoxHeight(30.h),
                CommonBtn(text: 'Subscribe Now'),
                SizedBox(
                  height: 100,
                )
              ],
            ),
          ),
        );
      },
    );
  }

  Widget ExitedCallsTab() {
    List<Map<String, String>> cardSwing = [
      {
        'text': 'Indiabulls Housing Finance Ltd',
        'amount': '₹ 196.50 - ₹ 197',
        'targetamount': '₹ 204',
        'stoploss': '₹ 190',
        'time': '4-5 Days',
      },
      {
        'text': 'Indiabulls Housing Finance Ltd',
        'amount': '₹ 196.50 - ₹ 197',
        'targetamount': '₹ 204',
        'stoploss': '₹ 190',
        'time': '4-5 Days',
      },
      {
        'text': 'Indiabulls Housing Finance Ltd',
        'amount': '₹ 196.50 - ₹ 197',
        'targetamount': '₹ 204',
        'stoploss': '₹ 190',
        'time': '4-5 Days',
      },
    ];

    List<Map<String, String>> cardMultibagger = [
      {
        'text': 'TATA MOTORS',
        'price': '₹ 453 - ₹234',
        'date': '25 March 2024',
        'returns': '103%',
        'stoploss': '₹ 198',
        'duration': '271 Days',
        'pdfname': 'Download Pdf'
      },
      {
        'text': 'TATA MOTORS',
        'price': '₹ 453 - ₹234',
        'date': '25 March 2024',
        'returns': '103%',
        'stoploss': '₹ 198',
        'duration': '271 Days',
        'pdfname': 'Download Pdf'
      },
      {
        'text': 'TATA MOTORS',
        'price': '₹ 453 - ₹234',
        'date': '25 March 2024',
        'returns': '103%',
        'stoploss': '₹ 198',
        'duration': '271 Days',
        'pdfname': 'Download Pdf'
      },
    ];

    List<Map<String, String>> cardOptionsbagger = [
      {
        'text': 'NIFTY/ BANK NIFTY',
        'optiontype': 'Call',
        'price': '₹ 46500',
        'date': '6th Mar',
        'premium': 'Buy above₹ 690',
        'price1': '₹ 740',
        'price2': '₹ 780',
        'stoploss': '₹ 645',
      },
      {
        'text': 'NIFTY/ BANK NIFTY',
        'optiontype': 'Call',
        'price': '₹ 46500',
        'date': '6th Mar',
        'premium': 'Buy above₹ 690',
        'price1': '₹ 740',
        'price2': '₹ 780',
        'stoploss': '₹ 645',
      },
      {
        'text': 'NIFTY/ BANK NIFTY',
        'optiontype': 'Call',
        'price': '₹ 46500',
        'date': '6th Mar',
        'premium': 'Buy above₹ 690',
        'price1': '₹ 740',
        'price2': '₹ 780',
        'stoploss': '₹ 645',
      },
    ];

    return SingleChildScrollView(
      child: Column(
        children: [
          sizedBoxHeight(20.h),
          Obx(() {
            return selectedIndex == 0
                ? Column(
                    children: List.generate(cardSwing.length, (index) {
                      return Column(
                        children: [
                          cardSwingWidget(
                              text: cardSwing[index]['text']!,
                              amount: cardSwing[index]['amount']!,
                              targetamount: cardSwing[index]['targetamount']!,
                              stoploss: cardSwing[index]['stoploss']!,
                              time: cardSwing[index]['time']!),
                          sizedBoxHeight(20.h)
                        ],
                      );
                    }),
                  )
                : selectedIndex == 1
                    ? Column(
                        children:
                            List.generate(cardMultibagger.length, (index) {
                          return Column(
                            children: [
                              cardMultibaggerWidget(
                                  text: cardMultibagger[index]['text']!,
                                  price: cardMultibagger[index]['price']!,
                                  date: cardMultibagger[index]['date']!,
                                  returns: cardMultibagger[index]['returns']!,
                                  stoploss: cardMultibagger[index]['stoploss']!,
                                  duration: cardMultibagger[index]['duration']!,
                                  pdfname: cardMultibagger[index]['pdfname']!),
                              sizedBoxHeight(20.h)
                            ],
                          );
                        }),
                      )
                    : Column(
                        children:
                            List.generate(cardMultibagger.length, (index) {
                          return Column(
                            children: [
                              cardOptionWidget(
                                  text: cardOptionsbagger[index]['text']!,
                                  optiontype: cardOptionsbagger[index]
                                      ['optiontype']!,
                                  price: cardOptionsbagger[index]['price']!,
                                  date: cardOptionsbagger[index]['date']!,
                                  premium: cardOptionsbagger[index]['premium']!,
                                  price1: cardOptionsbagger[index]['price1']!,
                                  price2: cardOptionsbagger[index]['price2']!,
                                  stoploss: cardOptionsbagger[index]
                                      ['stoploss']!),
                              sizedBoxHeight(20.h)
                            ],
                          );
                        }),
                      );
          }),
          sizedBoxHeight(200.h)
        ],
      ),
    );
  }

  Widget cardOptionWidget({
    required String text,
    required String optiontype,
    required String price,
    required String date,
    required String premium,
    required String price1,
    required String price2,
    required String stoploss,
  }) {
    return commonGlassContainer(
      width: double.infinity,
      height: 380.h,
      borderradius: 8,
      customWidget: Column(
        children: [
          Padding(
            padding: EdgeInsets.symmetric(vertical: 16.h, horizontal: 16.w),
            child: Row(
              children: [
                GlassmorphicContainer(
                  width: 47.w,
                  height: 47.h,
                  borderRadius: 100,
                  blur: 10,
                  alignment: Alignment.center,
                  border: 0.9,
                  linearGradient: LinearGradient(
                    begin: Alignment.topLeft,
                    end: Alignment.bottomRight,
                    colors: [
                      Color(0xff3A3A3A),
                      Color(0xFF3A3A3A),
                    ],
                  ),
                  borderGradient: LinearGradient(
                    begin: Alignment.topLeft,
                    end: Alignment.bottomRight,
                    colors: [
                      Color.fromRGBO(70, 5, 1, 0.8),
                      Color.fromRGBO(102, 102, 102, 0.8),
                    ],
                  ),
                  child: Center(
                    child: Image.asset(
                      'assets/images/png/Group 1000004495.png',
                      width: 26.w,
                      height: 23.h,
                    ),
                  ),
                ),
                sizedBoxWidth(15.w),
                SizedBox(width: 200.w, child: text18W600(text)),
                Spacer(),
                Container(
                  width: 62.w,
                  height: 25.h,
                  decoration: BoxDecoration(
                    borderRadius: BorderRadius.circular(4.r),
                    color: Color(0xFFFFAD31),
                  ),
                  child: Center(child: text14W600_1B1B1B('Hold')),
                )
              ],
            ),
          ),
          Container(
            width: double.infinity,
            height: 1.h,
            color: Color(0xFF3A3A3A),
          ),
          Padding(
            padding: const EdgeInsets.all(18),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Row(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        text14W400_979797('Option Type'),
                        sizedBoxHeight(5.h),
                        SizedBox(width: 150.w, child: text15W600(optiontype))
                      ],
                    ),
                    sizedBoxWidth(30.w),
                    Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        text14W400_979797('Strike Price'),
                        sizedBoxHeight(5.h),
                        text15W600(price)
                      ],
                    )
                  ],
                ),
                sizedBoxHeight(15.h),
                Row(
                  children: [
                    Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        text14W400_979797('Expiry Date'),
                        sizedBoxHeight(5.h),
                        SizedBox(width: 150.w, child: text15W600(date))
                      ],
                    ),
                    sizedBoxWidth(30.w),
                    Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        text14W400_979797('Premium'),
                        sizedBoxHeight(5.h),
                        text15W600(premium)
                      ],
                    )
                  ],
                ),
                sizedBoxHeight(15.h),
                Row(
                  children: [
                    Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        text14W400_979797('Target Price 01'),
                        sizedBoxHeight(5.h),
                        SizedBox(width: 150.w, child: text15W600(price1))
                      ],
                    ),
                    sizedBoxWidth(30.w),
                    Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        text14W400_979797('Target Price 01'),
                        sizedBoxHeight(5.h),
                        text15W600(price2)
                      ],
                    )
                  ],
                ),
                sizedBoxHeight(15.h),
                Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    text14W400_979797('Stop Loss'),
                    sizedBoxHeight(5.h),
                    text15W600(stoploss)
                  ],
                )
              ],
            ),
          )
        ],
      ),
    );
  }

  Widget cardMultibaggerWidget({
    required String text,
    required String price,
    required String date,
    required String returns,
    required String stoploss,
    required String duration,
    required String pdfname,
  }) {
    return commonGlassContainer(
      width: double.infinity,
      height: 330.h,
      borderradius: 8,
      customWidget: Column(
        children: [
          Padding(
            padding: EdgeInsets.symmetric(vertical: 16.h, horizontal: 16.w),
            child: Row(
              children: [
                GlassmorphicContainer(
                  width: 47.w,
                  height: 47.h,
                  borderRadius: 100,
                  blur: 10,
                  alignment: Alignment.center,
                  border: 0.9,
                  linearGradient: LinearGradient(
                    begin: Alignment.topLeft,
                    end: Alignment.bottomRight,
                    colors: [
                      Color(0xff3A3A3A),
                      Color(0xFF3A3A3A),
                    ],
                  ),
                  borderGradient: LinearGradient(
                    begin: Alignment.topLeft,
                    end: Alignment.bottomRight,
                    colors: [
                      Color.fromRGBO(70, 5, 1, 0.8),
                      Color.fromRGBO(102, 102, 102, 0.8),
                    ],
                  ),
                  child: Center(
                    child: Image.asset(
                      'assets/images/png/TATAMOTORS.NS_BIG 1.png',
                      width: 26.w,
                      height: 23.h,
                    ),
                  ),
                ),
                sizedBoxWidth(15.w),
                SizedBox(width: 200.w, child: text18W600(text)),
                Spacer(),
                Container(
                  width: 62.w,
                  height: 25.h,
                  decoration: BoxDecoration(
                    borderRadius: BorderRadius.circular(4.r),
                    color: Color(0xFFFFAD31),
                  ),
                  child: Center(child: text14W600_1B1B1B('Hold')),
                )
              ],
            ),
          ),
          Container(
            width: double.infinity,
            height: 1.h,
            color: Color(0xFF3A3A3A),
          ),
          Padding(
            padding: const EdgeInsets.all(18),
            child: Column(
              children: [
                Row(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        text14W400_979797('Initial Entry Price'),
                        sizedBoxHeight(5.h),
                        SizedBox(width: 150.w, child: text15W600(price))
                      ],
                    ),
                    sizedBoxWidth(30.w),
                    Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        SizedBox(
                            width: 130.w,
                            child: text14W400_979797('Date of recommendation')),
                        sizedBoxHeight(5.h),
                        text15W600(date)
                      ],
                    )
                  ],
                ),
                sizedBoxHeight(15.h),
                Row(
                  children: [
                    Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        text14W400_979797('% Return'),
                        sizedBoxHeight(5.h),
                        SizedBox(
                            width: 150.w, child: text15W600_00FF19(returns))
                      ],
                    ),
                    sizedBoxWidth(30.w),
                    Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        text14W400_979797('Stop Loss'),
                        sizedBoxHeight(5.h),
                        text15W600(stoploss)
                      ],
                    )
                  ],
                ),
                sizedBoxHeight(15.h),
                Row(
                  children: [
                    Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        text14W400_979797('Duration'),
                        sizedBoxHeight(5.h),
                        SizedBox(width: 150.w, child: text15W600(duration))
                      ],
                    ),
                    sizedBoxWidth(30.w),
                    Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        text14W400_979797('Report'),
                        sizedBoxHeight(5.h),
                        Row(
                          children: [
                            Image.asset(
                              'assets/images/png/pdf.png',
                              height: 20.h,
                              width: 20.w,
                            ),
                            text15W600(pdfname),
                          ],
                        )
                      ],
                    )
                  ],
                ),
              ],
            ),
          )
        ],
      ),
    );
  }

  Widget cardSwingWidget({
    required String text,
    required String amount,
    required String targetamount,
    required String stoploss,
    required String time,
  }) {
    return commonGlassContainer(
      width: double.infinity,
      height: 255.h,
      borderradius: 8,
      customWidget: Column(
        children: [
          Padding(
            padding: EdgeInsets.symmetric(vertical: 16.h, horizontal: 16.w),
            child: Row(
              children: [
                GlassmorphicContainer(
                  width: 47.w,
                  height: 47.h,
                  borderRadius: 100,
                  blur: 10,
                  alignment: Alignment.center,
                  border: 0.9,
                  linearGradient: LinearGradient(
                    begin: Alignment.topLeft,
                    end: Alignment.bottomRight,
                    colors: [
                      Color(0xff3A3A3A),
                      Color(0xFF3A3A3A),
                    ],
                  ),
                  borderGradient: LinearGradient(
                    begin: Alignment.topLeft,
                    end: Alignment.bottomRight,
                    colors: [
                      Color.fromRGBO(70, 5, 1, 0.8),
                      Color.fromRGBO(102, 102, 102, 0.8),
                    ],
                  ),
                  child: Center(
                    child: Image.asset('assets/images/png/Teal.png'),
                  ),
                ),
                sizedBoxWidth(15.w),
                SizedBox(width: 200.w, child: text18W600(text)),
                Spacer(),
                Container(
                  width: 62.w,
                  height: 25.h,
                  decoration: BoxDecoration(
                    borderRadius: BorderRadius.circular(4.r),
                    color: Color(0xFFFFAD31),
                  ),
                  child: Center(child: text14W600_1B1B1B('Hold')),
                )
              ],
            ),
          ),
          Container(
            width: double.infinity,
            height: 1.h,
            color: Color(0xFF3A3A3A),
          ),
          Padding(
            padding: const EdgeInsets.all(20),
            child: Column(
              children: [
                Row(
                  children: [
                    Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        text14W400_979797('Entry Price'),
                        sizedBoxHeight(5.h),
                        SizedBox(width: 150.w, child: text15W600(amount))
                      ],
                    ),
                    sizedBoxWidth(50.w),
                    Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        text14W400_979797('Target Price'),
                        sizedBoxHeight(5.h),
                        text15W600(targetamount)
                      ],
                    )
                  ],
                ),
                sizedBoxHeight(15.h),
                Row(
                  children: [
                    Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        text14W400_979797('Stop Loss'),
                        sizedBoxHeight(5.h),
                        SizedBox(width: 150.w, child: text15W600(stoploss))
                      ],
                    ),
                    sizedBoxWidth(50.w),
                    Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        text14W400_979797('Time Horizon'),
                        sizedBoxHeight(5.h),
                        text15W600(time)
                      ],
                    )
                  ],
                ),
              ],
            ),
          )
        ],
      ),
    );
  }

  Widget topContainer(String text, int index) {
    return Obx(() {
      return selectedIndex.value == index
          ? Container(
              height: 40.h,
              width: 126.w,
              decoration: BoxDecoration(
                borderRadius: BorderRadius.circular(5),
                color: Color(0Xff6C0000),
              ),
              child: Center(child: text16W500(text)),
            )
          : commonGlassContainer(
              width: 126.w,
              height: 40.h,
              borderradius: 5,
              customWidget: Center(child: text16W400(text)),
            );
    });
  }
}
