class PartnerContact
  OFFSET_COUNT = 20
  class << self
    def get_list page: 1, search_keyword: "", partner_id: nil
      if partner_id
        offset        = (page.to_i == 1 ? 0 : (OFFSET_COUNT * (page.to_i - 1 )))

        if !search_keyword.blank?
          search_keyword = search_keyword.try(:strip)
          search_query = "partner_id = #{partner_id}"
          contact_query = Contact.where("partner_id = ? and (
              lower(handle)       like lower(?) or
              lower(name)         like lower(?) or
              lower(organization) like lower(?) or
              lower(street)       like lower(?) or
              lower(city)         like lower(?) or
              lower(state)        like lower(?) or
              lower(postal_code)  like lower(?) or
              lower(country_code) like lower(?) or
              lower(email)        like lower(?) or
              lower(local_name)         like lower(?) or
              lower(local_organization) like lower(?) or
              lower(local_street)       like lower(?) or
              lower(local_city)         like lower(?) or
              lower(local_state)        like lower(?) or
              lower(local_postal_code)  like lower(?) or
              lower(local_country_code) like lower(?))",
              partner_id, "%#{search_keyword}%", "%#{search_keyword}%", "%#{search_keyword}%", "%#{search_keyword}%", "%#{search_keyword}%",
              "%#{search_keyword}%", "%#{search_keyword}%", "%#{search_keyword}%", "%#{search_keyword}%", "%#{search_keyword}%", "%#{search_keyword}%",
              "%#{search_keyword}%", "%#{search_keyword}%", "%#{search_keyword}%", "%#{search_keyword}%", "%#{search_keyword}%")
        else
          contact_query = Contact.where("partner_id = ?", partner_id).order(:handle).paginate(page: page, per_page: OFFSET_COUNT)
        end

        contacts      = contact_query.order(:handle).paginate(page: page, per_page: OFFSET_COUNT)
        total_count = contact_query.count

        if search_keyword.blank?
          offset_from   = "#{(offset ==  0 ? (offset + 1) - offset : offset + 1)}"
          offset_to     = "#{(offset == 0 ? OFFSET_COUNT : offset + OFFSET_COUNT)}"

          if ((total_count.to_i + 1) - offset_from.to_i) < OFFSET_COUNT
            offset_to     = "#{total_count}"
          end
        else
          if total_count < OFFSET_COUNT
            offset_from   = "#{(offset ==  0 ? (offset + 1) - offset : offset + 1)}"
            offset_to     = "#{total_count}"
          else
            offset_from   = "#{(offset ==  0 ? (offset + 1) - offset : offset + 1)}"
            offset_to     = "#{(offset == 0 ? OFFSET_COUNT : offset + OFFSET_COUNT)}"
          end

          if ((total_count.to_i + 1) - offset_from.to_i) < OFFSET_COUNT
            offset_from   = "#{(offset ==  0 ? (offset + 1) - offset : offset + 1)}"
            offset_to     = "#{total_count}"
          end
        end

        return {list: contacts.as_json, offset_from: offset_from, offset_to: offset_to, total_count: total_count}
      end
    end
  end
end