import Common from '../common/Common';
import DataModelBase from '../framework/DataModelBase';

export default class WeekRank extends DataModelBase
{
    constructor(modelName)
    {
        super(modelName);
        this._arrCityRank = [];
        this._arrProvinceRank = [];
        this._arrCountryRank = [];
        this._arrCityRankBuff = [];
        this._arrProvinceRankBuff = [];
        this._arrCountryRankBuff = [];
    }

    getRankNum(rankType)
    {
        switch(rankType)
        {
            case Common.RankType.CityRank:
                return this._arrCityRank.length;
                break;
            case Common.RankType.ProvinceRank:
                return this._arrProvinceRank.length;
                break;
            case Common.RankType.CountryRank:
                return this._arrCountryRank.length;
                break;
        }
    }

    clearRank(rankType)
    {
        switch(rankType)
        {
            case Common.RankType.CityRank:
                this._arrCityRank.length = 0;
                break;
            case Common.RankType.ProvinceRank:
                this._arrProvinceRank.length = 0;
                break;
            case Common.RankType.CountryRank:
                this._arrCountryRank.length = 0;
                break;
        }
    }

    clearBuff(rankType)
    {
        switch(rankType)
        {
            case Common.RankType.CityRank:
                this._arrCityRankBuff.length = 0;
                break;
            case Common.RankType.ProvinceRank:
                this._arrProvinceRankBuff.length = 0;
                break;
            case Common.RankType.CountryRank:
                this._arrCountryRankBuff.length = 0;
                break;
        }
    }

    addRank (rankType, rankInfo)
    {
        switch(rankType)
        {
            case Common.RankType.CityRank:
                this._arrCityRankBuff.push(rankInfo);
                break;
            case Common.RankType.ProvinceRank:
                this._arrProvinceRankBuff.push(rankInfo);
                break;
            case Common.RankType.CountryRank:
                this._arrCountryRankBuff.push(rankInfo);
                break;
        }
    }

    swapBuff(rankType)
    {
        this.clearRank(rankType);
        switch(rankType)
        {
            case Common.RankType.CityRank:
                for (var i = 0; i < this._arrCityRankBuff.length; ++i)
                {
                    this._arrCityRank.push(this._arrCityRankBuff[i]);
                }
                this._arrCityRankBuff.length = 0;
                break;
            case Common.RankType.ProvinceRank:
                for (var i = 0; i < this._arrProvinceRankBuff.length; ++i)
                {
                    this._arrProvinceRank.push(this._arrProvinceRankBuff[i]);
                }
                this._arrProvinceRankBuff.length = 0;
                break;
            case Common.RankType.CountryRank:
                for (var i = 0; i < this._arrCountryRankBuff.length; ++i)
                {
                    this._arrCountryRank.push(this._arrCountryRankBuff[i]);
                }
                this._arrCountryRankBuff.length = 0;
                break;
        }
    }

    cityRank()
    {
        if (this._arrCityRank.length > 1)
            this._arrCityRank.sort(this.sort);
    }

    provinceRank()
    {
        if (this._arrProvinceRank.length > 1)
            this._arrProvinceRank.sort(this.sort);
    }

    countryRank()
    {
        if (this._arrcountryRank.length > 1)
            this._arrcountryRank.sort(this.sort);
    }

    sort(r1, r2)
    {
        if (r1.weekScore > r2.weekScore)
            return -1;
        return 1;
    }

    getRank(rankType, id)
    {
        switch(rankType)
        {
            case Common.RankType.CityRank:
                if (id < 0 || id >= this._arrCityRank.length)
                    return null;
                return this._arrCityRank[id];
                break;
            case Common.RankType.ProvinceRank:
                if (id < 0 || id >= this._arrProvinceRank.length)
                    return null;
                return this._arrProvinceRank[id];
                break;
            case Common.RankType.CountryRank:
                if (id < 0 || id >= this._arrCountryRank.length)
                    return null;
                return this._arrCountryRank[id];
                break;
        }
    }

    getSelfRankID(rankType)
    {
        switch(rankType)
        {
            case Common.RankType.CityRank:
                for (var i = 0; i < this._arrCityRank.length; ++i)
                {
                    var info = this._arrCityRank[i];
                    if (!info)
                        continue;
        
                    if (info.headUrl == this.selfUrl && info.nickName == this.selfName)
                        return i;
                }
                break;
            case Common.RankType.ProvinceRank:
                for (var i = 0; i < this._arrProvinceRank.length; ++i)
                {
                    var info = this._arrProvinceRank[i];
                    if (!info)
                        continue;
        
                    if (info.headUrl == this.selfUrl && info.nickName == this.selfName)
                        return i;
                }
                break;
            case Common.RankType.CountryRank:
                for (var i = 0; i < this._arrCountryRank.length; ++i)
                {
                    var info = this._arrCountryRank[i];
                    if (!info)
                        continue;
        
                    if (info.headUrl == this.selfUrl && info.nickName == this.selfName)
                        return i;
                }
                break;
        }
        return -1;
    }
}