import {Injectable} from "@angular/core";
import {DbAbstractionLayer} from "@nodeart/dal/index";
@Injectable()
export class ProductService{

  private esIndex = 'firebase';

   constructor(private dal: DbAbstractionLayer){
  }

  getOneProduct(id: string){
    return this.dal.getOneProduct(id);
  }

  searchProducts(categoryId: string, currentPrice, attributes: any[], tags: any[], size: number, from: number){
    let queryObject = {
     size: size,
     from: from,
     query: {
        bool: {
          must: [
            {
              bool: {
                should: []
              }
            },
            {
              match: {
                "category" : categoryId
              }
            },
            {
              "range" : {
                "price" : {
                    "gte" : currentPrice[0],
                    "lte" : currentPrice[1]
                }
              }
            }
          ]
        }
      }
    };
    for(let i = 0; i < attributes.length; i++){
        let key = "attributes." + attributes[i]['attrId'];
        queryObject.query.bool.must[0]['bool'].should.push({
          match: {
            [key] : attributes[i]['valueName']
          }
        });
    }
    for(let i = 0; i < tags.length; i++){
        let key = "tags";
        queryObject.query.bool.must[0]['bool'].should.push({
          match: {
            [key] : tags[i]['id']
          }
        });
    }
    console.log(queryObject);
    return this.dal.requestData(this.esIndex, 'product', queryObject);
  }

  getPriceRanges(categoryId){
    let queryObject = {
     aggs : {
        max_price : { max : { field : "price" } },
        min_price : { min : { field : "price" } }
      },
      query: {
        bool: {
          should: {
            match: {
              category: categoryId
                  }
              }
          }
      }
    };
    console.log(JSON.stringify(queryObject));
    return this.dal.requestFullData(this.esIndex, 'product', queryObject);
  }

  filterProducts(priceRange: number[], attributes : any[], tags: any[], size, offset){
    let queryObject = {
     size: size,
     from: offset,
     query: {
        bool: {
          must: [
            {
              bool: {
                should: []
              }
            },
            {
              "range" : {
                "price" : {
                    "gte" : priceRange[0],
                    "lte" : priceRange[1]
                }
              }
            }
          ]
        }
      }
    };
    for(let i = 0; i < attributes.length; i++){
        let key = "attributes." + attributes[i]['attrId'];
        queryObject.query.bool.must[0]['bool'].should.push({
          match: {
            [key] : attributes[i]['valueName']
          }
        });
    }
    for(let i = 0; i < tags.length; i++){
        let key = "tags";
        queryObject.query.bool.must[0]['bool'].should.push({
          match: {
            [key] : tags[i]['id']
          }
        });
    }
    return this.dal.requestFullData(this.esIndex, 'product', queryObject);
  }

  getGeneralCategory(generalCategoryId){
    let queryObj = {
        "query": {
            "term":{
                "_id": generalCategoryId.toString()
            }
        }
    };
    return this.dal.requestData(this.esIndex, 'general-category', queryObj);
  }

  getCategory(categoryId){
    let queryObj = {
        "query": {
            "term":{
                "_id": categoryId.toString()
            }
        }
    };
   return this.dal.requestData(this.esIndex, 'category', queryObj);
  }

  getGeneralCategories(){
    let queryObj = {
        "query": {
            "match_all": {}
        }
    };
    return this.dal.requestData(this.esIndex, 'general-category', queryObj);
  }

  getCategories(categoryId){
    let queryObj;
    if(categoryId == ''){
      queryObj = {
        query: {
          filtered: {
            filter: {
              bool: {
                must_not: [
                  {
                    exists: {
                      field: "parentId"
                    }
                  }
                ]
              }
            }
          }
        }
      }
    } else {
      queryObj = {
        query: {
          term: {
            parentId: categoryId
          }
        }
      };
    }
    console.log(JSON.stringify(queryObj));
    return this.dal.requestData(this.esIndex, 'category', queryObj);
  }

  getProductsByCategoryIds(categoryIds, size, from){
    let queryObj = {
      size: size,
      from: from,
      query: {
        constant_score: {
          filter: {
            terms: {
              category: categoryIds
            }
          }
        }
      }
    }
    return this.dal.requestData(this.esIndex, 'product', queryObj);
  }

  getProducts(query, size){
    let queryObj = {
      "size": size,
        "query": {
            "wildcard": {
              "name": "*" + query + "*"
            }
        }
    };
    return this.dal.requestData(this.esIndex, 'product', queryObj);
  }

  getTotalPages(categoryId, currentPrice, attributes: any[], tags: any[]){
    let queryObject = {
     size: 0,
     query: {
        bool: {
          must: [
            {
              bool: {
                should: []
              }
            },
            {
              match: {
                "category" : categoryId
              }
            },
            {
              "range" : {
                "price" : {
                    "gte" : currentPrice[0],
                    "lte" : currentPrice[1]
                }
              }
            }
          ]
        }
      }
    };
    for(let i = 0; i < attributes.length; i++){
        let key = "attributes." + attributes[i]['attrId'];
        queryObject.query.bool.must[0]['bool'].should.push({
          match: {
            [key] : attributes[i]['valueName']
          }
        });
    }
    for(let i = 0; i < tags.length; i++){
        let key = "tags";
        queryObject.query.bool.must[0]['bool'].should.push({
          match: {
            [key] : tags[i]['id']
          }
        });
    }
    console.log(JSON.stringify(queryObject));
    return this.dal.requestItemsTotal(this.esIndex, 'product', queryObject);
  }

  getTotalPagesByCategoryIds(categoryIds){
    let queryObj = {
      size: 0,
      query: {
        constant_score: {
          filter: {
            terms: {
              category: categoryIds
            }
          }
        }
      }
    };
    return this.dal.requestItemsTotal(this.esIndex, 'product', queryObj);
  }

  getOneAttribute(attributeId){
    let queryObj = {
        "query": {
            "term": {
                "_id": attributeId
            }
        }
    }
    return this.dal.requestData(this.esIndex, 'attributes', queryObj);
  }

  getOneTag(tagId){
    let queryObj = {
      "query": {
        "term": {
            "_id": tagId
        }
      }
    };
    return this.dal.requestData(this.esIndex, 'tags', queryObj);
  }
}
