// Copyright (c) 2020, Frappe and contributors
// For license information, please see license.txt

frappe.ui.form.on('Invoice', {
	refresh: function (frm) {
		if (frm.pg.stripe_invoice_id) {
			frm.add_web_link(
				`https://dashboard.stripe.com/invoices/${frm.pg.stripe_invoice_id}`,
				'View Stripe Invoice',
			);
		}
		if (frm.pg.frappe_invoice) {
			frm.add_web_link(
				`https://framework.jingrow.com/app/sales-invoice/${frm.pg.frappe_invoice}`,
				'View Frappe Invoice',
			);
		}

		if (frm.pg.frappe_partner_order) {
			frm.add_web_link(
				`https://framework.jingrow.com/app/partner-order/${frm.pg.frappe_partner_order}`,
				'View Frappe Partner Order',
			);
		}

		if (frm.pg.status == 'Paid' && !frm.pg.frappe_invoice) {
			let btn = frm.add_custom_button('Create Invoice on framework.jingrow.com', () => {
				frm
					.call({
						pg: frm.pg,
						method: 'create_invoice_on_frappeio',
						btn,
					})
					.then((r) => {
						if (r.message) {
							frappe.msgprint(
								`Sales Invoice ${r.message} created successfully.`,
							);
						}
						frm.refresh();
					});
			});
		}

		if (frm.pg.status == 'Paid' && frm.pg.stripe_invoice_id) {
			frm.add_custom_button('Refund Invoice', () => {
				let d = new frappe.ui.Dialog({
					title: 'Refund Invoice',
					fields: [
						{
							label: 'Reason',
							fieldname: 'reason',
							fieldtype: 'Data',
						},
					],
					primary_action({ reason }) {
						if (!reason) {
							frappe.msgprint('Please enter a reason for the refund.');
							return;
						}
						d.hide();
						frappe
							.call({
								pg: frm.pg,
								method: 'refund',
								args: {
									reason,
								},
								btn: d.get_primary_btn(),
							})
							.then((r) => {
								if (r.message) {
									frappe.msgprint('Refunded successfully.');
									d.hide();
								}
								frm.refresh();
							});
					},
				});
				d.show();
			});
		}

		if (frm.pg.status == 'Invoice Created') {
			let btn = frm.add_custom_button(
				'Finalize Invoice',
				() => {
					frappe.confirm(
						"This action will finalize the Stripe Invoice and charge the customer's card. Continue?",
						() => {
							frm
								.call({
									pg: frm.pg,
									method: 'finalize_stripe_invoice',
									btn,
								})
								.then((r) => frm.refresh());
						},
					);
				},
				'Stripe Invoice',
			);
		}

		if (frm.pg.stripe_invoice_url) {
			let btn = frm.add_custom_button(
				'Refresh Payment Link',
				() => {
					frm
						.call({
							pg: frm.pg,
							method: 'refresh_stripe_payment_link',
							btn,
						})
						.then((r) => {
							frm.refresh();
							frappe.utils.copy_to_clipboard(r.message);
							frappe.msgprint({
								title: 'Stripe Payment Link Updated',
								indicator: 'green',
								message: 'The Link has been copied to the clipboard.',
							});
						});
				},
				'Stripe Invoice',
			);
		}

		if (frm.pg.docstatus == 1 && frm.pg.stripe_invoice_id) {
			let btn = frm.add_custom_button(
				'Change Status',
				() => {
					let d = new frappe.ui.Dialog({
						title: 'Change Stripe Invoice Status',
						fields: [
							{
								label: 'Status',
								fieldname: 'status',
								fieldtype: 'Select',
								options: ['Paid', 'Uncollectible', 'Void'],
							},
						],
						primary_action({ status }) {
							frm
								.call({
									pg: frm.pg,
									method: 'change_stripe_invoice_status',
									args: {
										status,
									},
									btn,
								})
								.then((r) => frm.refresh());
						},
					});
					d.show();
				},
				'Stripe Invoice',
			);
		}

		if (frm.pg.docstatus === 0) {
			let btn = frm.add_custom_button('Finalize Invoice', () =>
				frappe.confirm(
					'This action will apply credits (if applicable) and generate a Stripe invoice if the amount due is greater than 0. ' +
						'If a Stripe invoice was generated already, it will be voided and a new one will be generated. Continue?',
					() =>
						frm
							.call({
								pg: frm.pg,
								method: 'finalize_invoice',
								btn,
							})
							.then(() => {
								frm.refresh();
							}),
				),
			);
		}
	},
});
