<?php
if ( ! defined( 'ABSPATH' ) ) {
    exit; // Exit if accessed directly.
}

class fbbttmTabledata{

	/**
	 * function for insert countries data
	 * 
	 */
	  
	public function fbbttm_set_countryinfo($tablename=null){

		global $wpdb, $fbbttm_helperobj;


		$tabledata = $fbbttm_helperobj->fbbttm_comman_single_tableinfo($tablename,'id');

		if(!empty($tabledata)) return '';

		$countryfile= FBBTTM_DIR.'dbschema/countries.json';
		// phpcs:disable WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents 
		$str = file_get_contents($countryfile);
		$json = json_decode($str, true);
		
		if(empty($json)) return '0';

		$insertData = array();
		$jsonarray = array ();
		$jsondata= $json['countries']; 
	
		for($i=0;$i<count($jsondata);$i++)
		{
			
			$jsonarray['id'] = $jsondata[$i]['id'];
			$jsonarray['shortname'] = $jsondata[$i]['sortname'];
			$jsonarray['name'] = $jsondata[$i]['name'];
			$jsonarray['phoneCode'] = $jsondata[$i]['phoneCode'];

			$insertData[] = $jsonarray;
		}

		$fbbttm_helperobj->fbbttm_common_multiple_insert($tablename,$insertData);

		return true;
	}


	/**
	 *  function for insert countries states data
	 */ 

	public function fbbttm_set_stateinfo($tablename){


		global $wpdb, $fbbttm_helperobj;

		$tabledata = $fbbttm_helperobj->fbbttm_comman_single_tableinfo($tablename,'id');

		if(!empty($tabledata)) return '';

		$countryfile= FBBTTM_DIR.'dbschema/states.json';
		// phpcs:disable WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
		$str = file_get_contents($countryfile);
		$json = json_decode($str, true);

		
		if(empty($json)) return '0';

		$insertData = array();
		$jsonarray = array ();
		$jsondata= $json['states']; 
	
		for($i=0;$i<count($jsondata);$i++)
		{
			$jsonarray['id'] = $jsondata[$i]['id'];
			$jsonarray['name'] = $jsondata[$i]['name'];
			$jsonarray['country_id'] = $jsondata[$i]['country_id'];
			$insertData[] = $jsonarray;
		}

		$fbbttm_helperobj->fbbttm_common_multiple_insert($tablename,$insertData);

		return true;
	}


	/**
	 *  function for insert countries states data
	 */ 

	public function fbbttm_set_currency_info($tablename){


		global $wpdb, $fbbttm_helperobj;

		$tabledata = $fbbttm_helperobj->fbbttm_comman_single_tableinfo($tablename,'id');

		if(!empty($tabledata)) return '';

		$countryfile = FBBTTM_DIR.'dbschema/currencies.json';

		// phpcs:disable WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
		$str = file_get_contents($countryfile);
		$json = json_decode($str, true);

		
		if(empty($json)) return '0';

		$insertData = array();
		$jsonarray = array ();
		$jsondata= $json; 
	
		for($i=0;$i<count($jsondata);$i++)
		 {
			
			$jsonarray['name'] = $jsondata[$i]['name'];
			$jsonarray['symbol'] = $jsondata[$i]['symbol'];
			$jsonarray['symbolNative'] = $jsondata[$i]['symbolNative'];
			$jsonarray['decimalDigits'] = $jsondata[$i]['decimalDigits'];
			$jsonarray['rounding'] = $jsondata[$i]['rounding'];
			$jsonarray['code'] = $jsondata[$i]['code'];
			$jsonarray['namePlural'] = $jsondata[$i]['namePlural'];
			$insertData[] = $jsonarray;

		 }

		$fbbttm_helperobj->fbbttm_common_multiple_insert($tablename,$insertData);

		return true;
	}

}