package tkio.task;

import common.context.AppUtils;
import common.model.AppCategory;
import common.model.AppInfo;
import common.repository.AppCategoryRepository;
import common.repository.AppInfoRepository;
import common.repository.CityRepository;
import common.repository.UserRepository;
import dmp.model.TagCrawlerAppsWandoujia;
import dmp.repository.TagCrawlerAppsWandoujiaRepository;
import org.springframework.beans.factory.annotation.Autowired;
import security.annotation.Authorization;
import tkio.model.App;
import tkio.repository.AccountRepository;
import util.DateUtil;
import util.StringUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Created by zxy on 2017/12/26.
 */
public class SyncAppDataTask
{

    @Autowired TagCrawlerAppsWandoujiaRepository tagRepository;
    @Autowired AppInfoRepository appInfoRepository;
    @Autowired AppCategoryRepository appCategoryRepository;
    @Autowired CityRepository cityRepository;

    public void syncAppData()
    {
        System.out.println(DateUtil.getBeforeDays(1));
        List<TagCrawlerAppsWandoujia> list = tagRepository.findAllByDs("2017-12-15");
        List<String> citys = cityRepository.findCitys();
        List<AppInfo> appInfos = new ArrayList<>();
        List<AppCategory> appCategories = appCategoryRepository.findAll();
        Map<String, List<String>> map = new HashMap<>();
        for (AppCategory appCategory : appCategories) {
            List<String> innerList = map.get(appCategory.getLevel());
            if (innerList == null) {
                innerList = new ArrayList<>();
            }
            innerList.add(appCategory.getName());
            map.put(appCategory.getLevel(), innerList);
        }

        List<AppCategory> newCategorys = new ArrayList<>();
        List<AppInfo> newInfos = new ArrayList<>();
        for (TagCrawlerAppsWandoujia tag : list) {
            AppInfo appInfo = new AppInfo();
            appInfo.setName(tag.getName());
            appInfo.setCompany(tag.getMaker());
            appInfo.setOs("Android");
            appInfo.setPkgName(tag.getPkgname());
            appInfo.setLogoUrl(tag.getApplogo_link());
            String types = tag.getMix_types();
            if (!StringUtil.isEmpty(types)) {
                String[] typeArray = types.split("_");
                String otherCase = "";
                for (int i=0; i<typeArray.length; i++) {
                    if (map.containsKey(String.valueOf(i+1)) && !map.get(String.valueOf(i+1)).contains(typeArray[i])) {
                        List<String> mapValue = map.get(String.valueOf(i+1));
                        AppCategory newCate = new AppCategory();
                        newCate.setName(typeArray[i]);
                        newCate.setLevel(String.valueOf(i+1));
                        newCategorys.add(newCate);
                        mapValue.add(typeArray[i]);
                        map.put(String.valueOf(i+1), mapValue);
                    } else if (!map.containsKey(String.valueOf(i+1))) {
                        AppCategory newCate = new AppCategory();
                        newCate.setName(typeArray[i]);
                        newCate.setLevel(String.valueOf(i+1));
                        newCategorys.add(newCate);
                        List<String> mapValue = new ArrayList<>();
                        mapValue.add(typeArray[i]);
                        map.put(String.valueOf(i+1), mapValue);
                    }
                    if (i == 0) {
                        appInfo.setFirstCate(typeArray[i]);
                    }
                    if (i == 1) {
                        appInfo.setSecondCate(typeArray[i]);
                    }
                    if (i == 2) {
                        appInfo.setThirdCate(typeArray[i]);
                    }
                    if (i > 2) {
                        otherCase += "_" + typeArray[i];
                    }
                }
                if (otherCase.length() > 0) {
                    appInfo.setOtherCate(otherCase.substring(1));
                }
            }
            for (String city : citys) {
                if (tag.getMaker().indexOf(city) > -1) {
                    appInfo.setLocation(city);
                    break;
                }
            }
            newInfos.add(appInfo);
            System.out.println(appInfo);
        }
        appInfoRepository.save(newInfos);
        appCategoryRepository.save(newCategorys);
    }

    public static void main(String[] args)
    {
        SyncAppDataTask task = new SyncAppDataTask();
        task.syncAppData();
    }
}
