import { Component, OnInit } from '@angular/core';
import { Share, ShareOptions } from '@capacitor/share';
import { IonicModule, NavController } from '@ionic/angular';

import { NgFor, NgIf } from '@angular/common';
import { FooterComponent } from '@components/footer/footer.component';
import { MessageNotificationBadgeComponent } from '@components/message-notification-badge/message-notification-badge.component';
import { ProfileCompleteIndicatorComponent } from '@components/profile-complete-indicator/profile-complete-indicator.component';
import { api } from '@enums/api.enum';
import { constants } from '@enums/constants.enum';
import { GJainStorage } from '@enums/gjain-storage.enum';
import { Roots } from '@enums/root.enum';
import { CommonResponse, FriendList, ProfileData, ProfileResponse, UserData } from '@interfaces/common';
import { HttpRequestService } from '@services/http-request.service';
import { StateManagementService } from '@services/state-management.service';
import { UtilityService } from '@services/utility.service';
import { CertificateBannerComponent } from '../certificate-banner/certificate-banner.component';

@Component({
  selector: 'app-menu',
  templateUrl: './menu.page.html',
  styleUrls: ['./menu.page.scss'],
  standalone: true,
  imports: [IonicModule, MessageNotificationBadgeComponent, NgIf, CertificateBannerComponent, ProfileCompleteIndicatorComponent, NgFor, FooterComponent]
})
export class MenuPage implements OnInit {
  userProfileData: ProfileData;
  friendList: Array<FriendList> = [];
  friendListArray: Array<FriendList> = [];
  userData: UserData;

  constructor(
    private stateManagementService: StateManagementService,
    private navController: NavController,
    private httpRequestService: HttpRequestService,
    public utilityService: UtilityService
  ) { }

  ngOnInit() {
    this.getFriendList();
  }

  ionViewWillEnter() {
    this.getProfileInfo();
  }

  getProfileInfo() {
    this.httpRequestService.get(api.shravakProfile).then((res: ProfileResponse) => {
      this.userProfileData = res.data;
      this.stateManagementService.userProfileData = res.data;
      this.stateManagementService.storage.set(GJainStorage.profileData, res.data);
    });
  }

  getFriendList() {
    this.httpRequestService.get(api.friendList).then((res: CommonResponse) => {
      this.friendListArray = res.data;
      if (res.data.length > 3) {
        res.data.forEach((element, i) => {
          if (i < 3) {
            this.friendList.push(element);
          }
        });
      } else {
        this.friendList = res.data;
      }
    });
  }

  async logout() {
    const isYes = await this.utilityService.presentConfirm('Are you sure to logout?');
    if (isYes) {
      this.utilityService.logout();
    }
  }

  goToProfile() {
    this.navController.navigateForward(Roots.page + '/' + Roots.shravakViewProfile);
  }

  viewPublicProfile(id: number) {
    this.navController.navigateForward([Roots.page + '/' + Roots.shravakOtherProfile, { id }]);
  }

  visibilityPolicy() {
    this.navController.navigateForward([Roots.page + '/' + Roots.visibilityPolicy]);
  }

  termsPolicy() {
    this.navController.navigateForward([Roots.page + '/' + Roots.termsPolicy]);
  }

  goToFriendList() {
    this.navController.navigateForward([Roots.page + '/' + Roots.friendRequest]);
  }

  goToSettings() {
    this.navController.navigateForward([Roots.page + '/' + Roots.settings]);
  }

  async inviteUser() {
    const options: ShareOptions = {
      text: 'Global Jain lets you follow Sant real time,' +
        ' stay tuned with Sangh events and will also let us follow each other\'s activities.' +
        ' Click here to join - It\'s free: ',
      url: constants.inviteLink,
    };
    await Share.share(options);
  }

  goToYoutube() {
    this.utilityService.openBrowser(constants.youtubeLink);
  }

  goToContribution() {
    this.utilityService.openBrowser(constants.contributionLink);
  }

  goToCertificate() {
    this.navController.navigateForward([Roots.page + '/' + Roots.certificate]);
  }

  editProfile() {
    this.navController.navigateForward([Roots.page, Roots.shravakAbout], {
      state: {
        from: 'edit'
      },
    });
  }

}
