

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

class ParticularTankConnectionsModel {
  String tank_name = '';
  String capacity='';
  String capacityForParse='';
  String finalCapacity='';
  String block_name='';
  String type_of_water='';
  String type='';
  String tank_location='';
  String water_level='';
  List inConnections=[];
  List outConnections=[];
  List inputConnections=[];
  List outputConnections=[];
  String currentWaterLevelPercent='';
  bool isManual=false;
  Image  tank_image=Image(image: const AssetImage('images/100%.png'),);
  bool minimumPercentageTextEditable=true;
  bool maximumPercentageTextEditable=true;
  String slave_status='';
  String slaveDisconnectedTime='';

  TextEditingController minimumPercentageTextController = TextEditingController();
  TextEditingController maximumPercentageTextController = TextEditingController();

  Widget minimumPercentage(var height,var width,bool readOnly) {
    return Container(
      height: height,
      width: width,
      child: TextFormField(
        cursorColor: primaryColor,
        style: TextStyle(color: primaryColor),
        controller: minimumPercentageTextController,
        decoration: textFormFieldDecorationForBoxId(Icons.percent,'Enter min %'),
        readOnly:readOnly ,

      ),
    );
  }

  Widget maximumPercentage(var height,var width,bool readOnly) {
    return Container(
      height: height,
      width: width,
      child: TextFormField(
        cursorColor: primaryColor,
        readOnly:readOnly ,
        style: TextStyle(color: primaryColor),
        controller: maximumPercentageTextController,
        decoration: textFormFieldDecorationForBoxId(Icons.percent,'Enter max %'),

      ),
    );
  }


  ParticularTankConnectionsModel();


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

    rtvm.tank_name = json['tankName'] ?? '';
    rtvm.capacity = json['capacity'] ??'';
    rtvm.tank_location = json['tankLocation'] ??'';
    rtvm.capacityForParse =rtvm.capacity. replaceAll(',', '');
    rtvm.block_name = json['blockName'] ?? '';
    rtvm.type_of_water = json['typeOfWater'] ?? '';
    rtvm.minimumPercentageTextController.text = json['auto_min_percentage'] ?? '';
    rtvm.maximumPercentageTextController.text = json['auto_max_percentage'] ?? '';
    rtvm.water_level = json['waterlevel'] ?? '';
    rtvm.inConnections = json['connections']['inputConnections']??[];
    rtvm.outConnections = json['connections']['outputConnections']??[];
    rtvm.isManual=false;
    //rtvm.minimumPercentageTextEditable=false;

    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.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: 100,width:100,);
      }
      else if( percent>20&&percent<50){
        /*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: 100,width:100,);
      }
      else if( percent>50&&percent<80){
        /*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: 100,width:100,);
      }
      else{
        rtvm.tank_image=Image(image: const AssetImage('images/100%.png'),height: 100,width:100,);
      }
    }
    else{
      rtvm.water_level ='';
    }
    rtvm.slave_status = json['slave_status'] ?? '';
    rtvm.slaveDisconnectedTime = json['slave_disconnected_time'] ?? '';

    return rtvm;
  }




  Map<String, dynamic> toJson() => {
    "tankName":this.tank_name,
    "capacity": this.capacity,
  };
}