module Mobile
  module Entities
    class Issue <Grape::Entity
      include ApiHelper
      include Redmine::I18n
      def self.issue_expose(f)
        expose f do |issue, opt|
          if issue.is_a?(Hash) && issue.key?(f)
            issue[f]
          elsif issue.is_a?(::Issue)
            if issue.respond_to?(f)
              if f == :created_on
                format_time(issue.send(f))
              else
                issue.send(f)
              end
            else
              case f
                when :issue_priority
                  get_issue_priority_api issue.priority_id
                when :issue_assigned_to
                  (get_user(issue.assigned_to_id)).login
                when :issue_status
                  IssueStatus.find(issue.status_id).name
                when :comment_count
                  issue.journals.where("notes is not null and notes != ''").count
                  # all_comments = []
                  # get_all_children(all_comments, f).count
                when :project_name
                  issue.project.name
                when :praise_count
                  get_activity_praise_num(issue)
                when :act_type
                  'Issue'
                when :act_id
                  issue.id
                when :title
                  issue.subject
                when :subject
                  issue.subject
                when :description
                  issue.description
                when :done_ratio
                  issue.done_ratio
              end
            end
          elsif issue.is_a?(::Journal)
            case f
              when :content
                issue[:notes]
              when :lasted_comment
                time_from_now issue.created_on
              when :act_id
                issue.id
              when :act_type
                'Journal'
              when :praise_count
                get_activity_praise_num(issue)
            end
          end
        end
      end
      issue_expose :subject
      issue_expose :description
      expose :author, using: Mobile::Entities::User do |f, opt|
        if f.is_a?(::Issue)
          f.send(:author)
        end
      end
      expose :user,using: Mobile::Entities::User do |f, opt|
        if f.is_a?(::Journal)
          f.send(:user)
        end
      end
      issue_expose :content
      issue_expose :lasted_comment

      issue_expose :done_ratio
      issue_expose :title
      issue_expose :act_type
      issue_expose :act_id
      issue_expose :created_on
      issue_expose :issue_priority
      issue_expose :issue_assigned_to
      issue_expose :issue_status
      issue_expose :comment_count
      issue_expose :project_name
      issue_expose :praise_count

      expose :id
      # expose :issue_journals, using: Mobile::Entities::Journal  do |f, opt|
      #   if f.is_a?(::Issue)
      #     f.journals.where("notes is not null and notes != ''").reverse
      #   end
      # end

      expose :all_children, using: Mobile::Entities::Issue  do |f, opt|
        #f[:journals_for_messages] if f.is_a?(Hash) && f.key?(:journals_for_messages)
        if f.is_a?(::Issue)
          # f.journals_for_messages.reverse
          if !opt[:children] && opt[:type] == 0
            opt[:children] = true
            tStart = opt[:page]*5
            tEnd = (opt[:page]+1)*5 - 1

            all_comments = f.journals.where("notes is not null and notes != ''").reorder("created_on desc")
            all_comments[tStart..tEnd]
          end
        end
      end

      expose :has_praise , if: lambda { |instance, options| options[:user] } do |instance, options|
        has_praise = false
        current_user = options[:user]
        obj = PraiseTread.where("praise_tread_object_id=? and praise_tread_object_type=? and user_id=?",instance.id,instance.class.to_s,current_user.id)
        has_praise = obj.empty? ? false : true
        has_praise
      end

      expose :parents_count, if: lambda { |instance, options| options[:user] } do |instance, options|
        if instance.is_a?(::Journal)
          parents_reply = []
          parents_reply = get_reply_parents(parents_reply, instance)
          parents_reply.count
        end
      end

      expose :parents_reply_bottom, using:Mobile::Entities::Issue do |f,opt|
        if f.is_a? (::Journal)
          #取二级回复的底楼层
          parents_reply = []
          parents_reply = get_reply_parents(parents_reply, f)
          if parents_reply.count > 0 && !opt[:bottom]
            if opt[:type] == 1
              # opt[:bottom] = true
              # parents_reply[opt[:page]..opt[:page]]
            else
              opt[:bottom] = true
              parents_reply[0..0]
            end
          else
            []
          end
        end
      end

      expose :parents_reply_top, using:Mobile::Entities::Issue do |f,opt|
        if f.is_a? (::Journal)
          #取二级回复的顶楼层
          parents_reply = []
          parents_reply = get_reply_parents(parents_reply, f)
          if parents_reply.count > 2 && !opt[:top]
            if opt[:type] == 1
              opt[:top] = true
              tStart = (opt[:page]-1)*5+2
              tEnd = (opt[:page])*5+2 - 1

              if tEnd >= parents_reply.count - 1
                tEnd = parents_reply.count - 2
              end

              if tStart <= parents_reply.count - 2
                parents_reply = parents_reply.reverse[tStart..tEnd]
                parents_reply.reverse
              else
                []
              end
            else
              opt[:top] = true
              parents_reply = parents_reply.reverse[0..1]
              parents_reply.reverse
            end
          elsif parents_reply.count == 2 && !opt[:top]
            opt[:top] = true
            parents_reply = parents_reply.reverse[0..0]
            parents_reply.reverse
          else
            []
          end
        end
      end
    end
  end
end