import 'package:flutter/material.dart';
import 'package:file_picker/file_picker.dart';
import 'package:image_picker/image_picker.dart';
import '../services/app_strings.dart';
import '../services/license_service.dart';

class HomeScreen extends StatefulWidget { const HomeScreen({super.key}); @override State<HomeScreen> createState()=>_HomeScreenState(); }
class _HomeScreenState extends State<HomeScreen>{
  String lang='ru'; final license=LicenseService(); int selected=0; String status='';
  final login=TextEditingController(), pass=TextEditingController(), fio=TextEditingController(), phone=TextEditingController(), email=TextEditingController();
  String tr(String k)=>S.t[lang]![k] ?? k;
  Future<void> pickFiles() async { final r=await FilePicker.platform.pickFiles(allowMultiple:true, type:FileType.any); setState(()=>selected=r?.files.length??0); }
  Future<void> takePhoto() async { final x=await ImagePicker().pickImage(source:ImageSource.camera); setState(()=>selected=x==null?selected:selected+1); }
  Future<void> analyze() async { if(!await license.canAnalyze()){setState(()=>status='Demo limit exceeded');return;} await license.registerCheck(); final exp=await license.canExportChecklist(); setState(()=>status= exp ? 'Analysis complete. Checklist export enabled.' : 'Demo analysis complete. Checklist export disabled.'); }
  @override Widget build(BuildContext context){return Scaffold(appBar:AppBar(title:Text(tr('title'))),body:ListView(padding:const EdgeInsets.all(16),children:[
    DropdownButton<String>(value:lang,items:const [DropdownMenuItem(value:'ru',child:Text('Русский')),DropdownMenuItem(value:'en',child:Text('English')),DropdownMenuItem(value:'zh',child:Text('中文'))],onChanged:(v)=>setState(()=>lang=v??'ru')),
    Text(tr('login'),style:Theme.of(context).textTheme.titleLarge), TextField(controller:login,decoration:const InputDecoration(labelText:'Login')), TextField(controller:pass,decoration:const InputDecoration(labelText:'Password'),obscureText:true), TextField(controller:fio,decoration:const InputDecoration(labelText:'Full name')), TextField(controller:phone,decoration:const InputDecoration(labelText:'Phone')), TextField(controller:email,decoration:const InputDecoration(labelText:'E-mail')),
    const SizedBox(height:16), Text(tr('demo')), const SizedBox(height:8), Wrap(spacing:8,children:[ElevatedButton(onPressed:pickFiles,child:Text(tr('pick'))), ElevatedButton(onPressed:takePhoto,child:Text(tr('camera'))), ElevatedButton(onPressed:analyze,child:Text(tr('analyze')))]),
    const SizedBox(height:16), Text('Selected files: $selected'), Text(status), const Divider(), const Text('Developer: www.kurpishev.com | me@kurpishev.ru | tel +789128325861')
  ]));}
}
