import { array } from "yup";
import { getComponentViewBindingId } from "../utils/helpers";
import { findComponentsWithInAppPurchaseAction } from "../utils/inAppPurchase";
import { forEach } from "jszip";

export const billingFragmentFunctions = (fragment, configuration) => {
  if (!configuration.isInappPurchase) return "";
  if (findComponentsWithPayment(fragment).length == 0) return "";
  return `
    override fun onBillingServiceDisconnected() {
        Log.d(TAG, "onBillingServiceDisconnected")
    }

    override fun onBillingSetupFinished(p0: BillingResult) {
        billingViewModel.querySubscriptionStatus()
//        binding.tvBuySubscription.setOnClickWithDebounce {
//            showEditSheet()
//        }
    }`;
};
export const billingFragmentImports = (fragment, configuration) => {
  if (!configuration.isInappPurchase) return "";
  if (findComponentsWithPayment(fragment).length == 0) return "";
  return `import android.content.ContentValues.TAG
import android.util.Log
import androidx.lifecycle.lifecycleScope
import com.android.billingclient.api.BillingClientStateListener
import com.android.billingclient.api.BillingResult
import kotlinx.coroutines.async
import kotlinx.coroutines.launch
import org.koin.androidx.viewmodel.ext.android.sharedViewModel`;
};

export const buildBillingFragmentFields = (fragment, configuration) => {
  if (!configuration.isInappPurchase) return "";
  if (findComponentsWithPayment(fragment).length == 0) return "";
  return `private val billingViewModel: BillingViewModel by sharedViewModel()`;
};

export const buildBillingFragmentDeps = (fragment, configuration) => {
  if (!configuration.isInappPurchase) return "";
  const hasInAppPurchaseAction =
    findComponentsWithInAppPurchaseAction(fragment);

  if (
    hasInAppPurchaseAction &&
    hasInAppPurchaseAction.find(
      (item) => item.name === fragment.name || item.id === fragment.id
    )
  ) {
    return `, PurchasesUpdatedListener`;
  }
  if (findComponentsWithPayment(fragment).length == 0) return "";

  return `,BillingClientStateListener `;
};

export const buildFragmentDeps = (fragment, configuration) => {
  if (!configuration.isInappPurchase) {
    if(findVideoViewFragment(fragment).length > 0) {
      return "";
    } else {
      return "";
    }
  } else {
    buildBillingFragmentDeps(fragment, configuration);
  }
 
}

export const buildBillingFragmentTrigger = (fragment, configuration) => {
  let paymentElements = findComponentsWithPayment(fragment);
  if (paymentElements.length == 0) return "";
  const content = [];
  content.push(` lifecycleScope.launch {
        async {
        }.await()
    }`);
  content.push(
    `billingViewModel.initializeBillingClient(requireActivity(), this)`
  );
  for (const element of paymentElements) {
    content.push(
      `binding.${getComponentViewBindingId(
        element
      )}.setOnClickListener { billingViewModel.launchBillingFlow(requireActivity()) }`
    );
  }
  return content.join("\n");
};

function findVideoViewFragment(data, result = []) {
  if (!Array.isArray(data)) data = [data];
  for (const item of data) {
    if ("components" in item) {
      findVideoViewFragment(item.components, result);
    }
    if ("children" in item) {
      findVideoViewFragment(item.children, result);
    }
    if (item.name === "VideoView") {
      result.push(item);
    }
  }
  return result;
}

function findComponentsWithPayment(data, result = []) {
  if (!Array.isArray(data)) data = [data];
  for (const item of data) {
    if ("components" in item) {
      findComponentsWithPayment(item.components, result);
    }
    if ("props" in item && item.props.onclick === "payment") {
      result.push(item);
    }
  }
  return result;
}
