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

class TankLevelsModel {
  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 source='';
  Color cardColor=Colors.white;
  bool isCheckedInput=false;
  bool isCheckedOutput=false;
  var showInConnections='';
  var showOutConnections='';
  Color percentageTextColor=Colors.black;
  Image  tank_image=Image(image: const AssetImage('images/100%.png'),);
  List<Color> startButtonColor=<Color>[];
  List<Color> stoptButtonColor=<Color>[];
  String motorStatus='';
  String currentWaterLevelPercent='';
  DateTime lastUpdatedTime=DateTime.now();
  String displayLastUpdatedTime='';
  String mode='';
  String tank_status='';
  String slave_status='';
  String slaveDisconnectedTime='';
  bool isAutomaticMode=true;

  TextEditingController minimumLevelTextboxController = TextEditingController();
  TextEditingController maximumLevelTextboxController = TextEditingController();

  Widget minTextBox() {
    return Container(
      child: TextFormField(
        cursorColor: Colors.white,
        style: TextStyle(color: primaryColor),
        controller: minimumLevelTextboxController,
        decoration: textFormFieldDecorationForBoxId(Icons.percent,'Minimum Water level in percentage (%)'),

      ),
    );
  }

  Widget maxTextBox() {
    return Container(
      child: TextFormField(
        cursorColor: Colors.white,
        style: TextStyle(color: primaryColor),
        controller: maximumLevelTextboxController,
        decoration: textFormFieldDecorationForBoxId(Icons.percent,'Maximum Water level in percentage (%)'),

      ),
    );
  }

  Widget saveButton(context){
    return
      Container(
        width: MediaQuery.of(context).size.width * .99,
    height: 50,
    padding: const EdgeInsets.fromLTRB(10, 0, 10, 0),
      child: ElevatedButton(
        style: ElevatedButton.styleFrom(
          foregroundColor: Colors.white,
          backgroundColor: primaryColor,
        ),


        onPressed: () {  },
        child: const Text(
          'Save',
          style: TextStyle(
            fontSize: 20,
          ),
        ),),)
      ;
  }




  TankLevelsModel();

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

    rtvm.tank_name = json['tankName'] ?? '';
    rtvm.capacity = json['capacity'] ??'';
    rtvm.capacityForParse =rtvm.capacity. replaceAll(',', '');
    rtvm.tank_status = json['status'] ?? '';
    rtvm.block_name = json['blockName'] ?? '';
    rtvm.type_of_water = json['typeOfWater'] ?? '';
    rtvm.water_level = json['waterlevel'] ?? '';
    rtvm.lastUpdatedTime = DateTime.now();
      if(rtvm.isAutomaticMode==false){
        rtvm.mode = 'Manual Mode';
      }
      else{
        rtvm.mode = 'Automatic Mode';
      }


    rtvm.displayLastUpdatedTime = DateFormat('yyyy-MM-dd HH:mm:ss').format(rtvm.lastUpdatedTime);

    rtvm.motorStatus ='OFF';
    rtvm.tank_location = json['tankLocation']??'';
    //40/100*80

    /*if(rtvm.water_level.toString().toLowerCase()=='nan') {
      rtvm.water_level='20';
    }*/
    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.percentageTextColor=Color(0XFFE2483D);
        /*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: 70,width:70,);
      }
      else if( percent>20&&percent<50){
        rtvm.percentageTextColor=Color(0XFFE56910);
        /*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: 70,width:70,);
      }
      else if( percent>50&&percent<80){
        rtvm.percentageTextColor=Color(0XFF1D7AFC);
        /*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: 70,width:70,);
      }
      else{
        rtvm.percentageTextColor=Color(0XFF0A9E04);
       /* 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: 70,width:70,);
      }
    }
    else{
      rtvm.water_level ='';
    }

    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);
    }





    Random random = new Random();


    rtvm.type = json['type'] ?? '';


    //rtvm.connections=json['connections']??[];

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


    rtvm.inConnections.forEach((element) {

      rtvm.inputConnections.add(element['inputConnections']);
      rtvm.showInConnections=rtvm.inputConnections.join(",");
    });

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

      rtvm.outputConnections.add(element['outputConnections']);
      rtvm.showOutConnections=rtvm.outputConnections.join(",");
    });

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

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

    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,
    "blockName": this.block_name,
    "typeOfWater": this.type_of_water ,
    "type": this.type,
    "tankLocation": this.tank_location,
    "waterlevel": this.water_level,
    "source": this.source,
  };
}