import { PublicAction } from "../actions-type";
import _ from 'lodash';
import location from '../../util/location';

const initialState = {
  theme : localStorage.getItem('theme') || 'light',
  homeData: [],
  newProducts: [],
  levels: [],
  loading: false,
  error: null,
  searchResult: [],
  arList: [],
  blogList: [],
  blog: null,
  province: null,
  city: null,
  departmans: [
    {
      id: 0,
      name: "محتوا",
    },
    {
      id: 0,
      name: "دیزاین",
    },
    {
      id: 0,
      name: "برنامه نویسی",
    },
  ],
  header: [],
  selectedMenu: null,
  contactData: null,
  selectedVideo: null,
  bookInfo: null,
  activeCategory: null,
  categories: null,
  bookSection: null,
};

const grades = [
  "پیش دبستان",
  "اول",
  "دوم",
  "سوم",
  "چهارم",
  "پنجم",
  "ششم",
  "هفتم",
  "هشتم",
  "نهم",
  "دهم",
  "یازدهم",
  "دوازدهم",
];
export default function publicApi( state = initialState, action: PublicAction) {
  let { type, data } = action;
  switch (type) {
    case "public/setTheme":
      localStorage.setItem("theme", data);
      return { ...state, location: false, error : null, theme: data };
    case "active/video":
      return {
        ...state,
        loading: false,
        error: null,
        selectedVideo: data,
      };
    case "active/category":
      return {
        ...state,
        loading: false,
        activeCategory: state.activeCategory === data ? null : data,
        error: null,
      };
    case "public/vod/bookSection":
      const videoId: any = location.getId();
      return {
        ...state,
        loading: false,
        bookSection: data,
        error: null,
        selectedVideo: data.vods?.filter((item: any) => item.id !== videoId)[0]
      };

    case "public/vod/bookCourse":
      let allCats;
      allCats = Array.from(
        new Set(
          data.vods.map((item: any) => item.categoryId && item.categoryId)
        )
      );
      // allCats =  _.sortedUniq(allCats);
      allCats.sort((a : any , b : any) => a - b);
      return {
        ...state,
        loading: false,
        bookInfo: {
          ...data,
          vods: data.vods.map((item: any) => {
            return { ...item, active: false };
          }),
        },
        error: null,
        categories: allCats.map((item: any) =>
          data.vods.filter((item1: any) => item1.categoryId === item)
        ),
        selectedVideo: data.vods.filter((item : any) => item.id !== location.getId())[0],
      };
    case "public/homePage":
      let itemsLevel;
      if (window.innerWidth > 1000) {
        const jadidTarin = data.filter(
          (item: any) => item.title === "جدیدترین کتاب‌های دانوین"
        );
        itemsLevel = jadidTarin[0].data
          .map((item: any) => item.book.gradeId * 1)
          .sort((a: any, b: any) => a - b)
          .map((e: any) => grades[e]);
        itemsLevel.unshift("همه");
      }

      return {
        ...state,
        loading: false,
        levels: window.innerWidth > 1000 ? Array.from(new Set(itemsLevel)) : [],
        error: null,
        homeData: data,
      };
    // case "public/filter":
    //   const realList =
    //     state.newProducts.length === 0
    //       ? state.homeData.filter(
    //           (item) => item.title === "جدیدترین کتاب‌های دانوین"
    //         )[0].data
    //       : state.newProducts;
    //   if (state.newProducts.length === 0) {
    //     state = {
    //       ...state,
    //       newProducts: realList,
    //     };
    //     console.log("here1");
    //   }
    //   const level = data.levels;
    //   const search = data.search;
    //   if (level.length > 0 || search) {
    //     localStorage.setItem("filter", "ON");
    //   } else {
    //     return {
    //       ...state,
    //       loading: false,
    //       error: null,
    //       homeData: state.homeData.map((item) =>
    //         item.title === "جدیدترین کتاب‌های دانوین"
    //           ? { ...item, data: state.newProducts }
    //           : { ...item }
    //       ),
    //       newProducts: [],
    //     };
    //   }
    //   let byLevelFilter = [];
    //   if (level.length > 0) {
    //     level.map((item1) => {
    //       for (let i = 0; i < realList.length; i++) {
    //         if (grades[realList[i].book.gradeId] === item1) {
    //           byLevelFilter.push(realList[i]);
    //         }
    //       }
    //     });
    //   } else {
    //     byLevelFilter = realList;
    //   }

    //   // let bySubjectFilter = [];
    //   // if (subject.length > 0) {
    //   //   subject.map((item1) => {
    //   //     for (let i = 0; i < byLevelFilter.length; i++) {
    //   //       if (byLevelFilter[i].name === item1) {
    //   //         bySubjectFilter.push(byLevelFilter[i]);
    //   //       }
    //   //     }
    //   //   });
    //   // } else {
    //   //   bySubjectFilter = byLevelFilter;
    //   // }

    //   const bySearchFilter = search
    //     ? byLevelFilter.filter((item1) => item1.name.indexOf(search) !== -1)
    //     : byLevelFilter;
    //   // if (sort === "گران ترین") {
    //   //   finalSort = bySearchFilter.sort((item1, item2) => {
    //   //     return item1.product[0].price - item2.product[0].price;
    //   //   });
    //   // } else {
    //   //   finalSort = bySearchFilter;
    //   // }

    //   return {
    //     ...state,
    //     loading: false,
    //     error: null,
    //     homeData: state.homeData.map((item) =>
    //       item.title === "جدیدترین کتاب‌های دانوین"
    //         ? { ...item, data: bySearchFilter }
    //         : { ...item }
    //     ),
    //   };
    // case "faq/list":
    //   return { ...state, loading: false, error: null, faqList: data };
    // case "public/faq/search":
    //   return {
    //     ...state,
    //     loading: false,
    //     error: null,
    //     searchResult: state.faqList.filter(
    //       (item) =>
    //         item.title.indexOf(data) !== -1 ||
    //         item.description.indexOf(data) !== -1
    //     ),
    //   };
    case "public/contact":
      return { ...state, loading: false, error: null, contactData: data };
    // case "public/ar":
    //   return { ...state, loading: false, error: null, arList: data };
    case "public/blogList":
      return { ...state, loading: false, error: null, blogList: data };
    case "public/blogInfo":
      return { ...state, loading: false, error: null, blog: data };
    case "public/setSubscribe":
      return { ...state, loading: false, error: null, selectedSubscribe: data };
    case "public/province":
      return { ...state, loading: false, error: null, province: data };
    case "public/city":
      return { ...state, loading: false, error: null, city: data };
    case "public/selectMenu":
      return { ...state, loading: false, error: null, selectedMenu: data };
    case "public/VOD/homePage":
      return { ...state, loading: false, error: null, header: data };
    case "loading":
      return { ...state, loading: true };
    case "error":
      return { ...state, loading: false, error: data.message };
    default:
      return state;
  }
}
