private void ActivateRefinery(IRefineryActivity activity)
        {
            bool hasRequirement = activity.HasActivityRequirements(); // if the requirements for the activity are fulfilled

            if (hasRequirement)
            {
                currentActivity     = activity; // the activity will be treated as the current activity
                refinery_is_enabled = true;     // refinery is now on
            }
            else
            {
                activity.PrintMissingResources();
            }
        }
        public override void OnStart(PartModule.StartState state)
        {
            powerSupply = part.FindModuleImplementing <IPowerSupply>();

            if (powerSupply != null)
            {
                powerSupply.DisplayName = Localizer.Format("#LOC_KSPIE_Refinery_started");//"started"
            }
            if (state == StartState.Editor)
            {
                return;
            }

            // load stored overflow setting
            overflowAllowed = lastOverflowSettings;

            _window_ID = new System.Random(part.GetInstanceID()).Next(int.MinValue, int.MaxValue);

            _refinery_activities = part.FindModulesImplementing <IRefineryActivity>();
            if (_refinery_activities == null || _refinery_activities.Count == 0)
            {
                var unsortedList = new List <IRefineryActivity>();

                unsortedList.Add(new AnthraquinoneProcessor());
                unsortedList.Add(new NuclearFuelReprocessor());
                unsortedList.Add(new AluminiumElectrolyser());
                unsortedList.Add(new SabatierReactor());
                unsortedList.Add(new WaterElectroliser());
                unsortedList.Add(new HeavyWaterElectroliser());
                unsortedList.Add(new PeroxideProcess());
                unsortedList.Add(new UF4Ammonolysiser());
                unsortedList.Add(new HaberProcess());
                unsortedList.Add(new AmmoniaElectrolyzer());
                unsortedList.Add(new CarbonDioxideElectroliser());
                unsortedList.Add(new WaterGasShift());
                unsortedList.Add(new ReverseWaterGasShift());
                unsortedList.Add(new PartialOxidationMethane());
                unsortedList.Add(new SolarWindProcessor());
                unsortedList.Add(new RegolithProcessor());
                unsortedList.Add(new AtmosphericExtractor());
                unsortedList.Add(new SeawaterExtractor());

                _refinery_activities = unsortedList.Where(m => ((int)m.RefineryType & this.refineryType) == (int)m.RefineryType).OrderBy(a => a.ActivityName).ToList();
            }

            _refinery_activities.ForEach(m => m.Initialize(this.part));

            // load same
            if (refinery_is_enabled && !string.IsNullOrEmpty(lastActivityName))
            {
                Debug.Log("[KSPI]: ISRU Refinery looking to restart " + lastActivityName);
                _current_activity = _refinery_activities.FirstOrDefault(a => a.ActivityName == lastActivityName);

                if (_current_activity == null)
                {
                    Debug.Log("[KSPI]: ISRU Refinery looking to restart " + lastClassName);
                    _current_activity = _refinery_activities.FirstOrDefault(a => a.GetType().Name == lastClassName);
                }
            }

            if (_current_activity != null)
            {
                bool hasRequirement = _current_activity.HasActivityRequirements();
                lastActivityName = _current_activity.ActivityName;

                Debug.Log("[KSPI]: ISRU Refinery initializing " + lastActivityName + " for which hasRequirement: " + hasRequirement);

                var productionModifier = productionMult * baseProduction;

                var timeDifference = (Planetarium.GetUniversalTime() - lastActiveTime);

                if (timeDifference > 0.01)
                {
                    string message = Localizer.Format("#LOC_KSPIE_Refinery_Postmsg1", lastActivityName, timeDifference.ToString("0"));//"IRSU performed " +  + " for " +  + " seconds"
                    Debug.Log("[KSPI]: " + message);
                    ScreenMessages.PostScreenMessage(message, 20, ScreenMessageStyle.LOWER_CENTER);
                }

                if (lastActivityName == "Atmospheric Extraction")
                {
                    ((AtmosphericExtractor)_current_activity).ExtractAir(lastPowerRatio * productionModifier, lastPowerRatio, productionModifier, lastOverflowSettings, timeDifference, true);
                }
                else if (lastActivityName == "Seawater Extraction")
                {
                    ((SeawaterExtractor)_current_activity).ExtractSeawater(lastPowerRatio * productionModifier, lastPowerRatio, productionModifier, lastOverflowSettings, timeDifference, true);
                }
                else
                {
                    _current_activity.UpdateFrame(lastPowerRatio * productionModifier, lastPowerRatio, productionModifier, lastOverflowSettings, timeDifference, true);
                }
            }
        }
        public override void OnStart(StartState state)
        {
            powerSupply = part.FindModuleImplementing <IPowerSupply>();

            if (powerSupply != null)
            {
                powerSupply.DisplayName = Localizer.Format("#LOC_KSPIE_Refinery_started"); //"started"
            }
            if (state == StartState.Editor)
            {
                return;
            }

            // load stored overflow setting
            _overflowAllowed = lastOverflowSettings;

            _windowId = new System.Random(part.GetInstanceID()).Next(int.MinValue, int.MaxValue);

            var refineriesList = part.FindModulesImplementing <IRefineryActivity>().ToList();

            if (refineryType > 0)
            {
                AddIfMissing(refineriesList, new AluminiumElectrolyzer());
                AddIfMissing(refineriesList, new AmmoniaElectrolyzer());
                AddIfMissing(refineriesList, new AnthraquinoneProcessor());
                AddIfMissing(refineriesList, new AtmosphereProcessor());
                AddIfMissing(refineriesList, new CarbonDioxideElectrolyzer());
                AddIfMissing(refineriesList, new HaberProcess());
                AddIfMissing(refineriesList, new HeavyWaterElectrolyzer());
                AddIfMissing(refineriesList, new PartialMethaneOxidation());
                AddIfMissing(refineriesList, new PeroxideProcess());
                AddIfMissing(refineriesList, new UF4Ammonolysiser());
                AddIfMissing(refineriesList, new RegolithProcessor());
                AddIfMissing(refineriesList, new ReverseWaterGasShift());
                AddIfMissing(refineriesList, new NuclearFuelReprocessor());
                AddIfMissing(refineriesList, new SabatierReactor());
                AddIfMissing(refineriesList, new OceanProcessor());
                AddIfMissing(refineriesList, new SolarWindProcessor());
                AddIfMissing(refineriesList, new WaterElectrolyzer());
                AddIfMissing(refineriesList, new WaterGasShift());

                availableRefineries = refineriesList
                                      .Where(m => ((int)m.RefineryType & refineryType) == (int)m.RefineryType)
                                      .OrderBy(a => a.ActivityName).ToList();
            }
            else
            {
                availableRefineries = refineriesList.OrderBy(a => a.ActivityName).ToList();
            }

            // initialize refineries
            foreach (var availableRefinery in availableRefineries)
            {
                try
                {
                    availableRefinery.Initialize(part, this);
                }
                catch (Exception e)
                {
                    Debug.LogError("[KSPI]: Failed to initialize " + availableRefinery.ActivityName + " with exception: " + e.Message);
                }
            }

            // load same
            if (refinery_is_enabled && !string.IsNullOrEmpty(lastActivityName))
            {
                Debug.Log("[KSPI]: ISRU Refinery looking to restart " + lastActivityName);
                currentActivity = availableRefineries.FirstOrDefault(a => a.ActivityName == lastActivityName);

                if (currentActivity == null)
                {
                    Debug.Log("[KSPI]: ISRU Refinery looking to restart " + lastClassName);
                    currentActivity = availableRefineries.FirstOrDefault(a => a.GetType().Name == lastClassName);
                }
            }

            if (currentActivity != null)
            {
                bool hasRequirement = currentActivity.HasActivityRequirements();
                lastActivityName = currentActivity.ActivityName;

                Debug.Log("[KSPI]: ISRU Refinery initializing " + lastActivityName + " for which hasRequirement: " + hasRequirement);

                var timeDifference = (Planetarium.GetUniversalTime() - lastActiveTime);

                if (timeDifference > 0.01)
                {
                    string message = Localizer.Format("#LOC_KSPIE_Refinery_Postmsg1", lastActivityName, timeDifference.ToString("0")); //"IRSU performed " +  + " for " +  + " seconds"
                    Debug.Log("[KSPI]: " + message);
                    ScreenMessages.PostScreenMessage(message, 20, ScreenMessageStyle.LOWER_CENTER);
                }

                var productionModifier = productionMult * baseProduction;
                if (lastActivityName == "Atmospheric Extraction")
                {
                    ((AtmosphereProcessor)currentActivity).ExtractAir(lastPowerRatio * productionModifier,
                                                                      lastPowerRatio, productionModifier, lastOverflowSettings, timeDifference, true);
                }
                else if (lastActivityName == "Seawater Extraction")
                {
                    ((OceanProcessor)currentActivity).ExtractSeawater(lastPowerRatio * productionModifier,
                                                                      lastPowerRatio, productionModifier, lastOverflowSettings, timeDifference, true);
                }
                else
                {
                    currentActivity.UpdateFrame(lastPowerRatio * productionModifier, lastPowerRatio,
                                                productionModifier, lastOverflowSettings, timeDifference, true);
                }
            }
        }