import 'dart:convert';
import 'dart:io';
import 'package:flutter/foundation.dart';
import 'package:http/http.dart' as http;
//8008202992

class RemoteData {
  final String loginUrl          = 'http://vaman.plutokm.com/login';
  final String imageupload       = 'http://vaman.plutokm.com/vasthram_mobile';
  final String identifystylecode = 'https://vaman.plutokm.com/get_vasthram_style_code';
  final String registerstylecode = 'https://vaman.plutokm.com/register_style_code';
  final String detectattributes  = 'https://vaman.plutokm.com/detect_attributes';
  final String sale              = 'https://vaman.plutokm.com/stylecode_sales_data';
  final String stock             = 'https://vaman.plutokm.com/stylecode_stock_data';
  final String type              = 'Content-Type';
  final String appjson           = 'application/json';

  Future<String> getLoginRequest(
      String loginid, String pass, String token) async {
    Map<String, dynamic> body = {
      'login_id': loginid,
      'password': pass,
      'token': token,
      'os': Platform.isAndroid
          ? 'Android'
          : Platform.isIOS
              ? 'IOS'
              : 'Unknown',
    };
    if (kDebugMode) {
      print(body);
    }
    var client = http.Client();
    var res = await client.post(Uri.parse(loginUrl),
        headers: {type: appjson}, body: json.encode(body));
    if (res.statusCode == 200) {
      return res.body;
    } else {
      throw Exception('Status code was wrong$res.statusCode');
    }
  }

  Future<String> stockData(String stylecode) async {
    print(stylecode);
    try {
      final request = http.MultipartRequest('POST', Uri.parse(stock));

      request.files.add(http.MultipartFile.fromString('stylecode', stylecode));
      //request.files.add(await http.MultipartFile.fromPath('embedding', embedding));
      var response = await request.send();
      var responseString = await response.stream.bytesToString();
      if (kDebugMode) {
        print('Response   :$responseString');
      }
      print(response.statusCode);
      if (response.statusCode == 200) {
        return responseString;
      } else {
        return responseString;
      }
    } catch (e) {
      if (kDebugMode) {
        print(e);
      }
    }
    return '';
  }

  Future<String> saleData(String stylecode) async {
    print(stylecode);
    try {
      final request = http.MultipartRequest('POST', Uri.parse(sale));
      request.files.add(http.MultipartFile.fromString('stylecode', stylecode));
      //request.files.add(await http.MultipartFile.fromPath('embedding', embedding));
      var response = await request.send();
      var responseString = await response.stream.bytesToString();
      if (kDebugMode) {
        print('Response   :$responseString');
      }
      print(response.statusCode);
      if (response.statusCode == 200) {
        return responseString;
      } else {
        return responseString;
      }
    } catch (e) {
      if (kDebugMode) {
        print(e);
      }
    }
    return '';
  }

  Future<String?> uploadimage(File imagepath, String embedding) async {
    try {
      final request = http.MultipartRequest(
          'POST', Uri.parse('http://vaman.plutokm.com/detect_saree'));
      request.files
          .add(await http.MultipartFile.fromPath('image', imagepath.path));
      var response = await request.send();
      var responseString = await response.stream.bytesToString();
      if (kDebugMode) {
        print('Response   :$responseString');
      }
      if (response.statusCode == 200) {
        return responseString;
      } else {
        return responseString;
      }
    } catch (e) {
      if (kDebugMode) {
        print(e);
      }
    }
    return '';
  }

  Future<String?> IdentifyStyleCode(
      File imagepath, String bodyimagepath) async {
    if (kDebugMode) {
      print('Identify Stylecode $bodyimagepath');
    }
    try {
      final request =
          http.MultipartRequest('POST', Uri.parse(identifystylecode));
      request.files
          .add(await http.MultipartFile.fromPath('image', imagepath.path));
      request.files
          .add(http.MultipartFile.fromString('body_image_path', bodyimagepath));
      //request.files.add(await http.MultipartFile.fromPath('embedding', embedding));
      var response = await request.send();
      var responseString = await response.stream.bytesToString();
      if (kDebugMode) {
        print('Response   :$responseString');
      }
      if (response.statusCode == 200) {
        return responseString;
      } else {
        return responseString;
      }
    } catch (e) {
      if (kDebugMode) {
        print(e);
      }
    }
    return '';
  }

  Future<String?> detectAttributes(File imagepath, String image) async {
    try {
      final request =
          http.MultipartRequest('POST', Uri.parse(detectattributes));
      request.files
          .add(await http.MultipartFile.fromPath('image', imagepath.path));
      request.files
          .add(http.MultipartFile.fromString('body_image_path', image));
      //request.fields['style_code'] = stylecode;
      var response = await request.send();
      var responseString = await response.stream.bytesToString();
      if (kDebugMode) {
        print('Response   :$responseString');
      }
      if (response.statusCode == 200) {
        return responseString;
      } else {
        return responseString;
      }
    } catch (e) {
      if (kDebugMode) {
        print(e);
      }
    }
    return '';
  }

  Future<String?> registerStyleCode(
      File imagepath, var embedding, String stylecode) async {
    if (kDebugMode) {
      print('Register Style Code : $stylecode');
    }
    try {
      final request =
          http.MultipartRequest('POST', Uri.parse(registerstylecode));
      request.files
          .add(await http.MultipartFile.fromPath('image', imagepath.path));
      request.files
          .add(http.MultipartFile.fromString('body_image_path', embedding));
      request.fields['style_code'] = stylecode;
      var response = await request.send();
      var responseString = await response.stream.bytesToString();
      if (kDebugMode) {
        print('Response   :$responseString');
      }
      if (response.statusCode == 200) {
        return responseString;
      } else {
        return responseString;
      }
    } catch (e) {
      if (kDebugMode) {
        print(e);
      }
    }
    return '';
  }

  Future<String?> searchbybarcode(String barcode) async {
    var client = http.Client();
    var res = await client.get(
        Uri.parse('https://vaman.plutokm.com/get_vas_barcode_image/$barcode'));
    if (kDebugMode) {
      print(res.body);
    }
    if (kDebugMode) {
      print(res.statusCode);
    }
    if (res.statusCode == 200) {
      return res.body;
    } else {
      throw Exception('Status code was wrong$res.statusCode');
    }
  }
}

//https://vaman.plutokm.com/get_vas_barcode_image/


