import { ImagePosition, TitlePosition } from '@dvn/alaca/components/dvn-card-header/models';
import { CompositeViewLinearConfig } from '@dvn/core/components/views/dvn-app-view-composite-view-linear/composite-view-linear-config';
import { AttachmentField } from '@dvn/core/doldurma/src/fields/attachment';
import { TextField } from '@dvn/core/doldurma/src/fields/text';
import { InvalidationAction } from '@dvn/core/models/actions';
import { AppConfig } from '@dvn/core/models/app-config';
import { DetailViewConfig } from '@dvn/core/models/detail-view-config';
import { TableConfig } from '@dvn/core/models/table-config';
import { AppViewType } from '@dvn/core/models/view-config';
import { TakvimAvailableTimePickerViewConfig } from '@dvn/takvim/views/dvn-app-view-takvim-available-time-picker-view/takvim-available-time-picker-view-config';
import { InfoVisibility } from '../../../../mentornity-js-client/models';
import { generateCalendarConfig, getEmbeddedTranslation } from '../../api/mentorinty-bridge-api';
import { generateMembershipConfigForRole } from '../../util/session';
import { getMembershipFields, getUserDetailViewFieldConfigs } from '../common-wrapper-util';
import { TakvimMeetingListViewConfig } from "@dvn/takvim/views/dvn-app-view-takvim-meeting-list-view-table/takvim-meeting-list-view-config";
export function getAppConfig(mentornityCookieContext, organisationConfig, language, userRole, viewedUserRole) {
  const showMessageButton = viewedUserRole === 'mentor'
    ? [
      InfoVisibility.allMentors,
      InfoVisibility.everyone,
    ].includes(organisationConfig.conversationPermissions.mentee)
    : [
      InfoVisibility.allMentees,
      InfoVisibility.everyone,
    ].includes(organisationConfig.conversationPermissions.mentor);
  const actions = [];
  if (userRole === 'mentee' && viewedUserRole === 'mentor') {
    // show follow buttons when mentee is viewing mentor detail
    actions.push({
      ui: {
        componentTag: 'mentornity-follow-button',
        name: 'toggle-follow',
      },
      data: {
        language,
        mentornityCookieContext,
      }
    });
  }
  if (showMessageButton) {
    actions.push({
      ui: {
        label: getEmbeddedTranslation(language, 'sendMessage'),
        name: 'send-message',
        view: 'flat'
      }
    });
  }
  return AppConfig.decode({
    views: [
      DetailViewConfig.decode({
        _id: 'user-detail-page-summary-card-view',
        type: AppViewType.DetailViewCard,
        table: TableConfig.decode({
          fields: [
            new TextField('user.name', ''),
            new TextField('user.initials', ''),
            new AttachmentField('user.imageUrl', ''),
          ],
          roleName: viewedUserRole,
          name: `${viewedUserRole}-detail-view`,
        }),
        ui: {
          cardConfig: {
            header: {
              title: {
                fieldId: 'user.name',
                position: TitlePosition.RightOfImage,
              },
              image: {
                fieldId: 'user.imageUrl',
                placeholderFieldId: 'user.initials',
                position: ImagePosition.BottomLeft,
              },
              cover: {
                // placeholder: 'https://thumbs.dreamstime.com/z/books-wooden-desk-table-abstract-background-education-copy-space-back-to-school-184388515.jpg',
                size: 88,
              },
            },
            footer: {
              actions,
            }
          }
        },
      }),
      getDetailViewUIConfig(mentornityCookieContext, language, organisationConfig, viewedUserRole),
      TakvimAvailableTimePickerViewConfig.decode({
        _id: 'user-detail-page-available-time-picker-view',
        i18nEntryId: 'user-detail-page-available-time-picker-view',
        type: AppViewType.TakvimAvailableTimePickerView,
        table: generateCalendarConfig(organisationConfig),
        events: {
          success: [
            new InvalidationAction('user-detail-page-available-time-picker-view', 'user-detail-page-meeting-list-view'),
          ]
        },
        successNavigationAction: {
          type: 'navigation',
          direction: 'forward',
          name: 'open-meeting-detail-after-creation',
          params: {
            conversionConfig: {
              'item._id': 'meetingId',
            }
          }
        },
      }),
      TakvimMeetingListViewConfig.decode({
        _id: 'user-detail-page-meeting-list-view',
        i18nEntryId: 'user-detail-page-meeting-list-view',
        type: AppViewType.TakvimMeetingListViewTable,
        table: generateCalendarConfig(organisationConfig),
      }),
    ]
  });
}
export function getDetailViewUIConfig(mentornityCookieContext, language, config, viewedUserRole) {
  if (!config) {
    return null;
  }
  const fields = getMembershipFields(language, config, viewedUserRole, mentornityCookieContext.role);
  const summaryCardFields = getUserDetailViewFieldConfigs(language, config, viewedUserRole, mentornityCookieContext.role, true, 3);
  const detailCardFields = getUserDetailViewFieldConfigs(language, config, viewedUserRole, mentornityCookieContext.role, false, 0);
  return DetailViewConfig.decode({
    _id: 'user-detail-page-info-view',
    i18nEntryId: 'user-detail-page-info-view',
    type: AppViewType.DetailViewCard,
    table: TableConfig.decode({
      fields,
      roleName: viewedUserRole,
      name: `${viewedUserRole}-detail-view`,
    }),
    ui: {
      summaryCardConfig: {
        body: {
          fields: summaryCardFields,
        }
      },
      cardConfig: {
        body: {
          fields: detailCardFields,
        }
      },
    },
  });
}
export function getCompositeViewUIConfig(organisationConfig, language, userId, groupId, userRole, viewedUserRole) {
  const views = [
    {
      viewId: 'user-detail-page-summary-card-view',
    },
    {
      title: getEmbeddedTranslation(language, 'info').toLocaleUpperCase(),
      viewId: 'user-detail-page-info-view',
    }
  ];
  const mentorViewingMenteeProfileInOneToOneProgram = userRole === 'mentor' && viewedUserRole === 'mentee'
    && organisationConfig.menteeType !== 'group';
  // no need to check if it's one-to-one because mentees can view their group's meetings in mentor detail page
  const menteeViewingMentorProfile = userRole === 'mentee' && viewedUserRole === 'mentor';
  if (mentorViewingMenteeProfileInOneToOneProgram || menteeViewingMentorProfile) {
    views.push({
      title: getEmbeddedTranslation(language, 'getMeeting').toLocaleUpperCase(),
      viewId: 'user-detail-page-available-time-picker-view',
      params: {
        viewedUserMembershipConfig: generateMembershipConfigForRole(viewedUserRole),
        conversionConfig: {
          // viewedUserMembership will be provided by
          // the dvn-app-view-composite-view-linear with the name 'item'
          // we're setting the conversion config to convert the 'item' into 'viewedUserMembership'
          item: 'viewedUserMembership',
        }
      },
    }, {
      title: getEmbeddedTranslation(language, 'meetings').toLocaleUpperCase(),
      viewId: 'user-detail-page-meeting-list-view',
      params: {
        guest1Id: groupId || userId,
        conversionConfig: {
          // viewedUserMembership will be provided by
          // the dvn-app-view-composite-view-linear with the name 'item'
          // we're setting the conversion config to convert the 'item.user._id' into 'guest2Id'
          'item.user._id': 'guest2Id',
        }
      },
    });
  }
  return CompositeViewLinearConfig.decode({
    _id: 'mentornity-user-detail-page',
    table: {
      name: viewedUserRole === 'mentee' ? 'mentees' : 'mentors',
    },
    type: AppViewType.CompositeViewLinear,
    params: undefined,
    ui: {
      layout: 'vertical',
      views,
    }
  });
}
