import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:webview_flutter/webview_flutter.dart';

import 'package:http/http.dart' as http;
import 'package:login_ui/Feature/Login%20Screen/Login_Screen.dart';
import 'package:login_ui/Feature/home/Response.dart';

class HomePage extends StatefulWidget {
  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  final List<String> imagePaths = [
    'assets/1.jpeg',
    'assets/2.jpeg',
    'assets/3.jpeg',
    'assets/4.jpeg',
    'assets/5.jpeg',
    'assets/6.jpeg',
    'assets/7.jpeg',
    'assets/8.jpeg'
  ];

  List<String> filteredImagePaths = [];
  bool isSearching = false;

  Map<int, String> imageData = {
    0: 'HDFCBANK.NS',
    1: 'ICIC',
    2: 'data for image 3',
    3: 'data for image 4',
    4: 'data for image 5',
    5: 'data for image 6',
    6: 'data for image 7',
    7: 'data for image 8',
  };

  TextEditingController _searchController = TextEditingController();

  @override
  void initState() {
    super.initState();
    filteredImagePaths = imagePaths;
  }
  Future<void> _sendData(BuildContext context, String symbol) async {
    try {
      var url = Uri.parse('https://36d3-203-109-66-92.in.ngrok.io/hello');
      var response = await http.post(
        url,
        headers: <String, String>{
          'Content-Type': 'application/json; charset=UTF-8',
        },
        body: jsonEncode(<String, String>{'symbol': symbol}),
      );

      print('Data sent: $symbol');
      print('Response status code: ${response.statusCode}');
      print('Response body: ${response.body}');

      Map<String, dynamic> graphData = jsonDecode(response.body);
      String graphUrl = graphData['url'];
      await Navigator.of(context).push(
        MaterialPageRoute(
          builder: (_) => Scaffold(
            appBar: AppBar(
              title: Text(symbol),
            ),
            body: WebView(
              initialUrl: graphUrl,
              javascriptMode: JavascriptMode.unrestricted,
            ),
          ),
        ),
      );
    } catch (e) {
      print('Error: $e');
    }
  }

  WebView? _webView;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: !isSearching
            ? Text('Stocks')
            : TextField(
          controller: _searchController,
          decoration: InputDecoration(
            hintText: 'Search',
            icon: Icon(Icons.search),
          ),
          onSubmitted: (value) {
            _sendData(context, value);
          },
        ),
        actions: [
          IconButton(
            icon: Icon(Icons.search),
            onPressed: () {
              setState(() {
                isSearching = !isSearching;
                filteredImagePaths = imagePaths;
              });
            },
          ),
        ],
      ),
      body: Stack(
        children: [
          Container(
            decoration: BoxDecoration(
              image: DecorationImage(
                image: AssetImage('assets/theme.jpg'),
                fit: BoxFit.cover,
              ),
            ),
            child: GridView.count(
              crossAxisCount: 2,
              padding: EdgeInsets.all(8),
              mainAxisSpacing: 8,
              crossAxisSpacing: 8,
              children: List.generate(filteredImagePaths.length, (index) {
                return GestureDetector(
                  onTap: () async {
                    String symbol = imageData[index]!;
                    await _sendData(context, symbol);
                  },

                  child: ClipRRect(
                    borderRadius: BorderRadius.circular(8),
                    child: Stack(
                      fit: StackFit.expand,
                      children: [
                        Image.asset(
                          filteredImagePaths[index],
                          fit: BoxFit.cover,
                        ),
                        Container(
                          decoration: BoxDecoration(
                            gradient: LinearGradient(
                              begin: Alignment.bottomCenter,
                              end: Alignment.topCenter,
                              colors: [
                                Colors.black.withOpacity(0.8),
                                Colors.transparent
                              ],
                            ),
                          ),
                        ),
                      ],
                    ),
                  ),
                );
              }),
            ),
          ),
          if (_webView != null)
            Positioned.fill(
              child: Column(
                children: [
                  Expanded(
                    child: _webView!,
                  ),
                  ElevatedButton(
                    onPressed: () {
                      setState(() {
                        _webView = null;
                      });
                    },
                    child: Text('Close Graph'),
                  ),
                ],
              ),
            ),
        ],
      ),
    );
  }
}
cant see graph solve it