module JamRuby
  class SubscriptionDefinitions
    JAM_SILVER = 'jamsubsilver'
    JAM_SILVER_YEARLY = 'jamsubsilveryearly'
    JAM_GOLD = 'jamsubgold'
    JAM_GOLD_YEARLY = 'jamsubgoldyearly'
    JAM_PLATINUM = 'jamsubplatinum'
    JAM_PLATINUM_YEARLY = 'jamsubplatinumyearly'

    # ALL IN HOURS
    FREE_PLAY_TIME_PER_SESSION = 1
    FREE_PLAY_TIME_PER_MONTH = 4
    SILVER_PLAY_TIME_PER_SESSION = nil          # unlimited
    SILVER_PLAY_TIME_PER_MONTH = 10
    GOLD_PLAY_TIME_PER_SESSION = nil            # unlimited
    GOLD_PLAY_TIME_PER_MONTH = nil              # unlimited
    PLATINUM_PLAY_TIME_PER_SESSION = nil        # unlimited
    PLATINUM_PLAY_TIME_PER_MONTH = nil          # unlimited

    MONTHLY_PLANS = [
        nil,
        JAM_SILVER,
        JAM_GOLD,
        JAM_PLATINUM
    ]
    
    FREE_PLAN = {
        play_time_per_month: FREE_PLAY_TIME_PER_MONTH,
        play_time_per_session: FREE_PLAY_TIME_PER_SESSION,
        can_record_audio: false,
        can_use_video: false,
        can_record_video: false,
        can_record_wave: false,
        video_resolution: 1,
        audio_max_bitrate: 1, # 128
        can_broadcast: false,
        broadcasting_type: 3,
        max_players: 4,
        pro_audio: false,
        has_support: false,
        name: 'Free',
        rank: 0,

    }
    
    SILVER_PLAN = {
        play_time_per_month: SILVER_PLAY_TIME_PER_MONTH,
        play_time_per_session: SILVER_PLAY_TIME_PER_SESSION,
        can_record_audio: false,
        can_record_video: false,
        can_use_video: true,
        can_record_wave: true,
        video_resolution: 1, # CIF in the backend
        audio_max_bitrate: 2, #192
        can_broadcast: true,
        broadcasting_type: 3,
        max_players: nil,
        pro_audio: false,
        has_support: false,
        name: 'Silver',
        rank: 1,
    }
    
    GOLD_PLAN = {
        play_time_per_month: GOLD_PLAY_TIME_PER_MONTH,
        play_time_per_session: GOLD_PLAY_TIME_PER_SESSION,
        can_record_audio: true,
        can_record_video: true,
        can_use_video: true,
        can_record_wave: true,
        video_resolution: 3, # 720p in the backend
        audio_max_bitrate: 3, #256
        can_broadcast: true,
        broadcasting_type: 3,
        max_players: nil,
        pro_audio: true,
        has_support: true,
        name: 'Gold',
        rank: 2
    }
    
    PLATINUM_PLAN =   {
        play_time_per_month: PLATINUM_PLAY_TIME_PER_MONTH,
        play_time_per_session: PLATINUM_PLAY_TIME_PER_SESSION,
        can_record_audio: true,
        can_record_video: true,
        can_use_video: true,
        can_record_wave: true,
        video_resolution: 4, # 1080p in the backend
        audio_max_bitrate: 5, #512
        can_broadcast: true,
        broadcasting_type: 3,
        max_players: nil,
        pro_audio: true,
        has_support: true,
        name: 'Platinum',
        rank: 3
    }
    
    def self.rules(plan_code)
      if plan_code == nil || plan_code == ''
        FREE_PLAN
      elsif plan_code == JAM_SILVER || plan_code == JAM_SILVER_YEARLY
        SILVER_PLAN    
      elsif plan_code == JAM_GOLD || plan_code == JAM_GOLD_YEARLY
        GOLD_PLAN
      elsif plan_code == JAM_PLATINUM || plan_code == JAM_PLATINUM_YEARLY
        PLATINUM_PLAN
      else
        raise "unknown plan #{plan_code}"
      end
    end

    def self.is_downgrade(next_plan, current_plan)
      next_plan_rank = rules(next_plan)[:rank]
      current_plan_rank = rules(current_plan)[:rank]

      next_plan_rank < current_plan_rank
    end

    def self.higher_plan(plan_a, plan_b)
      if plan_a.nil?
        plan_b
      elsif plan_b.nil?
        plan_a
      else
        plan_a_rank = rules(plan_a)[:rank]
        plan_b_rank = rules(plan_b)[:rank]
        # if plan a is higher, take plan_a
        if plan_a_rank > plan_b_rank
          plan_a
        else
          plan_b
        end
      end
    end
  end
end
