<?php
namespace PEVFSLite;

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

if ( ! trait_exists( __NAMESPACE__ . '\\Licensing' ) ) {
	trait Licensing {
		public function is_connected() {
			$accounts = $this->get_accounts();
			$key = ( 'plugin' === $this->type ) ? 'plugins' : 'themes';
			return isset( $accounts[ $key ][ $this->slug ] ) && ! empty( $accounts[ $key ][ $this->slug ]['id'] ) && ! empty( $accounts[ $key ][ $this->slug ]['user_id'] );
		}

		public function is_plugin() { 
			return 'plugin' === $this->type; 
		}
		
		public function is_theme() { 
			return 'theme' === $this->type; 
		}
		
		public function is_premium() { 
			return ! empty( $this->params['is_premium'] ); 
		}
		
		public function is_free() { 
			return ! $this->is_premium(); 
		}
		
		public function is_pro() { 
			return $this->is_premium(); 
		}
		
		public function is_trial() { 
			return false; 
		}
		
		public function is_plan( $plan_slug ) { 
			return false; 
		}
		
		public function can_use_premium_code() { 
			return $this->is_connected() && $this->is_premium(); 
		}
		
		public function has_paid_plan() { 
			return $this->params['has_paid_plans'] ?? false; 
		}
		
		public function get_id() { 
			return $this->id; 
		}
		
		public function get_slug() { 
			return $this->slug; 
		}
		
		public function get_version() { 
			return $this->version; 
		}
		
		public function get_upgrade_url() { 
			return 'https://freemius.com'; 
		}

		public function get_anonymous_id() {
			$accounts = $this->get_accounts();
			$id = $accounts['unique_id'] ?? null;
			
			if ( ! $id ) {
				$key = \home_url();
				$secret = \defined( 'SECURE_AUTH_KEY' ) ? SECURE_AUTH_KEY : 'fs_lite_secret';
				$id = \md5( $key . $secret );
				$accounts['unique_id'] = $id;
				$this->save_accounts( $accounts );
			}
			return $id;
		}

		public function get_install_id() {
			$accounts = $this->get_accounts();
			$key = ( 'plugin' === $this->type ) ? 'plugins' : 'themes';
			return $accounts[ $key ][ $this->slug ]['id'] ?? null;
		}

		public function is_tracking_allowed() {
			return $this->is_connected() && (bool) $this->get_plugin_data( 'is_tracking_allowed', true );
		}
	}
}
