import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { api } from '@enums/api.enum';
import { GJainStorage } from '@enums/gjain-storage.enum';
import {
  BloodGroup, Country,
  Dharma, GeneralData, Jaati,
  MTongue,
  Message,
  ProfileData,
  Sampraday,
  addPassiveUser,
  viharInfo
} from '@interfaces/common';
import { Storage } from '@ionic/storage';
import { BehaviorSubject, Subject } from 'rxjs';

@Injectable({
  providedIn: 'root'
})
export class StateManagementService {
  generalData: GeneralData = {
    profile_completion_percentage: 0,
    pending_friend_request: 0,
    pending_sangh_invitation: 0,
    pending_sant_action: 0,
    sangh_menu: 0,
    sant_menu: 0,
    shravak_menu: 0,
    unread_message_count: 0,
    unread_notification_count: 0,
  };
  storage: Storage = new Storage({});
  countryDetails: Array<Country> = [];
  dharmaDetails: Array<Dharma> = [];
  jaatiDetails: Array<Jaati> = [];
  motherTongueDetails: Array<MTongue> = [];
  sampradayDetails: Array<Sampraday> = [];
  bloodGroupDetails: Array<BloodGroup> = [];
  chaturmasYear = [];
  userInformation: addPassiveUser = {};
  userProfileData: ProfileData;
  postType: string;
  postTypeList = [];
  userSanghList = [];
  noDataFlag = false;
  imageArray: Array<{ imgString: string; itemImage: Blob }> = [];
  santDetails: any;
  postTypeSubject = new Subject();
  postLocationSubject = new Subject();
  postLocationLatSubject = new Subject();
  postLocationLongSubject = new Subject();
  postSelectedSanghSubject = new Subject<number[]>();
  viharInfo: viharInfo;
  messageObservable = new Subject<Message>();
  messageDetailObservable = new Subject<Message>();
  messageClearObservable = new Subject();
  messageThreadRemoveObservable = new Subject<number>();
  pushNotyObservable = new BehaviorSubject<GeneralData>(this.generalData);
  shareData: any = null;
  common: any;

  constructor(
    private http: HttpClient,
  ) {
    this.storage.create();
  }

  get userData(): string | null {
    return localStorage.getItem(GJainStorage.userData);
  }

  get token(): string | null {
    return localStorage.getItem(GJainStorage.token);
  }

  set token(v: string) {
    localStorage.setItem(GJainStorage.token, v);
  }

  fetchNotificationCounts() {
    if (!this.token) { return; }
    this.http.get(api.unreadCounts)
      .subscribe((res: { data: GeneralData }) => {
        this.generalData = res.data;
        this.pushNotyObservable.next(this.generalData);
      });
  }

}
