import 'package:flutter/material.dart';
import 'package:bookatanker/common/settings.dart';


class GetTanksDetailsModel {
  String tank_name = '';
  String capacity='';
  String block_name='';
  String type_of_water='';
  String type='';
  String tank_location='';
  Color cardColor=Colors.white;
  String motorStatus='';
  String EntityMotorStatus='';
  String motorStartedTime='';
  bool isMotor=true;
  String? motor;
  int groupValue = -1;
  List inConnections  =[];
  List outConnections=[];
  List inConnectionsForUpdate=[];
  List outConnectionsForUpdate=[];
  List inputConnections=[];
  List inputRadioChecked=[];
  List<String> outputConnections=[];
  List<String> outputtypes=[];
  List<String> inputtypes=[];
  String source='';
  List<bool> motorForUpdateInput1=[];
  bool motorForUpdateInput=false;
  bool isCheckedInput=false;
  bool isCheckedOutput=false;
  int inconnectionsIndex = 0;
  int outconnectionsIndex = 0;
  var showInConnections='';
  var showOutConnections='';
  var showdetailOutputConnections='';
  var showdetailInputConnections='';
  List<bool> outConnectionsCheckboxes=[];
  List<bool> inConnectionsCheckboxes=[];
  List<bool> outConnectionsCheckboxesForUpdate=[];
  List<bool> inConnectionsCheckboxesForUpdate=[];
  List<bool> inConnectionsCheckboxesForUpdateSumpInput=[];
  List<bool> inConnectionsHaveMotor=[];
  List<bool> inConnectionsHaveMotornNew=[];
  List<bool> outConnectionsHaveMotor=[];
  List<bool> outConnectionsHaveMotornNew=[];
  List<bool> inConnectionsHaveMotorForBore=[];
  List<bool> inConnectionsHaveMotorForBoreNew=[];
  List<String> inConnectionsHaveMotorForUi=[];
  List<Color> startButtonColor=<Color>[Colors.green,Colors.greenAccent,Colors.lightGreen];
  List<Color> stoptButtonColor=<Color>[Colors.grey,Colors.grey,Colors.grey];
  String status='';
  String startTime='';
  String stopTime='';
  String hardware_id='';
  String tank_hardware_id='';
  String water_level='';
  Color onButton=Colors.green;
  Color offButton=Colors.grey;
  Color grey=Colors.grey;
  String status1='1';
  String message='';
  String motor_status='';
  String motor_stop_status='';
  String capacityForParse='';
  String minPercentage='';
  String maxPercentage='';
  String tankAutomationMode='';
  String currentWaterLevelPercent='';
  String tank_status='';
  Color textColor=Colors.black;
  Image  tank_image=Image(image: const AssetImage('images/100%.png'),);

  TextEditingController boxIdOutputConnectionsController = TextEditingController();
  TextEditingController boxIdInputConnectionsController = TextEditingController();

  Widget outputConnectionsBoxId() {
    return Container(
      child: TextFormField(
        cursorColor: Colors.white,
        style: TextStyle(color: primaryColor),
        controller: boxIdOutputConnectionsController,
        decoration: textFormFieldDecorationForBoxId(Icons.add_box,'Enter Box Id'),

      ),
    );
  }

  Widget inputConnectionsBoxId() {
    return Container(
      child: TextFormField(
        cursorColor: Colors.white,
        style: TextStyle(color: primaryColor),
        controller: boxIdInputConnectionsController,
        decoration: textFormFieldDecorationForBoxId(Icons.add_box,'Enter Box Id'),

      ),
    );
  }


  GetTanksDetailsModel();

  factory GetTanksDetailsModel.fromJson(Map<String, dynamic> json){
    GetTanksDetailsModel rtvm = new GetTanksDetailsModel();

    rtvm.tank_name = json['tankName'] ?? '';
    rtvm.capacity = json['capacity'] ??'';
    rtvm.capacityForParse =rtvm.capacity. replaceAll(',', '');
    rtvm.block_name = json['blockName'] ?? '';
    rtvm.type_of_water = json['typeOfWater'] ?? '';
    rtvm.type = json['type'] ?? '';
    rtvm.tank_location = json['tankLocation']??'';
    rtvm.motorStatus = json['motor_status']??'';
    rtvm.hardware_id = json['motor_status']??'';
    rtvm.tank_hardware_id = json['motor_status']??'';
    rtvm.water_level = json['waterlevel'] ?? '';
    rtvm.minPercentage = json['auto_min_percentage'] ?? '';
    rtvm.maxPercentage = json['auto_max_percentage'] ?? '';
    rtvm.tankAutomationMode = json['auto_mode_type'] ?? '';
    rtvm.tank_status = json['status'] ?? '';

   // rtvm.motorForUpdateInput=true;

    /*if(rtvm.motorStatus=='0'){
      rtvm.startButtonColor=<Color>[Colors.green,Colors.greenAccent,Colors.lightGreen];
      rtvm.stoptButtonColor=<Color>[Colors.grey,Colors.grey,Colors.grey];
      rtvm.status='OFF';
    }
    else{
      rtvm.startButtonColor=<Color>[Colors.grey,Colors.grey,Colors.grey];
      rtvm.stoptButtonColor=<Color>[Colors.redAccent,Colors.red,Colors.redAccent];
      rtvm.status='ON';
    }*/


    if(rtvm.tank_location.toString().toUpperCase()=='SUMP'&&rtvm.type_of_water.toString().toUpperCase()=='BORE WATER'){
      rtvm.cardColor=Color(0xffC4A484);
    }
    else if(rtvm.tank_location.toString().toUpperCase()=='SUMP'&&rtvm.type_of_water.toString().toUpperCase()=='DRINKING WATER'){
      rtvm.cardColor=Color(0xffc3e3f8);
    }
    else if(rtvm.tank_location.toString().toUpperCase()=='OVERHEAD'&&rtvm.type_of_water.toString().toUpperCase()=='BORE WATER'){
      rtvm.cardColor=Color(0xfff8f3cb);
    }
    else if(rtvm.tank_location.toString().toUpperCase()=='OVERHEAD'&&rtvm.type_of_water.toString().toUpperCase()=='DRINKING WATER'){
      rtvm.cardColor=Color(0XFF9bedff);
    }
    else{
      rtvm.cardColor=Color(0xffcdf3ce);
    }


    if(rtvm.water_level!=''){
      double percent=int.parse(rtvm.water_level )/double.parse(rtvm.capacityForParse )*100;

      rtvm.currentWaterLevelPercent=percent.toStringAsFixed(0);;

      print(percent);
      if( percent<20){
        rtvm.textColor=Colors.red;
        /*rtvm.tank_image=Image(image: const AssetImage('images/25%gif.gif'),height:60,width:60,);*/
        rtvm.tank_image=Image(image: const AssetImage('images/25%.png'),height: 90,width:120,);
      }
      else if( percent>20&&percent<40){
        rtvm.textColor=Colors.yellow;
        /*rtvm.tank_image=Image(image: const AssetImage('images/50%gif.gif'),height: 60,width:60,);*/
        rtvm.tank_image=Image(image: const AssetImage('images/50%.png'),height: 90,width:120,);
      }
      else if( percent>40&&percent<70){
        rtvm.textColor=Colors.green;
        /*rtvm.tank_image=Image(image: const AssetImage('images/75%gif.gif'),height: 60,width:60,);*/
        rtvm.tank_image=Image(image: const AssetImage('images/75%.png'),height: 90,width:120,);
      }
      else{
        rtvm.textColor=Colors.blue;
        /* rtvm.tank_image=Image(image: const AssetImage('images/100%gif.gif'),height: 60,width:60,);*/
        rtvm.tank_image=Image(image: const AssetImage('images/100%.png'),height: 90,width:120,);
      }
    }
    else{
      rtvm.water_level ='';
    }

    rtvm.source = json['tankName']??'';
    rtvm.inConnectionsForUpdate=json['connections']['inputConnections']??[];
    rtvm.outConnectionsForUpdate=json['connections']['outputConnections']??[];

    rtvm.inConnections = json['connections']['inputConnections']??[];


    rtvm.inConnections.forEach((element) {

      element["startButtonColor"] = <Color>[Colors.grey,Colors.grey,Colors.grey];
      element["stoptButtonColor"] = <Color>[Colors.grey,Colors.grey,Colors.grey];
      rtvm.inputRadioChecked.add(element['inputismotor']);

      /*if(element['input_type'].toString().toLowerCase()!='borewell'){
        if(element['inputismotor']==true){
          rtvm.inConnectionsHaveMotor.add('yes');
        }
        else{
          rtvm.inConnectionsHaveMotor.add('no');
        }
      }*/
      if(element['input_type'].toString().toLowerCase()!='borewell'){
        rtvm.inConnectionsHaveMotor.add(element['inputismotor']);
      }
      if(element['input_type'].toString().toLowerCase()=='borewell'||element['input_type'].toString().toLowerCase()=='govt.pipeline'){
        rtvm.inConnectionsHaveMotorForBore.add(element['inputismotor']);
      }


      rtvm.inputConnections.add(element['inputConnections']);
      rtvm.showInConnections=rtvm.inputConnections.join(",");
      rtvm.inputtypes.add(element['input_type']??'');
      rtvm.showdetailInputConnections=rtvm.inputtypes.join("-");
      rtvm.EntityMotorStatus=element['motor_status'];
      if(element['motor_status']=='0'){
      element["startButtonColor"]=<Color>[Colors.green,Colors.greenAccent,Colors.lightGreen];
      element["stoptButtonColor"]=<Color>[Colors.grey,Colors.grey,Colors.grey];
      rtvm.status='OFF';
    }
    else{
        element["stoptButtonColor"]=<Color>[Colors.grey,Colors.grey,Colors.grey];
        element["stoptButtonColor"]=<Color>[Colors.redAccent,Colors.red,Colors.redAccent];
      rtvm.status='ON';
    }


    });

    rtvm.outConnections = json['connections']['outputConnections']??[];
    rtvm.outConnections.forEach((element) {

      rtvm.outputConnections.add(element['outputConnections']);
      //rtvm.outputtypes.add(element['outputConnections']);
      rtvm.outputtypes.add(element['output_type']??'');
      rtvm.showOutConnections=rtvm.outputConnections.join(",");
      rtvm.showdetailOutputConnections=rtvm.outputtypes.join("-");
      rtvm.outConnectionsHaveMotor.add(element['outputismotor']??false);
    });

    rtvm.isCheckedInput = false;
    rtvm.isCheckedOutput = false;


    return rtvm;
  }





  Map<String, dynamic> toJson() => {
    "tankName":this.tank_name,
    "capacity": this.capacity,
    "blockName": this.block_name,
    "typeOfWater": this.type_of_water ,
    "type": this.type,
    "tankLocation": this.tank_location,
    "source": this.source,
  };
  @override
  bool operator ==(Object other) {
    if (identical(this, other)) return true;

    return other is GetTanksDetailsModel &&
        other.tank_name == tank_name &&
        other.tank_location == tank_location;
  }

  @override
  int get hashCode => tank_name.hashCode ^ tank_location.hashCode;
}