import { EstadoCaptacion, TipoCaptacion } from '@win2win/shared';
import { isArray } from 'lodash';
import { QTableColumn } from 'quasar';
import { formatIdCaptacion } from 'src/composables/useCaptacionPage';
import { formatDate } from 'src/helpers';
import { AppLocation, ArchivosESTADO_APROBACION, Producto, RawLocation } from 'src/models';
export const DEFAULT_CENTER = { lat: 40.689247, lng: -74.044502 };

export const LOCATION_FIELDS_LABELS: Partial<Record<keyof AppLocation, string>> = {
  streetNumber: 'Número',
  street: 'Calle',
  city: 'Ciudad',
  zone: 'Estado',
  country: 'País',
  postalCode: 'Código Postal',
  street_between_1: 'Entre calle',
  street_between_2: 'Entre calle 2',
  colony: 'Colonia',
  town: 'Municipio',
  floor_number: 'Piso',
  interior_number: 'Número interior',
  door_number: 'Puerta',
};

export class LocationMapper {
  fromRaw(rawLocation: RawLocation) {
    return {
      street: rawLocation.route,
      streetNumber: rawLocation.street_number,
      city: rawLocation.locality,
      zone: rawLocation.administrative_area_level_1,
      country: rawLocation.country,
      postalCode: rawLocation.postal_code,
      street_between_1: '',
      street_between_2: '',
      floor_number: '',
      interior_number: '',
      door_number: '',
      colony: '',
      town: '',
    };
  }
}

export const addressComponentsMapper: Partial<Record<keyof AppLocation, string>> = {
  country: 'country',
  zone: 'administrative_area_level_1',
  city: 'locality',
  town: 'town',
  colony: 'colony',
  street_between_1: 'street_between_1',
  street_between_2: 'street_between_2',
  postalCode: 'postal_code',
  street: 'route',
  streetNumber: 'street_number',
  floor_number: 'floor_number',
  interior_number: 'interior_number',
  door_number: 'door_number',
};

export const ESTADOS_CAPTACION = [
  {
    label: 'Todas',
    count: 0,
    value: 99,
    color: null,
  },
  {
    label: 'Pendiente',
    count: 0,
    value: EstadoCaptacion.EN_ESTUDIO,
    color: 'grey',
  },
  {
    label: 'Aceptada',
    count: 0,
    value: EstadoCaptacion.PRE_APROBADA,
    color: 'orange',
  },
  {
    label: 'Pre aprobada',
    count: 0,
    value: EstadoCaptacion.APROBADA,
    color: 'yellow',
  },
  {
    label: 'En negociación',
    count: 0,
    value: EstadoCaptacion.EN_NEGOCIACION,
    color: 'cyan',
  },
  {
    label: 'Cotización aceptada',
    count: 0,
    value: EstadoCaptacion.NEGOCIACION_FINALIZADA,
    color: 'green',
  },
  {
    label: 'Confirmada',
    count: 0,
    value: EstadoCaptacion.CONFIRMADA,
    color: 'coral',
  },
  {
    label: 'Tramitada',
    count: 0,
    value: EstadoCaptacion.FINALIZADA,
    color: 'blue',
  },
  {
    label: 'Descartada',
    count: 0,
    value: EstadoCaptacion.CANCELADA,
    color: 'black',
  },
  {
    label: 'Eliminada',
    count: -1,
    value: EstadoCaptacion.ELIMINADA,
    color: 'black',
  },
];
Object.freeze(ESTADOS_CAPTACION);

export const captacionProductTabs = [
  { label: 'Estado', to: 'estado', name: 'general' },
  { label: 'Contacto', to: 'contacto', name: 'contact' },
  { label: 'Formulario', to: 'formulario', name: 'questions' },
  { label: 'Finca', to: 'producto', name: 'product' },
  { label: 'Cuenta bancaria', to: 'cuenta-bancaria', name: 'bank-account' },
  { label: 'Documentos', to: 'documentacion', name: 'documents' },
  { label: 'Incidencias', to: 'incidencias', name: 'incidents' },
  { label: 'Propuesta alquiler', to: 'cotizacion', name: 'budget' },
  { label: 'Contrato y firma', to: 'contrato', name: 'contract' },
] as const;
Object.freeze(captacionProductTabs);

//Hipoteca tabs
export const captacionHipotecaTabs = [
  { label: 'Estado', to: 'estado', name: 'general' },
  { label: 'Titulares', to: 'titulares', name: 'headlines' },
  { label: 'Ingresos', to: 'ingresos', name: 'revenue' },
  { label: 'Deuda', to: 'deuda', name: 'debts' },
  { label: 'Garantías', to: 'garantias', name: 'garant' },
  { label: 'Contacto', to: 'contacto', name: 'contact' },
  { label: 'Dirección', to: 'direccion_contacto', name: 'direccion' },
] as const;
Object.freeze(captacionProductTabs);

export const captacionUsuarioTabs = [
  // { label: 'Estado', to: 'estado', name: 'general' },//hipoteca
  { label: 'Contacto', to: 'contacto', name: 'contact' },
  { label: 'Dirección', to: 'direccion_contacto', name: 'direccion' },
] as const;
Object.freeze(captacionProductTabs);

export const fileStates = {
  [ArchivosESTADO_APROBACION.PENDIENTE_A_SUBIR]: {
    label: 'Por subir',
    color: 'grey',
  },
  [ArchivosESTADO_APROBACION.PENDIENTE_A_APROBAR]: {
    label: 'Por verificar',
    color: 'yellow',
  },
  [ArchivosESTADO_APROBACION.RECHAZADA]: {
    label: 'Incidencia',
    color: 'red',
  },
  [ArchivosESTADO_APROBACION.APROBADA]: {
    label: 'Verificado',
    color: 'green',
  },
};
Object.freeze(fileStates);

export const fileTags = [
  { label: 'Titular', value: 'contact' },
  { label: 'Localización', value: 'location' },
  { label: 'Contrato', value: 'contract' },
  { label: 'Cotización', value: 'budget' },
  { label: 'Cuenta bancaria', value: 'bank' },
  { label: 'Ingreso', value: 'incomes' },
  { label: 'Garantía', value: 'guarantee' },
  { label: 'Deuda', value: 'debt' },
] as const;

export const MESSAGE_TYPES = [
  {
    value: 1,
    label: 'Mensaje coorporativo',
    icon: 'img:colores/1.png',
  },
  {
    value: 2,
    label: 'Incidencia',
    icon: 'img:colores/6.svg',
  },
  {
    value: 3,
    label: 'Incidencia tramitada',
    icon: 'img:colores/4.svg',
  },
  {
    value: 4,
    label: 'Liquidación',
    icon: 'img:colores/5.svg',
  },
] as const;
Object.freeze(MESSAGE_TYPES);

export const progressItems = [
  /*
  Esto deberia ir dinamico:
  {
    label: 'En revisión',
    value: 1,
    number: '1',
    width: '150px',
    active: false,
    activeColor: 'rgba(226, 237, 79)',
    pasiveColor: 'rgba(226, 237, 79, 0.5)',
  },
  {
    label: 'Pre aprobada',
    value: 3,
    number: '2',
    width: '135px',
    active: false,
    activeColor: 'rgba(147, 213, 108)',
    pasiveColor: 'rgba(147, 213, 108, 0.5)',
  },
  {
    label: 'Propuesta',
    value: 4,
    number: '3',
    width: '135px',
    active: false,
    activeColor: 'rgba(159, 216, 88, 1)',
    pasiveColor: 'rgba(159, 216, 88, 0.4)',
  },
  {
    label: 'Tramitada',
    value: 7,
    number: '3',
    width: '125px',
    active: false,
    activeColor: 'rgba(54, 1, 115, 1)',
    pasiveColor: 'rgba(54, 1, 115, 0.2)',
  }, */
] as const;

Object.freeze(progressItems);

export const tipos_incidencia = [
  { label: 'Datos de contacto', value: '1' },
  { label: 'Documentación', value: '2' },
  { label: 'Formulario', value: '3' },
  { label: 'Localización', value: '4' },
  { label: 'Cuenta Bancaria', value: '5' },
];

export const DEFAULT_LAT = -33.3643776;
export const DEFAULT_LON = -70.729728;
export const DEFAULT_LOCATION = {
  geolocation: {
    lat: -33.3643776,
    lng: -70.729728,
  },
  streetNumber: '',
  street: '',
  street_between_1: '',
  street_between_2: '',
  colony: '',
  town: '',
  city: '',
  zone: '',
  country: '',
  postalCode: '',
  fullAddress: '',
  registry: '',
};
Object.freeze(DEFAULT_LOCATION);

export const FINCAS_TABLE_COLUMN: QTableColumn[] = [
  {
    label: 'Cod. Sol.',
    name: 'captacion.ID_CAPTACION',
    align: 'left',
    sortable: true,
    field: (o) => formatIdCaptacion(o.ID_CAPTACION),
  },
  {
    label: 'Fecha',
    name: 'captacion.FECHA_CAPTADO',
    field: (o) => formatDate(o.FECHA_CAPTADO),
    align: 'left',
    sortable: true,
  },
  {
    label: 'Estado',
    name: 'captacion.ESTADO',
    align: 'left',
    sortable: true,
    field: 'ESTADO',
  },
  { label: 'Cod. Finca', name: 'ARTICULO.COD_ART_MVX', field: (o) => o?.ARTICULO?.COD_ART_MVX, align: 'left', sortable: true },
  { label: 'Nombre', name: 'CONTACTO.NOMBRE', field: (o) => o?.CONTACTO?.PERSONA_CONTACTO || o?.CONTACTO?.NOMBRE, align: 'left', sortable: true },
  { label: 'Teléfono', name: 'TELEFONO.NUMERO', field: (o) => o?.CONTACTO?.TELEFONO?.NUMERO || '-', align: 'left', sortable: true },
  { label: 'Correo electrónico', name: 'CONTACTO.EMAIL', field: (o) => o?.CONTACTO?.EMAIL, align: 'left', sortable: true },
  { label: 'Superficie', name: 'SUPERFICIE_SOLAR', field: (o) => getProductPropValue(o.ARTICULO, 'SUPERFICIE_SOLAR'), align: 'center', sortable: false },
  { label: 'Ubicación', name: 'UBICACION.PROVINCIA', field: (o) => `${o?.ARTICULO?.UBICACION?.CIUDAD || ''}, ${o?.ARTICULO?.UBICACION?.PROVINCIA || ''}`, align: 'left', sortable: true },
];

export const HIPOTECAS_TABLE_COLUMN: QTableColumn[] = [
  {
    label: 'Cod. Sol.',
    name: 'captacion.ID_CAPTACION',
    align: 'left',
    sortable: true,
    field: (o) => formatIdCaptacion(o.ID_CAPTACION),
  },
  {
    label: 'Fecha',
    name: 'captacion.FECHA_CAPTADO',
    field: (o) => formatDate(o.FECHA_CAPTADO),
    align: 'left',
    sortable: true,
  },
  {
    label: 'Estado',
    name: 'captacion.ESTADO',
    align: 'left',
    sortable: true,
    field: 'ESTADO',
  },
  { label: 'Tipo hipoteca', name: 'TIPO_HIPOTECA', field: 'PRODUCTOS', format: (p: any[]) => p?.[0]?.NOMBRE || '', align: 'left', sortable: true },
  { label: 'Nombre cliente', name: 'CONTACTO.NOMBRE', field: (o) => o?.CONTACTO?.PERSONA_CONTACTO || o?.CONTACTO?.NOMBRE, align: 'left', sortable: true },
  { label: 'Teléfono', name: 'TELEFONO.NUMERO', field: (o) => o?.CONTACTO?.TELEFONO?.NUMERO || '-', align: 'left', sortable: true },
  { label: 'Correo electrónico', name: 'CONTACTO.EMAIL', field: (o) => o?.CONTACTO?.EMAIL, align: 'left', sortable: true },
  // { label: 'Ubicación', name: 'UBICACION.PROVINCIA', field: (o) => `${o?.ARTICULO?.UBICACION?.CIUDAD || ''}, ${o?.ARTICULO?.UBICACION?.PROVINCIA || ''}`, align: 'left', sortable: true },
];

export const USUARIOS_TABLE_COLUMN: QTableColumn[] = [
  {
    label: 'Cod. Sol.',
    name: 'captacion.ID_CAPTACION',
    align: 'left',
    sortable: true,
    field: (o) => formatIdCaptacion(o.ID_CAPTACION),
  },
  {
    label: 'Fecha',
    name: 'captacion.FECHA_CAPTADO',
    field: (o) => formatDate(o.FECHA_CAPTADO),
    align: 'left',
    sortable: true,
  },
  {
    label: 'Estado',
    name: 'captacion.ESTADO',
    align: 'left',
    sortable: true,
    field: 'ESTADO',
  },
  { label: 'Razón social', name: 'CONTACTO.RAZON_SOCIAL', field: (o) => o?.CONTACTO?.RAZON_SOCIAL, align: 'left', sortable: true },
  { label: 'Nombre contacto', name: 'CONTACTO.NOMBRE', field: (o) => o?.CONTACTO?.PERSONA_CONTACTO || o?.CONTACTO?.NOMBRE, align: 'left', sortable: true },
  { label: 'Teléfono', name: 'TELEFONO.NUMERO', field: (o) => o?.CONTACTO?.TELEFONO?.NUMERO || '-', align: 'left', sortable: true },
  { label: 'Correo electrónico', name: 'CONTACTO.EMAIL', field: (o) => o?.CONTACTO?.EMAIL, align: 'left', sortable: true },
  {
    label: 'Ubicación',
    name: 'UBICACION.PROVINCIA',
    field: (o) => `${o?.CONTACTO?.DIRECCION?.CIUDAD || ''}${o?.CONTACTO?.DIRECCION?.CIUDAD ? ',' : ''} ${o?.CONTACTO?.DIRECCION?.PROVINCIA || ''}`,
    align: 'left',
    sortable: true,
  },
];

export const CAPTACION_COLUMNS = {
  Fincas: FINCAS_TABLE_COLUMN,
  Hipotecas: HIPOTECAS_TABLE_COLUMN,
  Usuarios: USUARIOS_TABLE_COLUMN,
};

export function getProductPropValue(product: Producto, code: string) {
  if (!product) {
    return '-';
  }
  const prop = product.PROPS[code];
  const gamaProp = product.R_GAMA.PROPS.filter((f) => f.code.toLowerCase() == code.toLowerCase())[0];
  let value = prop || '';
  if (isArray(prop)) {
    value = prop.join(', ') || '';
  }
  if (gamaProp && gamaProp.suffix) {
    value = value + ' ' + gamaProp.suffix;
  }
  return value;
}

export function getProductFromCaptacion(item: any) {
  if (item.TIPO === TipoCaptacion.PEDIDOS) {
    return item.PEDIDO?.R_PRODUCTOS_PEDIDO?.[0].R_PRODUCTO || {};
  }
  // agregar los demas casos (estandarizar)
  return {};
}

export const DEFAULT_FILTERS = {
  terms: '',
  bycol: {
    'UBICACION.ID_ZONA': null,
    'UBICACION.ID_PAIS': null,
  },
  fecha: null,
};

export const DEFAULT_CAPTACION_TABS = [
  { label: 'Estado', to: 'estado', name: 'general' },
  { label: 'Contacto', to: 'contacto', name: 'contact' },
  { label: 'Formulario', to: 'formulario', name: 'questions' },
  { label: 'Finca', to: 'producto', name: 'product' },
  { label: 'Cuenta bancaria', to: 'cuenta-bancaria', name: 'bank-account' },
  { label: 'Documentos', to: 'documentacion', name: 'documents' },
  { label: 'Incidencias', to: 'incidencias', name: 'incidents' },
  { label: 'Propuesta alquiler', to: 'cotizacion', name: 'budget' },
  { label: 'Contrato y firma', to: 'contrato', name: 'contract' },
] as const;

export const SEXOS = ['hombre', 'mujer'];
export const ESTADO_CIVIL = ['soltero', 'casado', 'divorciado', 'viudo', 'unión libre'];

export const NACIONALIDADES = [
  'Afgano',
  'Africano',
  'Albanés',
  'Alemán',
  'Andorrano',
  'Angoleño',
  'Antiguano',
  'Argelino',
  'Argentino',
  'Armenio',
  'Australiano',
  'Austriaco',
  'Azerbaiyano',
  'Bahameño',
  'Bareiní',
  'Bangladesí',
  'Barbadense',
  'Belga',
  'Beliceño',
  'Beninés',
  'Bielorruso',
  'Birmano',
  'Boliviano',
  'Bosnio',
  'Botsuano',
  'Brasileño',
  'Británico',
  'Bruneano',
  'Búlgaro',
  'Burkinés',
  'Burundés',
  'Butanés',
  'Cabo Verdiano',
  'Camboyano',
  'Camerunés',
  'Canadiense',
  'Centroafricano',
  'Chadiano',
  'Checo',
  'Chileno',
  'Chino',
  'Chipriota',
  'Colombiano',
  'Comorense',
  'Congoleño',
  'Costarricense',
  'Croata',
  'Cubano',
  'Danés',
  'Dominicano',
  'Ecuatoriano',
  'Egipcio',
  'Emiratí',
  'Eslovaco',
  'Esloveno',
  'Español',
  'Estadounidense',
  'Estonio',
  'Etíope',
  'Fiyiano',
  'Filipino',
  'Finlandés',
  'Francés',
  'Gabonés',
  'Gambiano',
  'Georgiano',
  'Ghanés',
  'Griego',
  'Guatemalteco',
  'Guineano',
  'Guyanés',
  'Haitiano',
  'Hondureño',
  'Húngaro',
  'Indio',
  'Indonesio',
  'Iraní',
  'Iraquí',
  'Irlandés',
  'Islandés',
  'Israelí',
  'Italiano',
  'Jamaicano',
  'Japonés',
  'Jordano',
  'Kazajo',
  'Keniano',
  'Kirguís',
  'Kiribatiano',
  'Kuwaití',
  'Laosiano',
  'Letón',
  'Libanés',
  'Liberiano',
  'Libio',
  'Liechtensteiniano',
  'Lituano',
  'Luxemburgués',
  'Macedonio',
  'Malasio',
  'Malauí',
  'Malgache',
  'Maliense',
  'Maltés',
  'Marfileño',
  'Marroquí',
  'Mauriciano',
  'Mauritano',
  'Mexicano',
  'Micronesio',
  'Moldavo',
  'Monegasco',
  'Mongol',
  'Montenegrino',
  'Mozambiqueño',
  'Namibio',
  'Neerlandés',
  'Nepalí',
  'Nicaragüense',
  'Nigeriano',
  'Norcoreano',
  'Noruego',
  'Neozelandés',
  'Omaní',
  'Pakistaní',
  'Palestino',
  'Panameño',
  'Papú',
  'Paraguayo',
  'Peruano',
  'Polaco',
  'Portugués',
  'Puertorriqueño',
  'Qatarí',
  'Ruandés',
  'Rumano',
  'Ruso',
  'Salvadoreño',
  'Samoano',
  'Sanmarinense',
  'Santotomense',
  'Saudí',
  'Senegalés',
  'Serbio',
  'Sierraleonés',
  'Singapurense',
  'Sirio',
  'Somalí',
  'Sri Lankaés',
  'Sudafricano',
  'Sudanés',
  'Sueco',
  'Suizo',
  'Surinamés',
  'Tailandés',
  'Tanzano',
  'Togolés',
  'Tongano',
  'Trinitense',
  'Tunecino',
  'Turco',
  'Tuvaluano',
  'Ucraniano',
  'Ugandés',
  'Uruguayo',
  'Uzbeco',
  'Vanuatuense',
  'Venezolano',
  'Vietnamita',
  'Yemení',
  'Yibutiano',
  'Zambiano',
  'Zimbabuense',
];
