text: const URL = require('./baseurl');
  const baseURL = URL.baseURL;
  
  class MainController extends Controller {
  
      load(data) {
          this.id = data.id;
          this.url = data.url;
          this.page_num = -1;
          this.page = 0;
  
          var cached = this.readCache();
          let list;
          if (cached) {
              list = cached.items;
          } else {
              list = [];
          }
  
          this.data = {
              list: list,
              loading: false,
              hasMore: this.id !== 'home'
          };
  
          this.userAgent = 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.102 Mobile Safari/537.36';
  
          if (cached) {
              let now = new Date().getTime();
              if (now - cached.time > 30 * 60 * 1000) {
                  this.reload();
              }
          } else {
              this.reload();
          }
  
      }
  
      async onPressed(index) {
          await this.navigateTo('book', {
              data: this.data.list[index]
          });
      }
  
      onRefresh() {
          this.reload();
      }
  
      async onLoadMore() {
          this.setState(() => {
              this.data.loading = true;
          });
          try {
              let page = this.page + 1;
              let url = this.makeURL(page);
              let res = await fetch(url, {
                  headers: {
                      'User-Agent': this.userAgent,
                  },
              });
              let text = await res.text();
              this.page = page;
              let items = this.parseData(text, url);
      
              this.setState(()=>{
                  for (let item of items) {
                      this.data.list.push(item);
                  }
                  this.data.loading = false;
                  this.data.hasMore = this.id != 'home' && (this.page < this.page_num - 1 || this.page == -1);
                  console.log(`id: ${this.id} ${this.data.hasMore}`)
              });
          } catch (e) {
              showToast(`${e}\n${e.stack}`);
              this.setState(()=>{
                  this.data.loading = false;
              });
          }
          
      }
  
      makeURL(page) {
          if (this.id == 'home') {
              return this.url;
          } else if (page != 0) {
              return this.url.replace('.html', `-page-${page + 1}.html`);
          } else {
              return this.url;
          }
      }
  
      async reload() {
          this.setState(() => {
              this.data.loading = true;
          });
          try {
              let url = this.makeURL(0);
              let res = await fetch(url, {
                  headers: {
                      'User-Agent': this.userAgent,
                  },
              });
              let text = await res.text();
              let items = this.parseData(text, url);
              this.page = 0;
              localStorage['cache_' + this.id] = JSON.stringify({
                  time: new Date().getTime(),
                  items: items,
              });
              this.setState(()=>{
                  this.data.list = items;
                  this.data.loading = false;
                  this.data.hasMore = this.id != 'home' && (this.page < this.page_num - 1 || this.page == -1);
              });
          } catch (e) {
              showToast(`${e}\n${e.stack}`);
              this.setState(()=>{
                  this.data.loading = false;
              });
          }
      }
  
      readCache() {
          let cache = localStorage['cache_' + this.id];
          if (cache) {
              let json = JSON.parse(cache);
              return json;
          }
      }
  
      parseData(text, url) {
          if (this.id == 'home') {
              return this.parseHomeData(text, url);
          } else {
              return this.parsePageData(text, url);
          } 
      }
  
      parseHomeData(html, url) {
          const doc = HTMLParser.parse(html);
          let results = [];
          let content = doc.querySelectorAll('.bg-white')[1];
          let comic_items = content.querySelectorAll('.px-1');
          let categroy_title = [];
          for (let item of content.querySelectorAll('.justify-content-between')) {
              categroy_title.push(item.querySelector('h2').textContent);
          }
          results.push({
              header: true,
              title: categroy_title[0],
              icon: ''
          });
          for (let i = 0; i < 12; i++) {
              let img_link = comic_items[i].querySelector('img').getAttribute('src');
              if (img_link.search(baseURL.substring(12, baseURL.length)) == -1) {
                  img_link = baseURL + img_link;
              }
              results.push({
                  title: comic_items[i].querySelector('h2').textContent,
                  subtitle: '',
                  link: baseURL + comic_items[i].querySelector('a').getAttribute('href'),
                  picture: img_link,
                  pictureHeaders: {
                      Referer: baseURL
                  },
              });
          }
          for (let i = 1; i < 4; i++) {
              results.push({
                  header: true,
                  title: categroy_title[i],
                  icon: ''
              });
              let index_list = [i * 9 + 3, i * 9 + 4, i * 9 + 5, i * 9 + 6, i * 9 + 7, i * 9 + 8, i * 9 + 9, i * 9 + 10, i * 9 + 11];
              for (let index of index_list) {
                  let img_link = comic_items[index].querySelector('img').getAttribute('src');
                  if (img_link.search(baseURL.substring(12, baseURL.length)) == -1) {
                      img_link = baseURL + img_link;
                  }
                  results.push({
                      title: comic_items[index].querySelector('a').getAttribute('title'),
                      subtitle: '',
                      link: baseURL + comic_items[index].querySelector('a').getAttribute('href'),
                      picture: img_link,
                      pictureHeaders: {
                          Referer: baseURL
                      },
                  });
              }
          }
          return results;
      }
  
      parsePageData(text, url) {
          const doc = HTMLParser.parse(text);
          if (this.page_num == -1) {
              let page_num_info = doc.querySelectorAll('.btn')[6].getAttribute('href');
              this.page_num = parseInt(page_num_info.substring(22, page_num_info.length - 5));
          }
          let results = [];
          let boxes = doc.querySelectorAll('.comic-book-unit');
          for (let box of boxes) {
              let item_info = box.querySelectorAll('a');
              let item = {};
              item.link = baseURL + item_info[0].getAttribute('href');
              item.picture = box.querySelector('img').getAttribute('src');
              item.title = item_info[1].textContent;
              item.subtitle = box.querySelector('.list-inline-item').textContent;
              item.pictureHeaders = {
                  Referer: baseURL
              };
              results.push(item);
          }
          return results;
      }
  }
  
  module.exports = MainController;
  