import 'package:flutter/material.dart';
import 'package:initial_folder/base_service.dart';
import 'package:initial_folder/providers/detail_course_provider.dart';
import 'package:initial_folder/providers/instructor_provider.dart';
import 'package:initial_folder/providers/lesson_course_provider.dart';
import 'package:initial_folder/providers/my_course_provider.dart'
    as myCourseProvider;
import 'package:initial_folder/providers/my_course_provider.dart';
import 'package:initial_folder/screens/course/play_course_page.dart';
import 'package:initial_folder/screens/home/components/body_comp/latest_course.dart';
import 'package:initial_folder/screens/home/components/home_page.dart';
import 'package:initial_folder/screens/home/home_screen.dart';
import 'package:initial_folder/services/course_service.dart';
import 'package:initial_folder/services/instructor_service.dart';
import 'package:initial_folder/services/lesson_course_service.dart';
// import 'package:initial_folder/models/course_model.dart';
import 'package:initial_folder/size_config.dart';
import 'package:initial_folder/theme.dart';
import 'package:initial_folder/widgets/login_regist/default_button.dart';
import 'package:initial_folder/widgets/login_regist/default_button_payment.dart';
import 'package:provider/provider.dart';

class SuccessFreeCourse extends StatelessWidget {
  const SuccessFreeCourse({
    Key? key,
    this.title,
    this.id,
    this.thumbnail,
    this.instructor,
  }) : super(key: key);

  final String? instructor, id, thumbnail, title;
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Theme.of(context).colorScheme.background,
        elevation: 0.0,
        // leadingWidth: 14,
        leading: IconButton(
          icon: Icon(Icons.arrow_back),
          onPressed: () async {
            // print(id);
            // // await Provider.of<DetailCourseProvider>(context, listen: false)
            // //     .getDetailCourseLogin(id);
            // await Provider.of<myCourseProvider.MyCourseProvider>(context,
            //         listen: false)
            //     .getMyCourse();
            // Navigator.pop(context, true); // Mengirim true sebagai nilai balik
            // // print('object');
            Navigator.pushAndRemoveUntil(
              context,
              MaterialPageRoute(builder: (context) => HomeScreen()),
              (Route<dynamic> route) => false,
            );
          },
        ),
      ),
      body: ListView(
        children: [
          SizedBox(height: getProportionateScreenHeight(8)),
          Image.asset(
            "assets/images/success_pay.png",
            width: getProportionateScreenWidth(100),
            height: getProportionateScreenHeight(100),
          ),
          SizedBox(height: getProportionateScreenHeight(15)),
          Text(
            'Pembayaran Berhasil',
            style: thirdTextStyle.copyWith(
              fontWeight: semiBold,
              fontSize: getProportionateScreenWidth(14),
            ),
            textAlign: TextAlign.center,
          ),
          SizedBox(height: getProportionateScreenHeight(15)),
          Container(
            margin: EdgeInsets.only(left: 16, right: 16),
            width: SizeConfig.screenWidth,
            height: getProportionateScreenHeight(169),
            decoration: BoxDecoration(
              borderRadius: BorderRadius.circular(5),
            ),
            child: Column(
              children: [
                Container(
                  width: getProportionateScreenWidth(278),
                  height: getProportionateScreenHeight(145),
                  decoration: BoxDecoration(
                    borderRadius: BorderRadius.circular(5),
                    image: DecorationImage(
                        image: NetworkImage(thumbnail ??
                            '$baseUrl/uploads/courses_thumbnail/course_thumbnail_default_57.jpg'),
                        fit: BoxFit.cover),
                  ),
                ),
              ],
            ),
          ),
          Container(
            margin: EdgeInsets.only(
              bottom: getProportionateScreenHeight(8),
            ),
            child: Center(
              child: Text(
                title ?? '',
                style: thirdTextStyle.copyWith(
                  fontSize: getProportionateScreenWidth(14),
                  letterSpacing: 0.2,
                ),
                textAlign: TextAlign.center,
              ),
            ),
          ),
          Container(
            child: Center(
              child: Text(
                'Oleh $instructor ',
                style: thirdTextStyle.copyWith(
                  fontSize: getProportionateScreenWidth(11),
                  letterSpacing: 0.5,
                  fontFamily: "Poppins",
                ),
                textAlign: TextAlign.center,
              ),
            ),
          ),
          SizedBox(
            height: getProportionateScreenHeight(20),
          ),
          Container(
            margin: EdgeInsets.only(left: 16, right: 16),
            child: DefaultButtonPayment(
              width: double.infinity,
              height: 35,
              text: 'Mulai Kursus',
              press: () {
                Navigator.pushReplacement(
                  context,
                  MaterialPageRoute(
                    builder: (context) => MultiProvider(
                      providers: [
                        ChangeNotifierProvider(
                          create: (context) => LessonCourseProvider(
                            lessonCourseService: LessonCourseService(),
                            id: int.parse(id ?? '0'),
                          ),
                        ),
                        ChangeNotifierProvider(
                            create: (context) => DetailCourseProvider(
                                courseService: CourseService(), id: id ?? '1')),
                        ChangeNotifierProvider(
                          create: (context) => InstructorProvider(
                              instructorService: InstructorService(),
                              id: int.parse(instructor!)),
                        ),
                        ChangeNotifierProvider(
                          create: (context) =>
                              MyCourseProvider(courseService: CourseService()),
                        ),
                      ],
                      child: PlayCourse(
                        judul: title ?? '',
                        instruktur: instructor ?? '',
                        thumbnail: thumbnail ??
                            '$baseUrl/uploads/courses_thumbnail/course_thumbnail_default_57.jpg',
                        courseeid: id ?? '',
                      ),
                    ),
                  ),
                );
              },
            ),
          ),
          SizedBox(
            height: getProportionateScreenHeight(20),
          ),
          LatestCourse(text: "Kursus Terbaru"),
        ],
      ),
    );
  }
}
