public with sharing class OFSBulletinBoardController {
    public String pType {get; private set;}
    public List<HBBInfo> hbbInfoList {get; private set;}
    public void init(){
        hbbInfoList = new List<HBBInfo>();
        pType = System.currentPageReference().getParameters().get('type');
        // TODO 公開開始日<=today && 公開終了日>today
        // List<HBBInfo> tmpList = new List<HBBInfo>();
        // 今回は1種類しか出さないため、tmpList.sort(); が要らない、そのまま hbbInfoList.add()
        if (pType == 'Product_Documentation__c') {
            List<Product_Documentation__c> pdList = [select Id, Material_open_day__c, Material_category__c, Product_document_name__c
                                          from Product_Documentation__c 
                                          where Material_status__c = '公开中'
                                          order by Material_open_day__c desc limit 20];
            for (Product_Documentation__c pd : pdList) {
                hbbInfoList.add(new HBBInfo(pd));
            }
        }
        else if (pType == 'Solution') {
            List<Solution> faqList = [select Id, FAQ_disclosed_day__c, Question_category__c, SolutionName
                                          from Solution
                                          where Status = 'レビュー済み'
                                          order by FAQ_disclosed_day__c desc limit 20];
            for (Solution faq : faqList) {
                hbbInfoList.add(new HBBInfo(faq));
            }
        }
        else if (pType == 'Campaign') {
            List<Campaign> camList = [select Id, StartDate, RecordType.Name, Name
                                          from Campaign
                                          where (Status = '公开中' or Status = '已结束' or Status = '已提交报告')
                                          order by StartDate desc limit 20];
            for (Campaign cam : camList) {
                hbbInfoList.add(new HBBInfo(cam));
            }
        }
        else {
            List<Bulletin_Board__c> bbList = [select Id, Board_open_day__c, Board_category1__c, Name
                                          from Bulletin_Board__c
                                          where Board_status__c = '公开中'
                                          order by Board_open_day__c desc limit 20];
            for (Bulletin_Board__c bb : bbList) {
                hbbInfoList.add(new HBBInfo(bb));
            }
        }
    }
    public class HBBInfo implements Comparable {
        public Id linkId {get; private set;}
        public Date openday {get; private set;}
        public String daiBunrui {get; private set;}
        public String bunrui {get; private set;}
        public String subject {get; private set;}
        public Bulletin_Board__c bb {get; private set;}

        public HBBInfo(Product_Documentation__c pd) {
            linkId = pd.Id;
            openday = pd.Material_open_day__c;
            daiBunrui = Schema.SObjectType.Product_Documentation__c.Label;
            bunrui = pd.Material_category__c;
            subject = pd.Product_document_name__c;
        }

        public HBBInfo(Solution faq) {
            linkId = faq.Id;
            openday = faq.FAQ_disclosed_day__c;
            daiBunrui = Schema.SObjectType.Solution.Label;
            bunrui = faq.Question_category__c;
            subject = faq.SolutionName;
        }

        public HBBInfo(Campaign cam) {
            linkId = cam.Id;
            openday = cam.StartDate;
            daiBunrui = Schema.SObjectType.Campaign.Label;
            bunrui = cam.RecordType.Name;
            subject = cam.Name;
        }

        public HBBInfo(Bulletin_Board__c bb) {
            linkId = bb.Id;
            openday = bb.Board_open_day__c;
            daiBunrui = Schema.SObjectType.Bulletin_Board__c.Label;
            bunrui = bb.Board_category1__c;
            subject = bb.Name;
            this.bb = bb;
        }

        // openday順
        public Integer compareTo(Object compareTo) {
            HBBInfo bbInfo = (HBBInfo)compareTo;
            if (openday == bbInfo.openday) return 0;
            if (openday > bbInfo.openday) return 1;
            return -1;
        }
    }
}