package basic.BuilderTypeManager;

import android.text.TextUtils;

import basic.BuilderTypeManager.model.BuildTypeUtil;
import basic.BuilderTypeManager.model.IBuilderTypeInterface;
import basic.BuilderTypeManager.model.SimpleBuilderTypeFactory;
import basic.app.TvAppLike;

/**
 * Created by fanwang on 9/8/16.
 *
 * this class managers old BuilderType configs
 */
public class BuilderTypeManager {
    //fix android system bug ,for reading meta as int not string
    private static final String CID_ = "cid_";

    private SimpleBuilderTypeFactory simpleBuilderTypeFactory = null;
    private IBuilderTypeInterface iBuilderTypeInterface = null;
    private String channelId = null;

    private static BuilderTypeManager sInatance;

    private BuilderTypeManager(){
        //get channelid from manifest meta
        channelId = BuildTypeUtil.readMetaDataFromApplication("channelid", TvAppLike.getContext());

        if(channelId.contains(CID_)){
            channelId = channelId.replace(CID_,"");
        }

        // create a builderTypeInterface according to channelid
        if(!TextUtils.isEmpty(channelId)){
            simpleBuilderTypeFactory = new SimpleBuilderTypeFactory();
            iBuilderTypeInterface = simpleBuilderTypeFactory.createBuilderType(channelId);
        }
    }

    public static BuilderTypeManager getInstance(){
        if(sInatance == null){
            synchronized (BuilderTypeManager.class){
                if(sInatance == null){
                    sInatance = new BuilderTypeManager();
                }
            }
        }

        return sInatance;
    }

    /**
     *
     * @return get BuilderType reference
     */
    public IBuilderTypeInterface getiBuilderTypeInterface(){
        return iBuilderTypeInterface;
    }

    /**
     *
     * @return channelId
     */
    public String getChannelId(){
        return channelId;
    }
}
