public with sharing class CampaignBTPDFController {
	//
	private Id btId;
	public campaignInfo campaignRecords { get; set; }
    // SWAG-BHW3Y9 update by vivek 20200108 start
    public List<BusinessTwo> businessPurposeTwoList { get; set; }
    public List<BusinessTwo> businessResultTwoList { get; set; }
    public List<BusinessTwo> businessHoldTwoList { get; set; }
    // SWAG-BHW3Y9 update by vivek 20200108 end
	private Integer nameMax = 200;   //名称最大长度 全是中文的话 最多18 但因为不会全是中文 所以设为20
	public CampaignBTPDFController() {
        // btId='70110000000urw2AAA';
		btId = ApexPages.currentPage().getParameters().get('id');
	}

	public void init() {
		//campaignRecords = new campaignInfo();
		Campaign cam = [select Id,Name,createddate,Status,report_Status__c,Owner.Category5__c,Owner.Category6__c,OwnerName__c,
		                    	Name2__c,StartDate,EndDate,State_Master__r.Name,City_Master__r.Name,Town__c,
		                    	WorkshopPlace__c,Meeting_Type__c,
                                // SWAG-BF39A8 start total_participants__c
                                Convening_Participants_Num__c
                                // SWAG-BF39A8 end
                                ,BusinessPurpose__c,
		                    	BusinessPurpose_det__c,BusinessResult_det__c,Society_Hold_Place_det__c,
		                    	Society_Hold_Place__c,BusinessResult__c from Campaign where id = : btId];

		// SWAG-BHW3Y9 update by vivek 20200108 start
        
        // List<String> businessPurposeList = pictureFormat(cam.BusinessPurpose_det__c,'','255');
        // cam.BusinessPurpose_det__c = businessPurposeList[0];
        // cam.BusinessPurpose_detRight__c = businessPurposeList[1].replace('00N10000008qovu', '00Np0000004IaHj');
        businessPurposeTwoList = new List<BusinessTwo>();
        businessResultTwoList = new List<BusinessTwo>();
        businessHoldTwoList = new List<BusinessTwo>();
        businessPurposeTwoList = pictureFormat(cam.BusinessPurpose_det__c,'','510');
        businessResultTwoList = pictureFormat(cam.BusinessResult_det__c,'','510');
        businessHoldTwoList = pictureFormat(cam.Society_Hold_Place_det__c,'','510');
        System.debug('businessPurposeTwoList======'+businessPurposeTwoList);
        // SWAG-BHW3Y9 update by vivek 20200108 end
        campaignRecords = new campaignInfo(cam,nameMax);
	}
    // SWAG-BHW3Y9 update by vivek 20200108 start
    // 用来格式化图片大小
    public List<BusinessTwo> pictureFormat(String pstr,String pht,String pwt){
        if(pstr == null || pstr == ''){
            return null;
        }
        List<BusinessTwo> businessTwoList = new List<BusinessTwo>();
        // 添加了表格start
        pstr = '>' + pstr + '<';
        String imgStrT = pstr.replaceAll('<table.*</table>','').replaceAll('>[^>]*<','><');
        // System.debug(imgStrT.substring(0,imgStrT.length()-4));
        String imgStr = imgStrT.substring(1,imgStrT.length()-1);
        // String tableStrT = pstr.replaceAll('<img.*</img>','').replaceAll('>[^>]*<','><');
        pstr = '</table' + pstr;
        String tableStrT = pstr.replaceAll('<img.*</img>','').replaceAll('</table[^<]*<', '</table><');
        // System.debug(tableStrT.substring(0,tableStrT.length()-4));
        String tableStr = tableStrT.substring(8,tableStrT.length()-1);
        // System.debug(tableStr.replaceAll('<table[^>]*>','<table align="left" border="1" cellpadding="0" cellspacing="0" style="width: '+pwt+'px;">'));

        // 表格格式处理
        BusinessTwo bpttable = new BusinessTwo();
        // bpttable.busImgSrcL = tableStr.replaceAll('<table[^>]*>','<table align="left" border="1" cellpadding="0" cellspacing="0" style="width: '+pwt+'px;">');
        bpttable.busImgStyleL = '';
        // Double pwtl = Double.valueOf(pwt)-50;
        Campaign campaignL = new Campaign();
        campaignL.BusinessPurpose_det__c = tableStr.replaceAll('<table[^>]*>','<table align="center" border="1" cellpadding="0" cellspacing="0" style="width: '+pwt+'px;">');
        bpttable.cn = campaignL;
        businessTwoList.add(bpttable);


        // 图片格式处理
        // String str = pstr;
        String str = imgStr;
        System.debug('pstr'+pstr);
        String pHeight = pht;
        String pWidth = pwt;
        Double pWht = 0;
        if(pHeight != ''){
            pWht = Double.valueOf(pHeight);
        }else{
            pWht = Double.valueOf(pWidth);
        }
        
        List<String> strlist = str.split('</img>');
        if(!strlist[strlist.size()-1].contains('<img')){
            strlist.remove(strlist.size()-1);
        }
        System.debug('strlist======'+strlist);
        // Map<Integer,Double> strMap = new Map<Integer,Double>();
        for(Integer s = 0 ; s < strlist.size() ; s++ ){
            List<String> strlist1 =  strlist[s].split('px;');
            System.debug('strlist1======'+strlist1+'');
            System.debug('strlist1.size()======'+strlist1.size());
            if(strlist1.size() == 1){
                if(pHeight != ''){
                    strlist1[0] = strlist1[0].substring(0,strlist1[0].length()-1)+'style="height: '+pHeight+'px; width: auto;">';
                }else{
                    strlist1[0] = strlist1[0].substring(0,strlist1[0].length()-1)+'style="height: auto; width: '+pWidth+'px;">';
                }
                
                strlist[s] = strlist1[0]; 
            }
            if(strlist1.size() == 3){
                List<String> strlist2 = strlist1[0].split(' ');
                System.debug('strlist2======'+strlist2);
                List<String> strlist3 = strlist1[1].split(' ');
                System.debug('strlist3======'+strlist3);
                double imgheight = Double.valueOf(strlist2[strlist2.size()-1]);
                double imgwidth = Double.valueOf(strlist3[strlist3.size()-1]);
                double imgwht = 0;
                if(pHeight != ''){
                    imgwht = imgheight;
                }else{
                    imgwht = imgwidth;
                }
                // double bilv = pHeight/imgheight;
                if(imgwht > pWht ){
                    // imgheight = imgheight*bilv;
                    // imgwidth = imgwidth*bilv;
                    // imgheight = pHeight;
                    System.debug('strlist:'+s+'imgheight======'+imgheight+'imgwidth======'+imgwidth);
                    if(pHeight != ''){
                        strlist2[strlist2.size()-1] = pHeight+'px;';
                        strlist3[strlist3.size()-1] = 'auto;';
                    }else{
                        strlist2[strlist2.size()-1] = 'auto;';
                        strlist3[strlist3.size()-1] = pWidth+'px;';
                    }
                    
                    String strlist2str = '';
                    String strlist3str = '';
                    for(String j : strlist2){
                        strlist2str += ' '+j;
                    }
                    for(String j : strlist3){
                        strlist3str += ' '+j;
                    }
                    strlist1[0] = strlist2str;
                    strlist1[1] = strlist3str;
                    String strliststr = '';
                    for(Integer i = 0 ; i< strlist1.size() ; i++){
                        strliststr += strlist1[i];
                    }
                    System.debug('strliststr======'+strliststr);
                    strlist[s] = strliststr;
                }
            }
        }
        String str1 = '';
        String str2 = '';
        System.debug('strlist=========='+strlist);
        if(strlist.size() == 1){
            BusinessTwo bpt = new BusinessTwo();
            bpt.busImgSrcL = strlist[0].split('src=')[1].split('style=')[0].replace('"','').replace('amp;','');
            bpt.busImgStyleL = strlist[0].split('src=')[1].split('style=')[1].replace('"','');
            businessTwoList.add(bpt);
        }else{
            // for(Integer i = 0, j = 1 ; j <= strlist.size() ; j++){
            for(Integer i = 0 ; i < strlist.size() ; i++){
                // if(isDivisibility(i,2)){
                    BusinessTwo bpt = new BusinessTwo();

                    bpt.busImgSrcL = strlist[i].split('src=')[1].split('style=')[0].replace('"','').replace('amp;','');
                    bpt.busImgStyleL = strlist[i].split('src=')[1].split('style=')[1].replace('"','');

                    // if(j >= strlist.size()){
                    //     break;
                    // }
                    // bpt.busImgSrc = strlist[j].split('src=')[1].split('style=')[0].replace('"','').replace('amp;','');
                    // bpt.busImgStyle = strlist[j].split('src=')[1].split('style=')[1].replace('"','');
                    businessTwoList.add(bpt);
                // }
                // i++;
                
            }
        }
        // if(str.substring(str.length()-6) == '</img>'){
        //     str1 += '</img>';
        // }
        // List<String> returnList = new List<String>();
        // returnList.add(str1);
        // returnList.add(str2);
        // System.debug('str1======'+str1.replace('amp;', ''));
        // System.debug('str2======'+str2.replace('amp;', ''));
        // System.debug('returnList======'+returnList);

        return businessTwoList;
    }

    public Boolean isDivisibility(Integer dividend,Integer divider) {
        //先将被除数转换成Decimal，否则先计算整数除法结果为Integer类型，然后将整数转换成Decimal
        Decimal resultDecimal = Decimal.valueOf(dividend) / divider;
        //获取结果上线
        Integer resultSeiling = Integer.valueOf(resultDecimal.round(System.RoundingMode.CEILING));
        Integer resultDown = Integer.valueOf(resultDecimal.round(System.RoundingMode.DOWN));
        
        return resultSeiling == resultDown;
    }

    class BusinessTwo{
        public String busImgSrcL { get; set; }
        public String busImgStyleL { get; set; }
        public String busImgSrc { get; set; }
        public String busImgStyle { get; set; }

        public Campaign cn { get; set; }

        public BusinessTwo(){
            this.busImgStyleL = '';
            this.busImgSrcL = '';
            this.busImgSrc = '';
            this.busImgStyle = '';
            this.cn = null;
        }
    }
    // SWAG-BHW3Y9 update by vivek 20200108 end
    

	// Data Bean
    class campaignInfo implements Comparable {
    	public Campaign cn { get; set; }
    	// 从富文本中拆出来的文本
        public String businessPurpose { get; set; }
        public String businessResult { get; set; }
        public String society_Hold_Place { get; set; }

        // SWAG-BHW3Y9 update by vivek 20200108 start
        public String BusinessPurpose_det { get; set; }
        // SWAG-BHW3Y9 update by vivek 20200108 end
        // 
        public campaignInfo(Campaign e) {
            cn = e;
        }

        //private String Substr(Integer length, String parameter){
        //    String params = '';
        //    Integer size = (parameter.length() / length) + (Math.mod(parameter.length(), length) > 0 ? 1 :0);
        //    if(size == 0){
        //        size = 1;
        //    }
        //    for(Integer I =0; I<size;I++){
        //        String s;
        //        if(I == (size-1)){
        //            s = parameter.substring(I*length,parameter.length());
        //        }else{
        //            s = parameter.substring(I*length,(I+1)*length)+'<br/>';
        //        }
                
        //        params += s;
        //    }
        //    return params;
        //}

        public campaignInfo(Campaign mcae, Integer nameMax) {
            this.cn = mcae;

         //   public String businessPurpose { get; set; }
	        //public String businessResult { get; set; }
	        //public String society_Hold_Place { get; set; }

            //this.NeedHide = HideOrNot;
                // Assetある
            this.businessPurpose = mcae.BusinessPurpose__c;
            this.businessResult = mcae.BusinessResult__c;
            this.society_Hold_Place = mcae.Society_Hold_Place__c;

            // SWAG-BHW3Y9 update by vivek 20200108 start
            // this.BusinessPurpose_det = 'https://ocsm--StageFull--c.cs31.content.force.com/servlet/rtaImage?eid=701p0000000IX0b&feoid=00N10000008qovu&refid=0EMp00000005Foh';
            // SWAG-BHW3Y9 update by vivek 20200108 end

            this.businessPurpose = businessPurpose == null? '' : businessPurpose.replaceAll('<[^>]*>','');
            this.businessResult = businessResult == null? '' : businessResult.replaceAll('<[^>]*>','');
            this.society_Hold_Place = society_Hold_Place == null? '' : society_Hold_Place.replaceAll('<[^>]*>','');
            //this.businessPurpose = Substr(nameMax,businessPurpose);
            //this.businessResult = Substr(nameMax,businessResult);
            //this.society_Hold_Place = Substr(nameMax,society_Hold_Place);
        }
        // 排序
        public Integer compareTo(Object compareTo) {
            return null;
        }
    }
}