# represenst the type of lesson package
module JamRuby
  class LessonPackageType < ActiveRecord::Base

    @@log = Logging.logger[LessonPackageType]

    PRODUCT_TYPE = 'LessonPackageType'

    TEST_DRIVE_4_ID = 'test-drive'

    SINGLE_FREE = 'single-free'
    TEST_DRIVE_4 = 'test-drive-4'
    TEST_DRIVE_2 = 'test-drive-2'
    TEST_DRIVE_1 = 'test-drive-1'
    SINGLE = 'single'
    AMAZON_TEST_DRIVE_4_PAID_ID = 'amazon-test-drive-paid-4'
    AMAZON_TEST_DRIVE_FREE_2_ID = 'amazon-test-drive-free-2'
    AMAZON_TEST_DRIVE_FREE_4_ID = 'amazon-test-drive-free-4'

    AMAZON_PACKAGES = [AMAZON_TEST_DRIVE_4_PAID_ID, AMAZON_TEST_DRIVE_FREE_2_ID, AMAZON_TEST_DRIVE_FREE_4_ID]
    LESSON_PACKAGE_TYPES =
        [
            SINGLE_FREE,
            TEST_DRIVE_4,
            TEST_DRIVE_2,
            TEST_DRIVE_1,
            SINGLE
        ]

    has_many :user_desired_packages, class_name: "JamRuby::User", :foreign_key => "lesson_package_type_id", inverse_of: :desired_package
    has_many :users_needing_purchase, class_name: "JamRuby::User", :foreign_key => "lesson_package_needs_purchase_id", inverse_of: :lesson_package_needs_purchase
    has_many :posa_cards, class_name: "JamRuby::PosaCard"

    validates :name, presence: true
    validates :description, presence: true
    validates :price, presence: true
    validates :package_type, presence: true, inclusion: {in: LESSON_PACKAGE_TYPES}

    def self.test_drive_package_ids
      LessonPackageType.select("id").where(is_test_drive: true).map {|i| i.id }
    end

    def self.monthly
      LessonPackageType.find(MONTHLY)
    end

    def self.single_free
      LessonPackageType.find(SINGLE_FREE)
    end

    def is_amazon?
      AMAZON_PACKAGES.include?(id)
    end

    def self.amazon_test_drive_free_4
      LessonPackageType.find(AMAZON_TEST_DRIVE_FREE_4_ID)
    end

    def self.amazon_test_drive_free_2
      LessonPackageType.find(AMAZON_TEST_DRIVE_FREE_2_ID)
    end

    def self.amazon_test_drive_paid_4
      LessonPackageType.find(AMAZON_TEST_DRIVE_4_PAID_ID)
    end

    def self.test_drive_4
      LessonPackageType.find(TEST_DRIVE_4_ID)
    end

    def self.test_drive_2
      LessonPackageType.find(TEST_DRIVE_2)
    end

    def self.test_drive_1
      LessonPackageType.find(TEST_DRIVE_1)
    end

    def self.single
      LessonPackageType.find(SINGLE)
    end


    def booked_price(lesson_booking)
      if is_single_free?
        0
      elsif is_test_drive?
        10.00
      elsif is_normal?
        lesson_booking.booked_price #teacher.teacher.booking_price(lesson_booking.lesson_length, lesson_booking.payment_style == LessonBooking::PAYMENT_STYLE_SINGLE)
      end
    end

    def self.package_for_test_drive_count(count)
      LessonPackageType.find_by_package_type("test-drive-#{count}")
    end
    def test_drive_count
      package_type["test-drive-".length, 1].to_i
    end

    def description(lesson_booking)
      if is_single_free?
        "Single Free Lesson"
      elsif is_test_drive?
        "Test Drive (#{test_drive_count})"
      elsif is_normal?
        if lesson_booking.recurring
          "Recurring #{lesson_booking.payment_style == LessonBooking::PAYMENT_STYLE_WEEKLY ? "Weekly" : "Monthly"} #{lesson_booking.lesson_length}m"
        else
          "Single #{lesson_booking.lesson_length}m lesson"
        end
      end
    end

    def stripe_description(lesson_booking)
      description(lesson_booking)
    end

    def is_free?
      # if it's a 0 dollar test drive, or the 'single free' lesson, then it's atually free
      (is_test_drive && price == 0.00) || is_single_free
    end
    def is_single_free?
      id == SINGLE_FREE
    end

    def is_test_drive?
      is_test_drive
    end

    def is_normal?
      id == SINGLE
    end


    def sale_display(variant = nil)
      name
    end

    def variant_price(variant = nil)
      price
    end
    def plan_code
      if package_type == SINGLE_FREE
        "lesson-package-single-free"
      elsif package_type == TEST_DRIVE_4
        "lesson-package-test-drive-4"
      elsif package_type == TEST_DRIVE_2
        "lesson-package-test-drive-2"
      elsif package_type == TEST_DRIVE_1
        "lesson-package-test-drive-1"
      elsif package_type == SINGLE
        "lesson-package-single"
      else
        raise "unknown lesson package type #{package_type}"
      end
    end

    def sales_region
      'Worldwide'
    end

    def to_s
      sale_display
    end

    def admin_name
      admin = name

      if is_amazon?
        admin = admin + " (Amazon "
        if id == AMAZON_TEST_DRIVE_FREE_2_ID
          admin = admin + "Free)"
        else
          admin = admin + "Paid)"
        end

      end
      admin
    end
  end
end
